Giter Club home page Giter Club logo

Comments (4)

jc21 avatar jc21 commented on June 26, 2024

Hey yep you can do this with your own manipulator:

class MyManipulator extends CliTableManipulator {
	public function colorNumber($value)
	{
		if ($value == 0) {
		    // default color
		    return $value;
		} else if ($value >= 0) {
		    // green
		    return chr(27) . '[1;32m' . $value . chr(27).'[0m';
		} else {
		    // red
		    return chr(27) . '[1;31m' . $value . chr(27).'[0m';
		}
	}
}

$table = new CliTable;
$table->setTableColor('blue');
$table->setHeaderColor('cyan');
$table->addField('First Name', 'firstName', false, 'white');
$table->addField('Last Name', 'lastName', false, 'white');
$table->addField('Number', 'numberColumn', new MyManipulator('colorNumber'));
$table->injectData($data);
$table->display();

from clitable.

johnclearyconsulting avatar johnclearyconsulting commented on June 26, 2024

Awesome. Thanks so much for taking the time to respond! 🎉

from clitable.

conrad10781 avatar conrad10781 commented on June 26, 2024

@jc21 , I'm not sure if you'll see this comment given the issue is closed, but I followed the above code , and am getting an unexpected output:

Screenshot from 2023-09-01 09-15-33

In my code, I pass the default value back if it's >= 5 && <= 5, but outside that it's the same code. The issue seems to be related to https://github.com/jc21/clitable/blob/master/src/jc21/CliTable.php#L403 where since we're passing back the extra color "information", mb_strwidth is showing significantly larger values.

If I update that line to:

$c = chr(27);
$fieldLength  = mb_strwidth(preg_replace("/({$c}\[(.*?)m)/", '', $field)) + 1;

I get a consistent column width, and even though $fieldLength appears to be reporting the correct values, the width of the columns are larger than expected.

Screenshot from 2023-09-01 10-26-50

versus the width with the code out of the box ( notice the padding width of the columns that previously had the colors from the Manipulator ) :

Screenshot from 2023-09-01 10-27-42

from clitable.

conrad10781 avatar conrad10781 commented on June 26, 2024

Nevermind, I see https://github.com/jc21/clitable/blob/master/src/jc21/CliTable.php#L354 plays a role in the length as well. Will submit a PR.

from clitable.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.