Skip to content

Commit

Permalink
Add raw param for colorized output
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Feb 27, 2024
1 parent 91672e4 commit def81e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ class Color
* @param string $string
* @param ?int $fg
* @param ?int $bg
* @param bool $raw
* @return string
*/
public static function colorize(string $string, ?int $fg = null, ?int $bg = null): string
public static function colorize(string $string, ?int $fg = null, ?int $bg = null, bool $raw = false): string
{
if (stripos(PHP_OS, 'win') === false) {
if ((stripos(PHP_OS, 'win') === false) && (!$raw)) {
return static::getFgColorCode($fg) . static::getBgColorCode($bg) . $string . "\x1b[0m";
} else {
return $string;
Expand Down
17 changes: 9 additions & 8 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,15 @@ public function addCommandsFromRoutes(Cli $routeMatch, ?string $scriptName = nul
* Get a help
*
* @param ?string $command
* @param bool $raw
* @return string|null
*/
public function help(?string $command = null): string|null
public function help(?string $command = null, bool $raw = false): string|null
{
if ($command !== null) {
return $this->commands[$command]?->getHelp();
} else {
$this->displayHelp();
$this->displayHelp($raw);
return null;
}
}
Expand Down Expand Up @@ -1183,7 +1184,7 @@ public function send(bool $withHeaders = true): Console
*
* @return void
*/
public function displayHelp(): void
public function displayHelp(bool $raw = false): void
{
$this->response = null;
$commands = [];
Expand All @@ -1203,14 +1204,14 @@ public function displayHelp(): void
$name1 = substr($name, 0, strpos($name, ' '));
$name2 = substr($name, strpos($name, ' ') + 1);
if (isset($this->helpColors[0])) {
$name1 = Color::colorize($name1, $this->helpColors[0]);
$name1 = Color::colorize($name1, $this->helpColors[0], null, $raw);
}
if (isset($this->helpColors[1])) {
$name2 = Color::colorize($name2, $this->helpColors[1]);
$name2 = Color::colorize($name2, $this->helpColors[1], null, $raw);
}
$name = $name1 . ' ' . $name2;
} else if (isset($this->helpColors[0])){
$name = Color::colorize($name, $this->helpColors[0]);
$name = Color::colorize($name, $this->helpColors[0], null, $raw);
}
}

Expand All @@ -1227,12 +1228,12 @@ public function displayHelp(): void
$colorIndex = 3;
}
$name .= ' ' . ((isset($this->helpColors[$colorIndex])) ?
Color::colorize($p, $this->helpColors[$colorIndex]) : $p);
Color::colorize($p, $this->helpColors[$colorIndex], null, $raw) : $p);
}
}
} else {
$name .= ' ' . ((isset($this->helpColors[2])) ?
Color::colorize($params, $this->helpColors[2]) : $params);
Color::colorize($params, $this->helpColors[2], null, $raw) : $params);
}
}

Expand Down

0 comments on commit def81e1

Please sign in to comment.