modifs.mineures sans commentaires
This commit is contained in:
parent
0d6971f377
commit
64fd232f71
|
@ -4,7 +4,7 @@ namespace nur\sery\output;
|
|||
use Exception;
|
||||
use nulib\cl;
|
||||
|
||||
class Console implements IMessenger {
|
||||
class StdMessenger implements IMessenger {
|
||||
const INDENT = " ";
|
||||
|
||||
const VALID_LEVELS = [
|
||||
|
@ -35,18 +35,18 @@ class Console implements IMessenger {
|
|||
const GENERIC_PREFIXES = [
|
||||
self::LEVEL_MAJOR => [
|
||||
"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>", ""],
|
||||
"error" => ["CRITICAL!", "<color @r>E!", "</color>"],
|
||||
"warn" => ["ATTENTION!", "<color @y>W!", "</color>"],
|
||||
"note" => ["IMPORTANT!", "<color @g>N!", "</color>"],
|
||||
"warn" => ["WARNING!", "<color @y>W!", "</color>"],
|
||||
"note" => ["ATTENTION!", "<color @g>N!", "</color>"],
|
||||
"info" => ["IMPORTANT!", "<color @b>I!", "</color>"],
|
||||
"step" => ["*", "<color @w>.</color>", ""],
|
||||
"print" => [null, null, null],
|
||||
],
|
||||
self::LEVEL_NORMAL => [
|
||||
"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>", ""],
|
||||
"error" => ["ERROR:", "<color @r>E</color><color r>", "</color>"],
|
||||
"warn" => ["WARN:", "<color @y>W</color><color y>", "</color>"],
|
||||
|
@ -56,7 +56,7 @@ class Console implements IMessenger {
|
|||
"print" => [null, null, null],
|
||||
],
|
||||
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],
|
||||
"desc" => [">", "<color b>></color>", ""],
|
||||
"error" => ["e", "<color r>e</color><color -r>", "</color>"],
|
||||
|
@ -75,6 +75,7 @@ class Console implements IMessenger {
|
|||
];
|
||||
|
||||
function __construct(?array $params=null) {
|
||||
$output = cl::get($params, "output");
|
||||
$color = cl::get($params, "color");
|
||||
$debug = boolval(cl::get($params, "debug"));
|
||||
$minLevel = self::verifix_level(cl::get($params, "min_level"), $debug);
|
||||
|
@ -84,10 +85,14 @@ class Console implements IMessenger {
|
|||
"color" => $color,
|
||||
"indent" => static::INDENT,
|
||||
];
|
||||
if ($output !== null) {
|
||||
$this->err = $this->out = new StdOutput($output, $params);
|
||||
} else {
|
||||
$this->out = new StdOutput(STDOUT, $params);
|
||||
$this->err = new StdOutput(STDERR, $params);
|
||||
$this->minLevel = intval($minLevel);
|
||||
$this->defaultLevel = intval($defaultLevel);
|
||||
}
|
||||
$this->minLevel = $minLevel;
|
||||
$this->defaultLevel = $defaultLevel;
|
||||
$this->inSection = false;
|
||||
$this->titles = [];
|
||||
$this->title = null;
|
||||
|
@ -127,7 +132,7 @@ class Console implements IMessenger {
|
|||
$maxlen = 0;
|
||||
foreach ($lines as &$content) {
|
||||
$line = $out->filterColors($content);
|
||||
$len = strlen($line);
|
||||
$len = mb_strlen($line);
|
||||
if ($len > $maxlen) $maxlen = $len;
|
||||
$content = [$content, $len];
|
||||
}; unset($content);
|
||||
|
@ -144,7 +149,7 @@ class Console implements IMessenger {
|
|||
} else {
|
||||
$prefix = $prefixes[1];
|
||||
if ($prefix !== null) $prefix .= " ";
|
||||
$prefix2 = str_repeat(" ", strlen($prefix));
|
||||
$prefix2 = str_repeat(" ", mb_strlen($prefix));
|
||||
$lines = $out->getLines(false, $content);
|
||||
foreach ($lines as $content) {
|
||||
$out->iprint($indentLevel, $prefix, $content);
|
||||
|
@ -157,36 +162,66 @@ class Console implements IMessenger {
|
|||
bool $printContent, $content,
|
||||
bool $printResult, ?bool $rsuccess, $rcontent,
|
||||
int $indentLevel, StdOutput $out): void {
|
||||
$color = $out->isColor();
|
||||
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 .= " ";
|
||||
$rprefixes = self::RESULT_PREFIXES[$type];
|
||||
if ($color) {
|
||||
$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 ($rcontent) {
|
||||
cl::ensure_array($content);
|
||||
$content[] = ": ";
|
||||
$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) {
|
||||
$prefixes = self::GENERIC_PREFIXES[$level]["step"];
|
||||
if ($out->isColor()) {
|
||||
if ($color) {
|
||||
$prefix = $prefixes[1];
|
||||
if ($prefix !== null) $prefix .= " ";
|
||||
$prefix2 = $out->filterColors($out->filterContent($prefix));
|
||||
$prefix2 = str_repeat(" ", mb_strlen($prefix2));
|
||||
$suffix = $prefixes[2];
|
||||
} else {
|
||||
$prefix = $prefixes[0];
|
||||
if ($prefix !== null) $prefix .= " ";
|
||||
$prefix2 = str_repeat(" ", mb_strlen($prefix));
|
||||
$suffix = null;
|
||||
}
|
||||
if ($prefix !== null) $prefix .= " ";
|
||||
$out->iprint($indentLevel, $prefix, $content, $suffix, ":");
|
||||
$lines = $out->getLines(false, $content, ":");
|
||||
foreach ($lines as $content) {
|
||||
$out->iprint($indentLevel, $prefix, $content, $suffix);
|
||||
$prefix = $prefix2;
|
||||
}
|
||||
} elseif ($printResult) {
|
||||
if (!$rcontent) {
|
||||
if (!$rcontent && $color) {
|
||||
if ($rsuccess === true) $rcontent = "succès";
|
||||
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];
|
||||
if ($out->isColor()) {
|
||||
$prefix = $prefixes[1];
|
||||
$prefix2 = null;
|
||||
if ($prefix !== null) {
|
||||
$prefix .= " ";
|
||||
$prefix2 = $out->filterColors($out->filterContent($prefix));
|
||||
$prefix2 = str_repeat(" ", mb_strlen($prefix2));
|
||||
}
|
||||
$suffix = $prefixes[2];
|
||||
$lines = $out->getLines(false, $content);
|
||||
foreach ($lines as $content) {
|
||||
$out->iprint($indentLevel, $prefix, $content, $suffix);
|
||||
$prefix = $prefix2;
|
||||
}
|
||||
} else {
|
||||
$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? */
|
|
@ -149,7 +149,7 @@ class StdOutput {
|
|||
$text .= "m";
|
||||
return $text;
|
||||
}
|
||||
protected function filterContent(string $text): string {
|
||||
function filterContent(string $text): string {
|
||||
# couleur au début
|
||||
$text = preg_replace_callback('/<color([^>]*)>/', [self::class, "replace_colors"], $text);
|
||||
# reset à la fin
|
||||
|
|
|
@ -1,5 +1,15 @@
|
|||
# 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
|
||||
* plusieurs types de messages:
|
||||
* section: flush tous les messages en cours, ne peut pas être imbriqué
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<?php
|
||||
require(__DIR__.'/../vendor/autoload.php');
|
||||
|
||||
use nur\sery\output\Console;
|
||||
use nur\sery\output\StdMessenger;
|
||||
|
||||
$params = [];
|
||||
$count = count($argv) - 1;
|
||||
|
@ -20,6 +20,10 @@ for ($i = 1; $i <= $count; $i++) {
|
|||
case "+d":
|
||||
$params["debug"] = false;
|
||||
break;
|
||||
case "-L":
|
||||
$i++;
|
||||
$params["output"] = $argv[$i];
|
||||
break;
|
||||
case "-D":
|
||||
$params["default_level"] = "debug";
|
||||
$params["debug"] = true;
|
||||
|
@ -32,7 +36,7 @@ for ($i = 1; $i <= $count; $i++) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
$c = new Console($params);
|
||||
$c = new StdMessenger($params);
|
||||
|
||||
$c->section("section");
|
||||
|
||||
|
@ -102,4 +106,13 @@ $c->section("multi-line\nsection");
|
|||
$c->title("multi-line\ntitle");
|
||||
$c->title("another\ntitle");
|
||||
$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);
|
||||
|
|
Loading…
Reference in New Issue