-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Colorize one cell of table #107
base: main
Are you sure you want to change the base?
Conversation
takes into account different scenarios
thank you 👍. however it has to be implemented differently. like using styles. eg: $writer->table([
['a' => 1, 'b' => 2],
['a' => 3, 'b' => 8],
['a' => 7, 'b' => 32],
], [
'head' => '', // for heading
'odd' => '', // for odd rows
'even' => '', // for even rows
'1:1' => '', // for cell in row 1 col 1 (1 based count)
'2:*' => '', // for all cells in row 2 (1 based count)
'*:2' => '', // for all cells in col 2 (1 based count)
'b' => '', // for all columns named 'b' (same as '*:2' in this example)
]); priority in increasing order:
actually this was thought out long ago: |
It's true that we could also do something like that, but I have a problem with that approach. For exemple $table = [];
$users = $db->getUsers();
$color = new Color();
$writer = new Writer();
foreach ($users as $user) {
$table[] = [
'Username' => $user->username,
'Statut' => $user->active ? $color->ok('Active') : $code->error('Inactive'),
'Role' => $color->line($user->role, [
'fg' => match($user->role) {
'admin' => Color::GREEN,
'moderator' => Color::YELLOW,
default => Color::WHITE
}
]),
]
}
$writer->table($table); You can see that in such a case, you can't really know in advance what colour the 2:3 cell should be. The table I showed in the image above is dynamically generated. |
can we still do the way i recommended and for complex edge cases like the one you mentioned of dynamic data, the style can support a callback eg: since you would already know the column of role (lets say 2), then
for implementation, all you need to check is if the style is a callback or string. if callback call it with (val, row, table) and use resulting string as style ;) |
@adhocore I implemented this functionality using the $styles parameter. |
this PR allows the developer to define a particular style for a specific table cell without necessarily impacting the entire row (as with the
old
andeven
parameters in the table definition).in fact, by using
old
oreven
, all the even or odd rows will have the same appearance and it would be interesting to be able to highlight a specific element in the table. hence this PR.I've also written tests for the table generator, taking into account several possible scenarios
Before this PR (the elements in the table are not correctly aligned - for example, look line 3 :
display_startup_errors
)After this PR
Step to test