Skip to content
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

Add support for outputting 256 colors and also add documentation about default command functionality #110

Merged
merged 61 commits into from
Nov 26, 2024

Conversation

kodie
Copy link
Contributor

@kodie kodie commented Nov 25, 2024

As the title says, this PR allows us to use 256 colors in our custom styles like so:

\Ahc\Cli\Output\Color::style('mystyle', [
  'fg' => '38;5;57',
  'bg' => '48;5;82'
]);

and all it took was removing typecasting on the fg style value (allowing us to pass strings) and doing a check on the bg style value to see if it's being passed an interval or not, if it is (default behavior) then add 10 to it and use it just the same before, but if it's not an interval (like a string) then just use that value, allowing us to use 256 colors.

This opens up the world of setting custom colors:

\Ahc\Cli\Output\Color::style('indigo', ['fg' => '38;5;57']);
\Ahc\Cli\Output\Color::style('lime', ['fg' => '38;5;190']);
$writer = new Writer();
$writer->colors("<indigo>Loading</end> <lime>{$input_file}</end>...", true);

I added a small example of this to the readme and also added documentation about the default command functionality as that seemed to be missing.

@kodie
Copy link
Contributor Author

kodie commented Nov 25, 2024

If we wanted to get real fancy, we could add two methods that add the prefixes for us:

\Ahc\Cli\Output\Color::style('mystyle', [
  'fg' => \Ahc\Cli\Output\Color::fg256(57),
  'bg' => \Ahc\Cli\Output\Color::bg256(82)
]);

Just an idea, thoughts?

@kodie
Copy link
Contributor Author

kodie commented Nov 25, 2024

Orrrrr, we could have it be it's own style property that can fallback for terminals that don't support 256:

\Ahc\Cli\Output\Color::style('mystyle', [
  'bg' => Ahc\Cli\Output\Color::CYAN, // used if no 256-support
  'fg' => Ahc\Cli\Output\Color::WHITE, // used if no 256-support
  'bg_256' => 82,
  'fg_256' => 57
]);

Just another idea. But I'm totally fine with just how the PR is now.

@adhocore
Copy link
Owner

adhocore commented Nov 26, 2024

hello, thank you for this idea/ feature/ pull. and yes we can add the color consts Color::RED6, Color::GREEN6 etc for common/major colors and static func Color::c6($code) for the rest.

@adhocore
Copy link
Owner

cc @dimtrovich more new exciting pulls might need your look 😁 😉

@kodie
Copy link
Contributor Author

kodie commented Nov 26, 2024

I figured the first suggestion was slightly better than the second as it only adds two methods rather than changing the flow of things (text, style, mod, etc).

So I've implemented this the fg256 and bg256 methods like so:

\Ahc\Cli\Output\Color::style('mystyle', [
  'fg' => \Ahc\Cli\Output\Color::fg256(57),
  'bg' => \Ahc\Cli\Output\Color::bg256(82)
]);

src/Output/Color.php Outdated Show resolved Hide resolved
adhocore
adhocore previously approved these changes Nov 26, 2024
Copy link
Owner

@adhocore adhocore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, thanks

@adhocore
Copy link
Owner

is it possible to share a screenshot of 256 colors using this code, as well as from the other pulls? :D

@kodie
Copy link
Contributor Author

kodie commented Nov 26, 2024

is it possible to share a screenshot of 256 colors using this code, as well as from the other pulls? :D

