Skip to content

Instantly share code, notes, and snippets.

@henriquemoody
Created July 16, 2012 17:08
Show Gist options
  • Save henriquemoody/3123806 to your computer and use it in GitHub Desktop.
Save henriquemoody/3123806 to your computer and use it in GitHub Desktop.
<?php
// https://wiki.archlinux.org/index.php/Color_Bash_Prompt
$colors = array(
// regular colors
'black' => '0;30', // black
'red' => '0;31', // red
'green' => '0;32', // green
'yellow' => '0;33', // yellow
'blue' => '0;34', // blue
'purple' => '0;35', // purple
'cyan' => '0;36', // cyan
'white' => '0;37', // white
// bold
'bold_black' => '1;30', // black
'bold_red' => '1;31', // red
'bold_green' => '1;32', // green
'bold_yellow' => '1;33', // yellow
'bold_blue' => '1;34', // blue
'bold_purple' => '1;35', // purple
'bold_cyan' => '1;36', // cyan
'bold_white' => '1;37', // white
// underline
'underline_black' => '4;30', // black
'underline_red' => '4;31', // red
'underline_green' => '4;32', // green
'underline_yellow' => '4;33', // yellow
'underline_blue' => '4;34', // blue
'underline_purple' => '4;35', // purple
'underline_cyan' => '4;36', // cyan
'underline_white' => '4;37', // white
// background
'background_black' => '40', // black
'background_red' => '41', // red
'background_green' => '42', // green
'background_yellow' => '43', // yellow
'background_blue' => '44', // blue
'background_purple' => '45', // purple
'background_cyan' => '46', // cyan
'background_white' => '47', // white
// high intensity
'intense_black' => '0;90', // black
'intense_red' => '0;91', // red
'intense_green' => '0;92', // green
'intense_yellow' => '0;93', // yellow
'intense_blue' => '0;94', // blue
'intense_purple' => '0;95', // purple
'intense_cyan' => '0;96', // cyan
'intense_white' => '0;97', // white
// bold high intensity
'intense_bold_black' => '1;90', // black
'intense_bold_red' => '1;91', // red
'intense_bold_green' => '1;92', // green
'intense_bold_yellow' => '1;93', // yellow
'intense_bold_blue' => '1;94', // blue
'intense_bold_purple' => '1;95', // purple
'intense_bold_cyan' => '1;96', // cyan
'intense_bold_white' => '1;97', // white
// high intensity backgrounds
'intense_background_black' => '0;100', // black
'intense_background_red' => '0;101', // red
'intense_background_green' => '0;102', // green
'intense_background_yellow' => '0;103', // yellow
'intense_background_blue' => '0;104', // blue
'intense_background_purple' => '0;105', // purple
'intense_background_cyan' => '0;106', // cyan
'intense_background_white' => '0;107', // white
);
function writeln($message, $stream = STDOUT)
{
$message .= PHP_EOL;
fwrite($stream, $message);
}
/**
* <message
* color="black"
* colorize="foreground|background"
* style="normal|bold|underline"
* intensity="normal|intense">
* <info>
* <error>
* <success>
* <warning>
*/
foreach ($colors as $name => $color) {
writeln("\033[{$color}m{$name}\033[0m");
}
@nickl-
Copy link

nickl- commented Jul 16, 2012

I've got something similar I was working on that we might be able to sync

My idea is

<?php

$cli->clear()->goto(30,30)->color('green')->style('bold')->print('ANSI colours are awesome!')->down(1)->back(25)->color('purple')->println('=========================');

I employed a similar method as you are with writeln but it is a custom() function and all the methods go through there changing the \033
I can't remember what state it is in I was last mapping 256 golour support and working through the escape codes to ensure completion.

Where were you going with this?

@henriquemoody
Copy link
Author

Just for fun!
I was working in a CLI script at Dafiti, much simple to use something more complex like Symfony Console and not too simple to use only shell script. Just one PHP file.
In the end I prefered to create a phar file. =]

@henriquemoody
Copy link
Author

Btw I think PHP should have a function like writeln, other languages, like Java, Ruby, Python, etc have one.

@nickl-
Copy link

nickl- commented Jul 17, 2012

Don't you think println is more appropriate for php?

@henriquemoody
Copy link
Author

Yes, you're right.

@nickl-
Copy link

nickl- commented Jul 17, 2012

You never had any doubts about that =)

So what are we going to do the complete ANSI cli solution as a Respect Project @augustohp @alganet your thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment