modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-27 00:34:59 +04:00
parent 029a16f6de
commit 0d6971f377
2 changed files with 106 additions and 24 deletions

View File

@ -41,6 +41,7 @@ class Console implements IMessenger {
"warn" => ["ATTENTION!", "<color @y>W!", "</color>"],
"note" => ["IMPORTANT!", "<color @g>N!", "</color>"],
"info" => ["IMPORTANT!", "<color @b>I!", "</color>"],
"step" => ["*", "<color @w>.</color>", ""],
"print" => [null, null, null],
],
self::LEVEL_NORMAL => [
@ -51,6 +52,7 @@ class Console implements IMessenger {
"warn" => ["WARN:", "<color @y>W</color><color y>", "</color>"],
"note" => ["NOTE:", "<color @g>N</color>", ""],
"info" => ["INFO:", "<color @b>I</color>", ""],
"step" => ["*", "<color @w>.</color>", ""],
"print" => [null, null, null],
],
self::LEVEL_DEBUG => [
@ -61,12 +63,12 @@ class Console implements IMessenger {
"warn" => ["w", "<color y>w</color><color -y>", "</color>"],
"note" => ["i", "<color b>i</color>", ""],
"info" => ["D", "<color @w>D</color><color w>", "</color>"],
"step" => ["*", "<color @w>.</color>", ""],
"print" => [null, null, null],
],
];
const RESULT_PREFIXES = [
"step" => ["*", "<color @w>.</color>"],
"failure" => ["(FAILURE)", "<color r>✘</color>"],
"success" => ["(SUCCESS)", "<color @g>✔</color>"],
"neutral" => [null, null],
@ -151,6 +153,43 @@ class Console implements IMessenger {
}
}
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);
}
}
protected function _printGeneric(int $level, string $type, $content, int $indentLevel, StdOutput $out): void {
$prefixes = self::GENERIC_PREFIXES[$level][$type];
if ($out->isColor()) {
@ -197,6 +236,18 @@ class Console implements IMessenger {
$this->section = null;
}
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;
}
/** @var array */
protected $titles;
@ -227,16 +278,16 @@ class Console implements IMessenger {
protected function printTitles(): void {
$this->printSection();
$out = $this->err;
$err = $this->err;
$indentLevel = 0;
foreach ($this->titles as &$title) {
if ($title["print_content"]) {
$this->_printTitle($title["level"], "title", $title["content"], $indentLevel, $out);
$this->_printTitle($title["level"], "title", $title["content"], $indentLevel, $err);
$title["print_content"] = false;
}
if ($title["print_descs"]) {
foreach ($title["descs"] as $desc) {
$this->_printGeneric($desc["level"], "desc", $desc["content"], $indentLevel, $out);
$this->_printGeneric($desc["level"], "desc", $desc["content"], $indentLevel, $err);
}
$title["descs"] = [];
$title["print_descs"] = false;
@ -261,58 +312,68 @@ class Console implements IMessenger {
/** @var array */
protected $action;
protected function getIndentLevel(): int {
$indentLevel = count($this->titles) - 1;
if ($indentLevel < 0) $indentLevel = 0;
foreach ($this->actions as $action) {
if ($action["level"] < $this->minLevel) continue;
$indentLevel++;
}
return $indentLevel;
}
function action($content, ?int $level=null): void {
$this->checkLevel($level);
$this->actions[] = [
"level" => $level,
"content" => $content,
"print_content" => true,
"success" => null,
"result" => null,
"print_result" => true,
"result_success" => null,
"result_content" => null,
];
$this->action =& $this->actions[count($this->actions) - 1];
}
function printActions(bool $willEnd=false): void {
$this->printTitles();
$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);
}
function step($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
if (!$this->actions) $this->action(null);
$this->printActions();
$this->_printGeneric($level, "step", $content, $this->getIndentLevel(), $this->err);
}
function success($content=null): void {
if (!$this->actions) $this->action(null);
$this->action["success"] = true;
$this->action["result"] = $content;
$this->action["result_success"] = true;
$this->action["result_content"] = $content;
$this->printActions(true);
$this->endAction();
}
function failure($content=null): void {
if (!$this->actions) $this->action(null);
$this->action["success"] = false;
$this->action["result"] = $content;
$this->action["result_success"] = false;
$this->action["result_content"] = $content;
$this->printActions(true);
$this->endAction();
}
function neutral($content=null): void {
if (!$this->actions) $this->action(null);
$this->action["success"] = null;
$this->action["result"] = $content;
$this->action["result_success"] = null;
$this->action["result_content"] = $content;
$this->printActions(true);
$this->endAction();
}

View File

@ -61,6 +61,27 @@ $c->success("action2 success");
$c->success("action1 success");
$c->success("action0 success");
$c->action("action");
$c->step("step");
$c->success();
$c->action("action");
$c->step("step");
$c->failure();
$c->action("action");
$c->success();
$c->action("action");
$c->failure();
$c->action("action0");
$c->action("action1");
$c->action("action2");
$c->success();
$c->success();
$c->success();
$c->info("info");
$c->note("note");
$c->warn("warn");