You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.5 KiB
105 lines
2.5 KiB
<?php
|
|
|
|
/**
|
|
* ANSI Colors for the Terminal Console
|
|
*
|
|
* PHP version 8.3
|
|
*
|
|
* @category Util
|
|
* @package Neato
|
|
* @author Robert S. <tips@technowizardbob.com>
|
|
* @license https://mit-license.org/ MIT License
|
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy
|
|
*/
|
|
|
|
/**
|
|
* Function getTermColors make ANSI color codes
|
|
*
|
|
* @param array|string $input text to display
|
|
* @param mixed $options colors to use
|
|
*
|
|
* @return string ANSI text
|
|
*/
|
|
function getTermColors(array|string $input, $options): string
|
|
{
|
|
|
|
$colored_string = "";
|
|
|
|
$styles = [
|
|
'normal' => '0', // reset
|
|
'bold' => '1',
|
|
'dim' => '2',
|
|
'underlined' => '4',
|
|
'blinking' => '5'
|
|
];
|
|
|
|
$fg_colors = [
|
|
'black' => '0;30',
|
|
'dark_gray' => '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_gray' => '0;37',
|
|
'white' => '1;37'
|
|
];
|
|
|
|
$bg_colors = [
|
|
'black' => '40',
|
|
'red' => '41',
|
|
'green' => '42',
|
|
'yellow' => '43',
|
|
'blue' => '44',
|
|
'magenta' => '45',
|
|
'cyan' => '46',
|
|
'light_gray' => '47'
|
|
];
|
|
|
|
$style = (isset($options['style'])) ? strtolower($options['style']) : '';
|
|
$color = (isset($options['color'])) ? strtolower($options['color']) : '';
|
|
$fg_color = (isset($options['fg_color'])) ? strtolower($options['fg_color']) : $color;
|
|
$bg_color = (isset($options['bg_color'])) ? strtolower($options['bg_color']) : '';
|
|
|
|
if ($style !== '' && isset($styles[$style])) {
|
|
$colored_string .= "\033[" . $styles[$style] . "m";
|
|
}
|
|
if ($fg_color !== '' && isset($fg_colors[$fg_color])) {
|
|
$colored_string .= "\033[" . $fg_colors[$fg_color] . "m";
|
|
}
|
|
if ($bg_color !== '' && isset($bg_colors[$bg_color])) {
|
|
$colored_string .= "\033[" . $bg_colors[$bg_color] . "m";
|
|
}
|
|
|
|
$str = '';
|
|
if (is_array($input)) {
|
|
foreach ($input as $s) {
|
|
$str .= $s . PHP_EOL;
|
|
}
|
|
} else {
|
|
$str = $input;
|
|
}
|
|
|
|
$colored_string .= $str . "\033[0m";
|
|
return $colored_string;
|
|
}
|
|
|
|
/**
|
|
* Alias to getTermColors
|
|
*
|
|
* @param array|string $data text to display
|
|
* @param mixed $a options for colors
|
|
*
|
|
* @return string of ANSI
|
|
*/
|
|
function ANSI(array|string $data, $a): string
|
|
{
|
|
return getTermColors($data, $a);
|
|
}
|
|
|