Compare commits

...

9 Commits

6 changed files with 142 additions and 12 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 museum-digital
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# Basic output formatting for CLI applications at museum-digital
## License
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for the full license text.

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();
}