Skip to content

Instantly share code, notes, and snippets.

@callumacrae
Created November 22, 2011 18:19
Show Gist options
  • Select an option

  • Save callumacrae/1386422 to your computer and use it in GitHub Desktop.

Select an option

Save callumacrae/1386422 to your computer and use it in GitHub Desktop.
A static class to format strings for CLI.
<?php
class FormatStringCLI
{
private static $fg_colors = array(
'black' => '0;30',
'dark_grey' => '1;30',
'blue' => '0;34',
'light_blue' => '1;34',
'green' => '0;32',
'light_green' => '1;32',
'cyan' => '0;36',
'light_cyan' => '1;36',
'red' => '0;31',
'light_red' => '1;31',
'purple' => '0;35',
'light_purple' => '1;35',
'brown' => '0;33',
'yellow' => '1;33',
'light_grey' => '0;37',
'white' => '1;37',
);
private static $bg_colors = array(
'black' => '40',
'red' => '41',
'green' => '42',
'yellow' => '43',
'blue' => '44',
'magenta' => '45',
'cyan' => '46',
'light_grey' => '47',
);
public static function color($in, $fg_color = false, $bg_color = false)
{
$out = '';
if ($fg_color && isset(self::$fg_colors[$fg_color]))
{
$out .= chr(27) . '[' . self::$fg_colors[$fg_color] . 'm';
}
if ($bg_color && isset(self::$bg_colors[$bg_color]))
{
$out .= chr(27) . '[' . self::$bg_colors[$bg_color] . 'm';
}
return $out . $in . chr(27) . '[0m';
}
public static function bold($in)
{
return chr(27) . '[1m' . $in . chr(27) . '[0m';
}
public static function underline($in)
{
return chr(27) . '[4m' . $in . chr(27) . '[0m';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment