Compare commits

...

7 Commits

4 changed files with 116 additions and 12 deletions

View File

@ -11,8 +11,8 @@ declare(strict_types = 1);
*/
final class MDConsole {
const CONSOLE_LOG_USED_RESOURCES_ENABLED = 1;
const CONSOLE_LOG_USED_RESOURCES_DISABLED = 0;
public const CONSOLE_LOG_USED_RESOURCES_ENABLED = 1;
public const CONSOLE_LOG_USED_RESOURCES_DISABLED = 0;
/** @var boolean */
public static $verbose = true;

48
src/MDConsoleColors.php Normal file
View File

@ -0,0 +1,48 @@
<?PHP
/**
* Constants for styling console outputs.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Constants for styling console outputs.
*/
final class MDConsoleColors {
public const ESC = "\033";
public const BLINK_ON = self::ESC . '[5m';
public const BOLD_ON = self::ESC . '[1m';
public const DIM_ON = self::ESC . '[2m';
public const REVERSE_ON = self::ESC . '[7m';
public const ITALIC_ON = self::ESC . '[3m';
public const ITALIC_OFF = self::ESC . '[23m';
public const HIGHLIGHT_ON = self::ESC . '[7m';
public const HIGHLIGHT_OFF = self::ESC . '[27m';
public const UNDERLINE_ON = self::ESC . '[4m';
public const UNDERLINE_OFF = self::ESC . '[24m';
public const RESET_STYLE = self::ESC . '(B' . self::ESC . '[m';
public const FG_BLACK = self::ESC . '[30m';
public const FG_RED = self::ESC . '[31m';
public const FG_GREEN = self::ESC . '[32m';
public const FG_YELLOW = self::ESC . '[33m';
public const FG_BLUE = self::ESC . '[34m';
public const FG_MAGENTA = self::ESC . '[35m';
public const FG_AQUA = self::ESC . '[36m';
public const FG_GREY = self::ESC . '[37m';
public const BG_BLACK = self::ESC . '[40m';
public const BG_RED = self::ESC . '[41m';
public const BG_GREEN = self::ESC . '[42m';
public const BG_YELLOW = self::ESC . '[43m';
public const BG_BLUE = self::ESC . '[44m';
public const BG_MAGENTA = self::ESC . '[45m';
public const BG_AQUA = self::ESC . '[46m';
public const BG_GREY = self::ESC . '[47m';
public const COLOR_OFF = self::ESC . '[0m';
}

View File

@ -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",
};
}
}

View File

@ -11,7 +11,9 @@ declare(strict_types = 1);
*/
final class MDOutputHandler {
const FLUSH_TO_LOGFILE_AFTER = 100;
private const FLUSH_TO_LOGFILE_AFTER = 100;
public static bool $disable_completely = false;
/** @var integer */
private int $_verbosity = 0;
@ -33,6 +35,22 @@ final class MDOutputHandler {
/** @var boolean */
private bool $_logfile_status_checked = false;
public bool $color_output = false;
/**
* Simple echo that is silent during $disable_completely.
*
* @param string $input Input text.
*
* @return void
*/
public static function echo(string $input):void {
if (self::$disable_completely === true) return;
echo $input;
}
/**
* Writes log file contents to log file.
*
@ -126,23 +144,23 @@ final class MDOutputHandler {
*/
public function toLog(string $msg, MDConsoleStatus $statusCode = MDConsoleStatus::NOTICE):void {
if (self::$disable_completely === true) return;
// Get parts for generating the message to log.
++$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%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;
@ -184,6 +202,10 @@ final class MDOutputHandler {
*/
public function __destruct() {
if (self::$disable_completely === true) return;
if ($this->_verbosity < 0) return;
if (!empty($this->_log_queue)) {
$this->flush_to_logfile();
}