Add option to colorize CLI output via MDOutputHandler
This commit is contained in:
parent
104d86a762
commit
e4dfaf5d3f
@ -29,4 +29,38 @@ enum MDConsoleStatus {
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color for a CLI command.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_cli_color():string {
|
||||
|
||||
return match($this) {
|
||||
self::NOTICE => MDConsoleColors::COLOR_OFF,
|
||||
self::ERROR => MDConsoleColors::BG_RED,
|
||||
self::UPDATE => MDConsoleColors::BG_GREEN,
|
||||
self::DELETION => MDConsoleColors::BG_BLUE,
|
||||
self::SKIPPED => MDConsoleColors::BG_GREY,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the symbol for each status.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_symbol():string {
|
||||
|
||||
return match($this) {
|
||||
self::SKIPPED => "-",
|
||||
self::UPDATE => "U",
|
||||
self::ERROR => "E",
|
||||
self::DELETION => "D",
|
||||
self::NOTICE => "N",
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,8 @@ final class MDOutputHandler {
|
||||
/** @var boolean */
|
||||
private bool $_logfile_status_checked = false;
|
||||
|
||||
public bool $color_output = false;
|
||||
|
||||
/**
|
||||
* Simple echo that is silent during $disable_completely.
|
||||
*
|
||||
@ -148,19 +150,17 @@ final class MDOutputHandler {
|
||||
++$this->_counter; // Increment counter
|
||||
$date = \date("Y-m-d H:i:s"); // Get current date & time.
|
||||
|
||||
$statusSymbol = match($statusCode) {
|
||||
MDConsoleStatus::SKIPPED => "-",
|
||||
MDConsoleStatus::UPDATE => "U",
|
||||
MDConsoleStatus::ERROR => "E",
|
||||
MDConsoleStatus::DELETION => "D",
|
||||
MDConsoleStatus::NOTICE => "N",
|
||||
};
|
||||
$statusSymbol = $statusCode->get_symbol();
|
||||
|
||||
if ($this->_context !== "") $contextStr = "{$this->_context} │ ";
|
||||
if ($this->_context !== "") $contextStr = " │ ";
|
||||
else $contextStr = "";
|
||||
|
||||
// Generate the message to log.
|
||||
$message = "$statusSymbol │ {$contextStr}#" . sprintf("%1$07s", $this->_counter) . " │ " . \sprintf("%1$7s", $this->_human_filesize(\memory_get_usage())) . " │ {$date} │ {$msg}" . PHP_EOL;
|
||||
$format = match($this->color_output) {
|
||||
true => $statusCode->get_cli_color() . "%s" . MDConsoleColors::COLOR_OFF . " │ " . MDConsoleColors::FG_AQUA . "%s" . MDConsoleColors::FG_GREY . "%s#%'.08d" . MDConsoleColors::COLOR_OFF . " │ " . MDConsoleColors::FG_GREY . "%s │ %s" . MDConsoleColors::COLOR_OFF . " │ %s",
|
||||
default => "%s │ %s#%'.08d │ %s │ %s │ %s",
|
||||
};
|
||||
$message = \sprintf($format . PHP_EOL, $statusSymbol, $this->_context, $contextStr, $this->_counter, $this->_human_filesize(\memory_get_usage()), $date, $msg);
|
||||
|
||||
if ($this->_file_logging === true) {
|
||||
$this->_log_queue[] = $message;
|
||||
|
Loading…
x
Reference in New Issue
Block a user