From 64fd232f71a99fd7eb8effcc3a925d4272af0ba7 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 27 Dec 2023 01:39:08 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/output/{Console.php => StdMessenger.php} | 101 ++++++++++++++----- src/output/StdOutput.php | 2 +- src/output/TODO.md | 10 ++ tbin/test-console.php | 17 +++- 4 files changed, 100 insertions(+), 30 deletions(-) rename src/output/{Console.php => StdMessenger.php} (82%) diff --git a/src/output/Console.php b/src/output/StdMessenger.php similarity index 82% rename from src/output/Console.php rename to src/output/StdMessenger.php index bd0331b..9272475 100644 --- a/src/output/Console.php +++ b/src/output/StdMessenger.php @@ -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!", "===", "=", "=", "==="], - "title" => [false, "TITLE!", null, "T", "", "---"], + "title" => [false, "TITLE!", null, "T", "", "==="], "desc" => ["DESC!", ">", ""], "error" => ["CRITICAL!", "E!", ""], - "warn" => ["ATTENTION!", "W!", ""], - "note" => ["IMPORTANT!", "N!", ""], + "warn" => ["WARNING!", "W!", ""], + "note" => ["ATTENTION!", "N!", ""], "info" => ["IMPORTANT!", "I!", ""], "step" => ["*", ".", ""], "print" => [null, null, null], ], self::LEVEL_NORMAL => [ "section" => [true, "SECTION:", "---", "-", "-", "---"], - "title" => [false, "TITLE:", null, "T", "", null], + "title" => [false, "TITLE:", null, "T", "", "---"], "desc" => ["DESC:", ">", ""], "error" => ["ERROR:", "E", ""], "warn" => ["WARN:", "W", ""], @@ -56,7 +56,7 @@ class Console implements IMessenger { "print" => [null, null, null], ], self::LEVEL_DEBUG => [ - "section" => [false, "section", null, ">>", "<<", null], + "section" => [true, "section", null, ">>", "<<", null], "title" => [false, "title", null, "t", "", null], "desc" => [">", ">", ""], "error" => ["e", "e", ""], @@ -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, ]; - $this->out = new StdOutput(STDOUT, $params); - $this->err = new StdOutput(STDERR, $params); - $this->minLevel = intval($minLevel); - $this->defaultLevel = intval($defaultLevel); + 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 = $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? */ diff --git a/src/output/StdOutput.php b/src/output/StdOutput.php index e969485..894673d 100644 --- a/src/output/StdOutput.php +++ b/src/output/StdOutput.php @@ -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('/]*)>/', [self::class, "replace_colors"], $text); # reset à la fin diff --git a/src/output/TODO.md b/src/output/TODO.md index a8cd04d..7c3bebb 100644 --- a/src/output/TODO.md +++ b/src/output/TODO.md @@ -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é diff --git a/tbin/test-console.php b/tbin/test-console.php index 186cb86..e206f40 100755 --- a/tbin/test-console.php +++ b/tbin/test-console.php @@ -2,7 +2,7 @@ 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);