modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-27 01:39:08 +04:00
parent 0d6971f377
commit 64fd232f71
4 changed files with 100 additions and 30 deletions

View File

@ -4,7 +4,7 @@ namespace nur\sery\output;
use Exception; use Exception;
use nulib\cl; use nulib\cl;
class Console implements IMessenger { class StdMessenger implements IMessenger {
const INDENT = " "; const INDENT = " ";
const VALID_LEVELS = [ const VALID_LEVELS = [
@ -35,18 +35,18 @@ class Console implements IMessenger {
const GENERIC_PREFIXES = [ const GENERIC_PREFIXES = [
self::LEVEL_MAJOR => [ self::LEVEL_MAJOR => [
"section" => [true, "SECTION!", "===", "<color @b>=", "=</color>", "==="], "section" => [true, "SECTION!", "===", "<color @b>=", "=</color>", "==="],
"title" => [false, "TITLE!", null, "<color @b>T</color><color b>", "</color>", "---"], "title" => [false, "TITLE!", null, "<color @b>T</color><color b>", "</color>", "==="],
"desc" => ["DESC!", "<color @b>></color>", ""], "desc" => ["DESC!", "<color @b>></color>", ""],
"error" => ["CRITICAL!", "<color @r>E!", "</color>"], "error" => ["CRITICAL!", "<color @r>E!", "</color>"],
"warn" => ["ATTENTION!", "<color @y>W!", "</color>"], "warn" => ["WARNING!", "<color @y>W!", "</color>"],
"note" => ["IMPORTANT!", "<color @g>N!", "</color>"], "note" => ["ATTENTION!", "<color @g>N!", "</color>"],
"info" => ["IMPORTANT!", "<color @b>I!", "</color>"], "info" => ["IMPORTANT!", "<color @b>I!", "</color>"],
"step" => ["*", "<color @w>.</color>", ""], "step" => ["*", "<color @w>.</color>", ""],
"print" => [null, null, null], "print" => [null, null, null],
], ],
self::LEVEL_NORMAL => [ self::LEVEL_NORMAL => [
"section" => [true, "SECTION:", "---", "<color @b>-", "-</color>", "---"], "section" => [true, "SECTION:", "---", "<color @b>-", "-</color>", "---"],
"title" => [false, "TITLE:", null, "<color @b>T</color><color b>", "</color>", null], "title" => [false, "TITLE:", null, "<color @b>T</color><color b>", "</color>", "---"],
"desc" => ["DESC:", "<color @b>></color>", ""], "desc" => ["DESC:", "<color @b>></color>", ""],
"error" => ["ERROR:", "<color @r>E</color><color r>", "</color>"], "error" => ["ERROR:", "<color @r>E</color><color r>", "</color>"],
"warn" => ["WARN:", "<color @y>W</color><color y>", "</color>"], "warn" => ["WARN:", "<color @y>W</color><color y>", "</color>"],
@ -56,7 +56,7 @@ class Console implements IMessenger {
"print" => [null, null, null], "print" => [null, null, null],
], ],
self::LEVEL_DEBUG => [ self::LEVEL_DEBUG => [
"section" => [false, "section", null, "<color @w>>>", "<<</color>", null], "section" => [true, "section", null, "<color @w>>>", "<<</color>", null],
"title" => [false, "title", null, "<color b>t", "</color>", null], "title" => [false, "title", null, "<color b>t", "</color>", null],
"desc" => [">", "<color b>></color>", ""], "desc" => [">", "<color b>></color>", ""],
"error" => ["e", "<color r>e</color><color -r>", "</color>"], "error" => ["e", "<color r>e</color><color -r>", "</color>"],
@ -75,6 +75,7 @@ class Console implements IMessenger {
]; ];
function __construct(?array $params=null) { function __construct(?array $params=null) {
$output = cl::get($params, "output");
$color = cl::get($params, "color"); $color = cl::get($params, "color");
$debug = boolval(cl::get($params, "debug")); $debug = boolval(cl::get($params, "debug"));
$minLevel = self::verifix_level(cl::get($params, "min_level"), $debug); $minLevel = self::verifix_level(cl::get($params, "min_level"), $debug);
@ -84,10 +85,14 @@ class Console implements IMessenger {
"color" => $color, "color" => $color,
"indent" => static::INDENT, "indent" => static::INDENT,
]; ];
$this->out = new StdOutput(STDOUT, $params); if ($output !== null) {
$this->err = new StdOutput(STDERR, $params); $this->err = $this->out = new StdOutput($output, $params);
$this->minLevel = intval($minLevel); } else {
$this->defaultLevel = intval($defaultLevel); $this->out = new StdOutput(STDOUT, $params);
$this->err = new StdOutput(STDERR, $params);
}
$this->minLevel = $minLevel;
$this->defaultLevel = $defaultLevel;
$this->inSection = false; $this->inSection = false;
$this->titles = []; $this->titles = [];
$this->title = null; $this->title = null;
@ -127,7 +132,7 @@ class Console implements IMessenger {
$maxlen = 0; $maxlen = 0;
foreach ($lines as &$content) { foreach ($lines as &$content) {
$line = $out->filterColors($content); $line = $out->filterColors($content);
$len = strlen($line); $len = mb_strlen($line);
if ($len > $maxlen) $maxlen = $len; if ($len > $maxlen) $maxlen = $len;
$content = [$content, $len]; $content = [$content, $len];
}; unset($content); }; unset($content);
@ -144,7 +149,7 @@ class Console implements IMessenger {
} else { } else {
$prefix = $prefixes[1]; $prefix = $prefixes[1];
if ($prefix !== null) $prefix .= " "; if ($prefix !== null) $prefix .= " ";
$prefix2 = str_repeat(" ", strlen($prefix)); $prefix2 = str_repeat(" ", mb_strlen($prefix));
$lines = $out->getLines(false, $content); $lines = $out->getLines(false, $content);
foreach ($lines as $content) { foreach ($lines as $content) {
$out->iprint($indentLevel, $prefix, $content); $out->iprint($indentLevel, $prefix, $content);
@ -157,36 +162,66 @@ class Console implements IMessenger {
bool $printContent, $content, bool $printContent, $content,
bool $printResult, ?bool $rsuccess, $rcontent, bool $printResult, ?bool $rsuccess, $rcontent,
int $indentLevel, StdOutput $out): void { int $indentLevel, StdOutput $out): void {
$color = $out->isColor();
if ($rsuccess === true) $type = "success"; if ($rsuccess === true) $type = "success";
elseif ($rsuccess === false) $type = "failure"; elseif ($rsuccess === false) $type = "failure";
else $type = "neutral"; else $type = "neutral";
$prefixes = self::RESULT_PREFIXES[$type]; $rprefixes = self::RESULT_PREFIXES[$type];
$prefix = $out->isColor()? $prefixes[1]: $prefixes[0]; if ($color) {
if ($prefix !== null) $prefix .= " "; $rprefix = $rprefixes[1];
$rprefix2 = null;
if ($rprefix !== null) {
$rprefix .= " ";
$rprefix2 = $out->filterColors($out->filterContent($rprefix));
$rprefix2 = str_repeat(" ", mb_strlen($rprefix2));
}
} else {
$rprefix = $rprefixes[0];
if ($rprefix !== null) $rprefix .= " ";
$rprefix2 = str_repeat(" ", mb_strlen($rprefix));
}
if ($printContent && $printResult) { if ($printContent && $printResult) {
if ($rcontent) { if ($rcontent) {
cl::ensure_array($content); cl::ensure_array($content);
$content[] = ": "; $content[] = ": ";
$content[] = $rcontent; $content[] = $rcontent;
} }
$out->iprint($indentLevel, $prefix, $content); $lines = $out->getLines(false, $content);
foreach ($lines as $content) {
$out->iprint($indentLevel, $rprefix, $content);
$rprefix = $rprefix2;
}
} elseif ($printContent) { } elseif ($printContent) {
$prefixes = self::GENERIC_PREFIXES[$level]["step"]; $prefixes = self::GENERIC_PREFIXES[$level]["step"];
if ($out->isColor()) { if ($color) {
$prefix = $prefixes[1]; $prefix = $prefixes[1];
if ($prefix !== null) $prefix .= " ";
$prefix2 = $out->filterColors($out->filterContent($prefix));
$prefix2 = str_repeat(" ", mb_strlen($prefix2));
$suffix = $prefixes[2]; $suffix = $prefixes[2];
} else { } else {
$prefix = $prefixes[0]; $prefix = $prefixes[0];
if ($prefix !== null) $prefix .= " ";
$prefix2 = str_repeat(" ", mb_strlen($prefix));
$suffix = null; $suffix = null;
} }
if ($prefix !== null) $prefix .= " "; $lines = $out->getLines(false, $content, ":");
$out->iprint($indentLevel, $prefix, $content, $suffix, ":"); foreach ($lines as $content) {
$out->iprint($indentLevel, $prefix, $content, $suffix);
$prefix = $prefix2;
}
} elseif ($printResult) { } elseif ($printResult) {
if (!$rcontent) { if (!$rcontent && $color) {
if ($rsuccess === true) $rcontent = "succès"; if ($rsuccess === true) $rcontent = "succès";
elseif ($rsuccess === false) $rcontent = "échec"; elseif ($rsuccess === false) $rcontent = "échec";
} }
if ($rcontent) $out->iprint($indentLevel + 1, $prefix, $rcontent); $rprefix = " $rprefix";
$rprefix2 = " $rprefix2";
$lines = $out->getLines(false, $rcontent);
foreach ($lines as $rcontent) {
$out->iprint($indentLevel, $rprefix, $rcontent);
$rprefix = $rprefix2;
}
} }
} }
@ -194,16 +229,28 @@ class Console implements IMessenger {
$prefixes = self::GENERIC_PREFIXES[$level][$type]; $prefixes = self::GENERIC_PREFIXES[$level][$type];
if ($out->isColor()) { if ($out->isColor()) {
$prefix = $prefixes[1]; $prefix = $prefixes[1];
$prefix2 = null;
if ($prefix !== null) {
$prefix .= " ";
$prefix2 = $out->filterColors($out->filterContent($prefix));
$prefix2 = str_repeat(" ", mb_strlen($prefix2));
}
$suffix = $prefixes[2]; $suffix = $prefixes[2];
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
$out->iprint($indentLevel, $prefix, $content, $suffix);
$prefix = $prefix2;
}
} else { } else {
$prefix = $prefixes[0]; $prefix = $prefixes[0];
$suffix = null; if ($prefix !== null) $prefix .= " ";
$prefix2 = str_repeat(" ", mb_strlen($prefix));
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
$out->iprint($indentLevel, $prefix, $content);
$prefix = $prefix2;
}
} }
$line = [$prefix];
if ($prefix !== null) $line[] = " ";
$line[] = $content;
$line[] = $suffix;
$out->iprint($indentLevel, ...$line);
} }
/** @var bool est-on dans une section? */ /** @var bool est-on dans une section? */

View File

@ -149,7 +149,7 @@ class StdOutput {
$text .= "m"; $text .= "m";
return $text; return $text;
} }
protected function filterContent(string $text): string { function filterContent(string $text): string {
# couleur au début # couleur au début
$text = preg_replace_callback('/<color([^>]*)>/', [self::class, "replace_colors"], $text); $text = preg_replace_callback('/<color([^>]*)>/', [self::class, "replace_colors"], $text);
# reset à la fin # reset à la fin

View File

@ -1,5 +1,15 @@
# IMessenger # IMessenger
## TOOD
* [ ] support `add_date` et `date_format`
* [ ] affichage des exceptions en message technique.
si pas de message, prendre le message de l'exception par défaut
* [ ] possibilité de paramétrer le nom du fichier destination pour faire une
rotation des logs
## Principe
* 3 niveaux: DEBUG, NORMAL, MAJOR * 3 niveaux: DEBUG, NORMAL, MAJOR
* plusieurs types de messages: * plusieurs types de messages:
* section: flush tous les messages en cours, ne peut pas être imbriqué * section: flush tous les messages en cours, ne peut pas être imbriqué

View File

@ -2,7 +2,7 @@
<?php <?php
require(__DIR__.'/../vendor/autoload.php'); require(__DIR__.'/../vendor/autoload.php');
use nur\sery\output\Console; use nur\sery\output\StdMessenger;
$params = []; $params = [];
$count = count($argv) - 1; $count = count($argv) - 1;
@ -20,6 +20,10 @@ for ($i = 1; $i <= $count; $i++) {
case "+d": case "+d":
$params["debug"] = false; $params["debug"] = false;
break; break;
case "-L":
$i++;
$params["output"] = $argv[$i];
break;
case "-D": case "-D":
$params["default_level"] = "debug"; $params["default_level"] = "debug";
$params["debug"] = true; $params["debug"] = true;
@ -32,7 +36,7 @@ for ($i = 1; $i <= $count; $i++) {
break; break;
} }
} }
$c = new Console($params); $c = new StdMessenger($params);
$c->section("section"); $c->section("section");
@ -102,4 +106,13 @@ $c->section("multi-line\nsection");
$c->title("multi-line\ntitle"); $c->title("multi-line\ntitle");
$c->title("another\ntitle"); $c->title("another\ntitle");
$c->print("multi-line\nprint"); $c->print("multi-line\nprint");
$c->info("multi-line\ninfo");
$c->action("multi-line\naction");
$c->success();
$c->action("multi-line\naction");
$c->step("multi-line\nstep");
$c->failure();
$c->action("multi-line\naction");
$c->step("multi-line\nstep");
$c->success("multi-line\nsuccess");
$c->end(true); $c->end(true);