I have a branch on my fork where I have been merging all of my PRs together for testing (https://github.com/kodie/adhocore-php-cli/tree/kodie/development).

Given this code:

$app = new Ahc\Cli\Application('My App', $version);
$writer = new Ahc\Cli\Output\Writer();
\Ahc\Cli\Output\Color::style('indigo', array('fg' => \Ahc\Cli\Output\Color::fg256(57)));
\Ahc\Cli\Output\Color::style('spring', array('fg' => \Ahc\Cli\Output\Color::RED, 'bg' => \Ahc\Cli\Output\Color::bg256(49)));
$help = "<indigo>Hello</end>\n<spring>World</end>";
$app->help($writer->colorizer()->colors($help));
$app->handle($_SERVER['argv']);

and running

./bin/my-app --help

I get this:
Screenshot 2024-11-25 220049

adhocore
adhocore previously approved these changes Nov 26, 2024
@adhocore
Copy link
Owner

thanks, looks good, lets see if @dimtrovich would like to have a look as well

@dimtrovich
Copy link
Contributor

thanks, looks good, lets see if @dimtrovich would like to have a look as well

For the moment I'm still at home. As soon as I get to the office, I'll take a closer look.

@kodie
Copy link
Contributor Author

kodie commented Nov 26, 2024

Here's a better screenshot of the additional colors as well as the customizable help styles from #111

Screenshot 2024-11-25 232723

@kodie
Copy link
Contributor Author

kodie commented Nov 26, 2024

Just playing around with colors. Added even and odd capabilities to #111

Screenshot 2024-11-26 000031

// Custom colors
\Ahc\Cli\Output\Color::style('indigo', array('fg' => \Ahc\Cli\Output\Color::fg256(57)));
\Ahc\Cli\Output\Color::style('orange', array('fg' => \Ahc\Cli\Output\Color::fg256(214)));
\Ahc\Cli\Output\Color::style('spring', array('fg' => \Ahc\Cli\Output\Color::fg256(49)));

\Ahc\Cli\Output\Color::style('question', array('fg' => \Ahc\Cli\Output\Color::fg256(57)));
\Ahc\Cli\Output\Color::style('choice', array('fg' => \Ahc\Cli\Output\Color::fg256(49)));
\Ahc\Cli\Output\Color::style('logo', array('fg' => \Ahc\Cli\Output\Color::fg256(172)));
\Ahc\Cli\Output\Color::style('choice', array('fg' => \Ahc\Cli\Output\Color::fg256(49)));
\Ahc\Cli\Output\Color::style('help_category', array('fg' => \Ahc\Cli\Output\Color::fg256(49)));
\Ahc\Cli\Output\Color::style('help_example', array('fg' => \Ahc\Cli\Output\Color::fg256(213)));
\Ahc\Cli\Output\Color::style('help_summary', array('fg' => \Ahc\Cli\Output\Color::fg256(190)));
\Ahc\Cli\Output\Color::style('help_description_even', array('fg' => \Ahc\Cli\Output\Color::fg256(250)));
\Ahc\Cli\Output\Color::style('help_description_odd', array('fg' => \Ahc\Cli\Output\Color::fg256(230)));
\Ahc\Cli\Output\Color::style('help_item_even', array('fg' => \Ahc\Cli\Output\Color::fg256(230)));
\Ahc\Cli\Output\Color::style('help_item_odd', array('fg' => \Ahc\Cli\Output\Color::fg256(250)));
\Ahc\Cli\Output\Color::style('help_footer', array('fg' => \Ahc\Cli\Output\Color::fg256(39)));
\Ahc\Cli\Output\Color::style('help_summary', array('fg' => \Ahc\Cli\Output\Color::fg256(58)));
\Ahc\Cli\Output\Color::style('help_text', array('fg' => \Ahc\Cli\Output\Color::fg256(174)));

@dimtrovich
Copy link
Contributor

it's cool, thanks.

@kodie
Copy link
Contributor Author

kodie commented Nov 26, 2024

@kodie pull needs a rebase

@adhocore Done!

@adhocore
Copy link
Owner

@kodie it still shows merge conflict:

This branch has conflicts that must be resolved
Use the web editor or the  to resolve conflicts.
Conflicting files
src/Output/Color.php
tests/Output/ColorTest.php

besides that i think good to go

@kodie
Copy link
Contributor Author

kodie commented Nov 26, 2024

@kodie it still shows merge conflict:

This branch has conflicts that must be resolved
Use the web editor or the  to resolve conflicts.
Conflicting files
src/Output/Color.php
tests/Output/ColorTest.php

besides that i think good to go

@adhocore I just pushed another update that should fix that

@adhocore adhocore merged commit 86e02bf into adhocore:main Nov 26, 2024
6 checks passed
@kodie
Copy link
Contributor Author

kodie commented Nov 26, 2024

Just wanted to tag this resource in case anyone is looking for a naming system for custom colors: https://gist.github.com/kodie/b63881ccf4368e9e00c0c3277779524a

I don't think it's worth making naming look up for 256 colors worth a feature but having a list is a sort of nice resource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants