-
Notifications
You must be signed in to change notification settings - Fork 18
Any way to conditionally format the data in a column? #6
Comments
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(); |
Awesome. Thanks so much for taking the time to respond! 🎉 |
@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: 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. 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 ) : |
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. |
Fixed issue with dynamic colors in jc21#6
I would love a way to be able to configure very basic conditional formatting, so I could have a 'positive value' colour and a 'negative value' colour when displaying a number. Is this possible do you know?
The text was updated successfully, but these errors were encountered: