nur-sery/src/output/Console.php

435 lines
14 KiB
PHP
Raw Normal View History

2023-12-26 19:50:59 +04:00
<?php
namespace nur\sery\output;
use Exception;
use nulib\cl;
class Console implements IMessenger {
2023-12-26 20:01:41 +04:00
const INDENT = " ";
2023-12-26 19:50:59 +04:00
const VALID_LEVELS = [
self::LEVEL_DEBUG,
self::LEVEL_NORMAL,
self::LEVEL_MAJOR,
];
const LEVEL_MAP = [
"debug" => self::LEVEL_DEBUG,
"d" => self::LEVEL_DEBUG,
"normal" => self::LEVEL_NORMAL,
"n" => self::LEVEL_NORMAL,
"major" => self::LEVEL_MAJOR,
"m" => self::LEVEL_MAJOR,
];
2023-12-26 23:27:51 +04:00
protected static function verifix_level($level, bool $debug): int {
if ($level === null) $level = $debug? self::LEVEL_DEBUG: self::LEVEL_NORMAL;
if (!in_array($level, self::VALID_LEVELS, true)) {
$level = cl::get(self::LEVEL_MAP, $level, $level);
}
if (!in_array($level, self::VALID_LEVELS, true)) {
throw new Exception("$level: invalid level");
}
return $level;
}
const GENERIC_PREFIXES = [
2023-12-26 19:50:59 +04:00
self::LEVEL_MAJOR => [
2023-12-26 23:27:51 +04:00
"section" => [true, "SECTION!", "===", "<color @b>=", "=</color>", "==="],
"title" => [false, "TITLE!", null, "<color @b>T</color><color b>", "</color>", "---"],
"desc" => ["DESC!", "<color @b>></color>", ""],
"error" => ["CRITICAL!", "<color @r>E!", "</color>"],
"warn" => ["ATTENTION!", "<color @y>W!", "</color>"],
"note" => ["IMPORTANT!", "<color @g>N!", "</color>"],
"info" => ["IMPORTANT!", "<color @b>I!", "</color>"],
2023-12-27 00:34:59 +04:00
"step" => ["*", "<color @w>.</color>", ""],
2023-12-26 23:27:51 +04:00
"print" => [null, null, null],
2023-12-26 19:50:59 +04:00
],
self::LEVEL_NORMAL => [
2023-12-26 23:27:51 +04:00
"section" => [true, "SECTION:", "---", "<color @b>-", "-</color>", "---"],
"title" => [false, "TITLE:", null, "<color @b>T</color><color b>", "</color>", null],
"desc" => ["DESC:", "<color @b>></color>", ""],
"error" => ["ERROR:", "<color @r>E</color><color r>", "</color>"],
"warn" => ["WARN:", "<color @y>W</color><color y>", "</color>"],
"note" => ["NOTE:", "<color @g>N</color>", ""],
"info" => ["INFO:", "<color @b>I</color>", ""],
2023-12-27 00:34:59 +04:00
"step" => ["*", "<color @w>.</color>", ""],
2023-12-26 23:27:51 +04:00
"print" => [null, null, null],
2023-12-26 19:50:59 +04:00
],
self::LEVEL_DEBUG => [
2023-12-26 23:27:51 +04:00
"section" => [false, "section", null, "<color @w>>>", "<<</color>", null],
"title" => [false, "title", null, "<color b>t", "</color>", null],
"desc" => [">", "<color b>></color>", ""],
"error" => ["e", "<color r>e</color><color -r>", "</color>"],
"warn" => ["w", "<color y>w</color><color -y>", "</color>"],
"note" => ["i", "<color b>i</color>", ""],
"info" => ["D", "<color @w>D</color><color w>", "</color>"],
2023-12-27 00:34:59 +04:00
"step" => ["*", "<color @w>.</color>", ""],
2023-12-26 23:27:51 +04:00
"print" => [null, null, null],
2023-12-26 19:50:59 +04:00
],
];
const RESULT_PREFIXES = [
"failure" => ["(FAILURE)", "<color r>✘</color>"],
"success" => ["(SUCCESS)", "<color @g>✔</color>"],
"neutral" => [null, null],
];
function __construct(?array $params=null) {
2023-12-26 23:27:51 +04:00
$color = cl::get($params, "color");
2023-12-26 19:50:59 +04:00
$debug = boolval(cl::get($params, "debug"));
2023-12-26 23:27:51 +04:00
$minLevel = self::verifix_level(cl::get($params, "min_level"), $debug);
$defaultLevel = self::verifix_level(cl::get($params, "default_level"), false);
2023-12-26 19:50:59 +04:00
2023-12-26 23:27:51 +04:00
$params = [
"color" => $color,
"indent" => static::INDENT,
];
$this->out = new StdOutput(STDOUT, $params);
$this->err = new StdOutput(STDERR, $params);
2023-12-26 19:50:59 +04:00
$this->minLevel = intval($minLevel);
2023-12-26 23:27:51 +04:00
$this->defaultLevel = intval($defaultLevel);
2023-12-26 19:50:59 +04:00
$this->inSection = false;
2023-12-26 23:27:51 +04:00
$this->titles = [];
$this->title = null;
$this->actions = [];
$this->action = null;
2023-12-26 19:50:59 +04:00
}
/** @var StdOutput la sortie standard */
protected $out;
/** @var StdOutput la sortie d'erreur */
protected $err;
/** @var int level minimum que doivent avoir les messages pour être affichés */
protected $minLevel;
2023-12-26 23:27:51 +04:00
/** @var int level par défaut dans lequel les messages sont affichés */
protected $defaultLevel;
protected function checkLevel(?int &$level): bool {
if ($level === null) $level = $this->defaultLevel;
return $level >= $this->minLevel;
}
protected function _printTitle(int $level, string $type, $content, int $indentLevel, StdOutput $out): void {
$prefixes = self::GENERIC_PREFIXES[$level][$type];
if ($prefixes[0]) $out->print();
if ($out->isColor()) {
$before = $prefixes[2];
$prefix = $prefixes[3];
$prefix2 = $prefix !== null? "$prefix ": null;
$suffix = $prefixes[4];
$suffix2 = $suffix !== null? " $suffix": null;
$after = $prefixes[5];
$lines = $out->getLines(false, $content);
$maxlen = 0;
foreach ($lines as &$content) {
$line = $out->filterColors($content);
$len = strlen($line);
if ($len > $maxlen) $maxlen = $len;
$content = [$content, $len];
}; unset($content);
if ($before !== null) {
$out->iprint($indentLevel, $prefix, substr($before, 1), str_repeat($before[0], $maxlen), $suffix);
}
foreach ($lines as [$content, $len]) {
$padding = $len < $maxlen? str_repeat(" ", $maxlen - $len): null;
$out->iprint($indentLevel, $prefix2, $content, $padding, $suffix2);
}
if ($after !== null) {
$out->iprint($indentLevel, $prefix, substr($after, 1), str_repeat($after[0], $maxlen), $suffix);
}
} else {
$prefix = $prefixes[1];
if ($prefix !== null) $prefix .= " ";
$prefix2 = str_repeat(" ", strlen($prefix));
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
$out->iprint($indentLevel, $prefix, $content);
$prefix = $prefix2;
}
}
}
2023-12-27 00:34:59 +04:00
protected function _printAction(int $level,
bool $printContent, $content,
bool $printResult, ?bool $rsuccess, $rcontent,
int $indentLevel, StdOutput $out): void {
if ($rsuccess === true) $type = "success";
elseif ($rsuccess === false) $type = "failure";
else $type = "neutral";
$prefixes = self::RESULT_PREFIXES[$type];
$prefix = $out->isColor()? $prefixes[1]: $prefixes[0];
if ($prefix !== null) $prefix .= " ";
if ($printContent && $printResult) {
if ($rcontent) {
cl::ensure_array($content);
$content[] = ": ";
$content[] = $rcontent;
}
$out->iprint($indentLevel, $prefix, $content);
} elseif ($printContent) {
$prefixes = self::GENERIC_PREFIXES[$level]["step"];
if ($out->isColor()) {
$prefix = $prefixes[1];
$suffix = $prefixes[2];
} else {
$prefix = $prefixes[0];
$suffix = null;
}
if ($prefix !== null) $prefix .= " ";
$out->iprint($indentLevel, $prefix, $content, $suffix, ":");
} elseif ($printResult) {
if (!$rcontent) {
if ($rsuccess === true) $rcontent = "succès";
elseif ($rsuccess === false) $rcontent = "échec";
}
if ($rcontent) $out->iprint($indentLevel + 1, $prefix, $rcontent);
}
}
2023-12-26 23:27:51 +04:00
protected function _printGeneric(int $level, string $type, $content, int $indentLevel, StdOutput $out): void {
$prefixes = self::GENERIC_PREFIXES[$level][$type];
if ($out->isColor()) {
$prefix = $prefixes[1];
$suffix = $prefixes[2];
} else {
$prefix = $prefixes[0];
$suffix = null;
}
$line = [$prefix];
if ($prefix !== null) $line[] = " ";
$line[] = $content;
$line[] = $suffix;
$out->iprint($indentLevel, ...$line);
}
2023-12-26 20:01:41 +04:00
2023-12-26 19:50:59 +04:00
/** @var bool est-on dans une section? */
protected $inSection;
2023-12-26 23:27:51 +04:00
/** @var array section qui est en attente d'affichage */
2023-12-26 19:50:59 +04:00
protected $section;
2023-12-26 23:27:51 +04:00
function section($content, ?int $level=null): void {
2023-12-26 19:50:59 +04:00
$this->endSection();
$this->inSection = true;
2023-12-26 23:27:51 +04:00
if (!$this->checkLevel($level)) return;
$this->section = [
"level" => $level,
"content" => $content,
"print_content" => true,
];
2023-12-26 19:50:59 +04:00
}
protected function printSection() {
2023-12-26 23:27:51 +04:00
$section =& $this->section;
if ($section["print_content"]) {
$this->_printTitle($section["level"], "section", $section["content"], 0, $this->err);
$section["print_content"] = false;
2023-12-26 19:50:59 +04:00
}
}
protected function endSection(): void {
$this->inSection = false;
$this->section = null;
}
2023-12-27 00:34:59 +04:00
protected function getIndentLevel(bool $withActions=true): int {
$indentLevel = count($this->titles) - 1;
if ($indentLevel < 0) $indentLevel = 0;
if ($withActions) {
foreach ($this->actions as $action) {
if ($action["level"] < $this->minLevel) continue;
$indentLevel++;
}
}
return $indentLevel;
}
2023-12-26 19:50:59 +04:00
/** @var array */
protected $titles;
/** @var array */
2023-12-26 23:27:51 +04:00
protected $title;
2023-12-26 19:50:59 +04:00
2023-12-26 23:27:51 +04:00
function title($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
2023-12-26 19:50:59 +04:00
$this->titles[] = [
2023-12-26 23:27:51 +04:00
"level" => $level,
"content" => $content,
"print_content" => true,
2023-12-26 19:50:59 +04:00
"descs" => [],
2023-12-26 23:27:51 +04:00
"print_descs" => false,
2023-12-26 19:50:59 +04:00
];
2023-12-26 23:27:51 +04:00
$this->title =& $this->titles[count($this->titles) - 1];
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function desc($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
$title =& $this->title;
$title["descs"][] = [
"level" => $level,
"content" => $content,
];
$title["print_descs"] = true;
2023-12-26 19:50:59 +04:00
}
protected function printTitles(): void {
$this->printSection();
2023-12-27 00:34:59 +04:00
$err = $this->err;
2023-12-26 23:27:51 +04:00
$indentLevel = 0;
foreach ($this->titles as &$title) {
if ($title["print_content"]) {
2023-12-27 00:34:59 +04:00
$this->_printTitle($title["level"], "title", $title["content"], $indentLevel, $err);
2023-12-26 23:27:51 +04:00
$title["print_content"] = false;
}
if ($title["print_descs"]) {
foreach ($title["descs"] as $desc) {
2023-12-27 00:34:59 +04:00
$this->_printGeneric($desc["level"], "desc", $desc["content"], $indentLevel, $err);
2023-12-26 23:27:51 +04:00
}
$title["descs"] = [];
$title["print_descs"] = false;
}
$indentLevel++;
}; unset($title);
2023-12-26 19:50:59 +04:00
}
protected function endTitle(): void {
array_pop($this->titles);
if ($this->titles) {
2023-12-26 23:27:51 +04:00
$this->title =& $this->titles[count($this->titles) - 1];
2023-12-26 19:50:59 +04:00
} else {
2023-12-26 23:27:51 +04:00
$this->titles = [];
unset($this->title);
2023-12-26 19:50:59 +04:00
}
}
/** @var array */
protected $actions;
/** @var array */
2023-12-26 23:27:51 +04:00
protected $action;
function action($content, ?int $level=null): void {
2023-12-27 00:34:59 +04:00
$this->checkLevel($level);
2023-12-26 19:50:59 +04:00
$this->actions[] = [
"level" => $level,
2023-12-26 20:01:41 +04:00
"content" => $content,
"print_content" => true,
2023-12-27 00:34:59 +04:00
"result_success" => null,
"result_content" => null,
2023-12-26 19:50:59 +04:00
];
2023-12-26 23:27:51 +04:00
$this->action =& $this->actions[count($this->actions) - 1];
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function printActions(bool $willEnd=false): void {
2023-12-26 19:50:59 +04:00
$this->printTitles();
2023-12-27 00:34:59 +04:00
$err = $this->err;
$indentLevel = $this->getIndentLevel(false);
$lastIndex = count($this->actions) - 1;
$index = 0;
foreach ($this->actions as &$action) {
$mergeResult = $index++ == $lastIndex && $willEnd;
$level = $action["level"];
$content = $action["content"];
$printContent = $action["print_content"];
$rsuccess = $action["result_success"];
$rcontent = $action["result_content"];
if ($mergeResult) {
$this->_printAction($level, $printContent, $content, true, $rsuccess, $rcontent, $indentLevel, $err);
} elseif ($printContent) {
$this->_printAction($level, $printContent, $content, false, $rsuccess, $rcontent, $indentLevel, $err);
$action["print_content"] = false;
}
$indentLevel++;
}; unset($action);
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function step($content, ?int $level=null): void {
2023-12-27 00:34:59 +04:00
if (!$this->checkLevel($level)) return;
2023-12-26 20:01:41 +04:00
if (!$this->actions) $this->action(null);
2023-12-26 19:50:59 +04:00
$this->printActions();
2023-12-27 00:34:59 +04:00
$this->_printGeneric($level, "step", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
function success($content=null): void {
2023-12-26 20:01:41 +04:00
if (!$this->actions) $this->action(null);
2023-12-27 00:34:59 +04:00
$this->action["result_success"] = true;
$this->action["result_content"] = $content;
2023-12-26 23:27:51 +04:00
$this->printActions(true);
2023-12-26 19:50:59 +04:00
$this->endAction();
}
function failure($content=null): void {
2023-12-26 20:01:41 +04:00
if (!$this->actions) $this->action(null);
2023-12-27 00:34:59 +04:00
$this->action["result_success"] = false;
$this->action["result_content"] = $content;
2023-12-26 23:27:51 +04:00
$this->printActions(true);
2023-12-26 19:50:59 +04:00
$this->endAction();
}
function neutral($content=null): void {
2023-12-26 20:01:41 +04:00
if (!$this->actions) $this->action(null);
2023-12-27 00:34:59 +04:00
$this->action["result_success"] = null;
$this->action["result_content"] = $content;
2023-12-26 23:27:51 +04:00
$this->printActions(true);
2023-12-26 19:50:59 +04:00
$this->endAction();
}
protected function endAction(): void {
array_pop($this->actions);
if ($this->actions) {
2023-12-26 23:27:51 +04:00
$this->action =& $this->actions[count($this->actions) - 1];
2023-12-26 19:50:59 +04:00
} else {
2023-12-26 23:27:51 +04:00
$this->actions = [];
unset($this->action);
2023-12-26 19:50:59 +04:00
}
}
2023-12-26 23:27:51 +04:00
function print($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
2023-12-26 20:01:41 +04:00
$this->printActions();
2023-12-26 23:27:51 +04:00
$this->_printGeneric($level, "print", $content, $this->getIndentLevel(), $this->out);
2023-12-26 20:01:41 +04:00
}
2023-12-26 23:27:51 +04:00
function info($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
2023-12-26 19:50:59 +04:00
$this->printActions();
2023-12-26 23:27:51 +04:00
$this->_printGeneric($level, "info", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function note($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
2023-12-26 19:50:59 +04:00
$this->printActions();
2023-12-26 23:27:51 +04:00
$this->_printGeneric($level, "note", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function warn($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
2023-12-26 19:50:59 +04:00
$this->printActions();
2023-12-26 23:27:51 +04:00
$this->_printGeneric($level, "warn", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function error($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
2023-12-26 19:50:59 +04:00
$this->printActions();
2023-12-26 23:27:51 +04:00
$this->_printGeneric($level, "error", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
function end(bool $all=false) {
if ($all) {
while ($this->actions) $this->neutral();
while ($this->titles) $this->endTitle();
$this->endSection();
} elseif ($this->actions) {
$this->endAction();
} elseif ($this->titles) {
$this->endTitle();
} else {
$this->endSection();
}
}
}