From 331afca9bf39145a776c372a41070e3717cebfa1 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 27 Dec 2023 11:38:43 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/output/IMessenger.php | 19 ++++--- src/output/Output.php | 16 ------ src/output/TOutputObjectAsCsv.php | 11 ---- src/output/TtyOutput.php | 17 ------- src/output/WebOutput.php | 12 ----- src/output/_messenger.php | 67 ++++++++++++++++++++++++ src/output/log.php | 12 +++++ src/output/msg.php | 16 ++++++ src/output/say.php | 12 +++++ src/output/{ => std}/StdMessenger.php | 73 +++++++++++++++++++-------- src/output/{ => std}/StdOutput.php | 71 +++++++++++++++----------- tbin/test-console.php | 53 ++++++++++++------- 12 files changed, 247 insertions(+), 132 deletions(-) delete mode 100644 src/output/Output.php delete mode 100644 src/output/TOutputObjectAsCsv.php delete mode 100644 src/output/TtyOutput.php delete mode 100644 src/output/WebOutput.php create mode 100644 src/output/_messenger.php create mode 100644 src/output/log.php create mode 100644 src/output/msg.php create mode 100644 src/output/say.php rename src/output/{ => std}/StdMessenger.php (87%) rename src/output/{ => std}/StdOutput.php (75%) diff --git a/src/output/IMessenger.php b/src/output/IMessenger.php index d40bde9..84dc3ad 100644 --- a/src/output/IMessenger.php +++ b/src/output/IMessenger.php @@ -7,6 +7,9 @@ namespace nur\sery\output; interface IMessenger { const LEVEL_DEBUG = -1, LEVEL_NORMAL = 0, LEVEL_MAJOR = 1, LEVEL_NONE = 2; + /** réinitialiser les paramètres de l'objet */ + function resetParams(?array $params=null): void; + /** * commencer une section. * @@ -45,35 +48,35 @@ interface IMessenger { * * démarrer une action le cas échéant (et la terminer aussitôt) */ - function success($content=null): void; + function asuccess($content=null): void; /** * terminer l'action courante avec le résultat "échec" * * démarrer une action le cas échéant (et la terminer aussitôt) */ - function failure($content=null): void; + function afailure($content=null): void; /** * terminer l'action courante avec le résultat "neutre" * * démarrer une action le cas échéant (et la terminer aussitôt) */ - function neutral($content=null): void; + function adone($content=null): void; /** afficher une donnée non structurée */ function print($content, ?int $level=null): void; - /** ajouter un événément "information" */ + /** afficher un événément "information" */ function info($content, ?int $level=null): void; - /** ajouter un événément "information importante" */ + /** afficher un événément "information importante" */ function note($content, ?int $level=null): void; - /** ajouter un événément "avertissement" */ + /** afficher un événément "avertissement" */ function warn($content, ?int $level=null): void; - /** ajouter un événément "erreur" */ + /** afficher un événément "erreur" */ function error($content, ?int $level=null): void; /** @@ -83,5 +86,5 @@ interface IMessenger { * @param bool $all faut-il terminer *tous* les chapitres ainsi que la section * en cours? */ - function end(bool $all=false); + function end(bool $all=false): void; } diff --git a/src/output/Output.php b/src/output/Output.php deleted file mode 100644 index 18b58b5..0000000 --- a/src/output/Output.php +++ /dev/null @@ -1,16 +0,0 @@ -printCsv($object); - else $this->print([$object]); - } -} diff --git a/src/output/TtyOutput.php b/src/output/TtyOutput.php deleted file mode 100644 index 5abc156..0000000 --- a/src/output/TtyOutput.php +++ /dev/null @@ -1,17 +0,0 @@ -resetParams($params); } } + function section($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->section($content, $level); } } + function title($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->title($content, $level); } } + function desc($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->desc($content, $level); } } + function action($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->action($content, $level); } } + function step($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->step($content, $level); } } + function asuccess($content=null): void { foreach (static::get_msgs() as $msg) { $msg->asuccess($content); } } + function afailure($content=null): void { foreach (static::get_msgs() as $msg) { $msg->afailure($content); } } + function adone($content=null): void { foreach (static::get_msgs() as $msg) { $msg->adone($content); } } + function print($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->print($content, $level); } } + function info($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->info($content, $level); } } + function note($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->note($content, $level); } } + function warn($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->warn($content, $level); } } + function error($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->error($content, $level); } } + function end(bool $all=false): void { foreach (static::get_msgs() as $msg) { $msg->end($all); } } +} diff --git a/src/output/log.php b/src/output/log.php new file mode 100644 index 0000000..0bc35e0 --- /dev/null +++ b/src/output/log.php @@ -0,0 +1,12 @@ + self::LEVEL_DEBUG, - "d" => self::LEVEL_DEBUG, + "debug" => self::LEVEL_DEBUG, "verbose" => self::LEVEL_DEBUG, "normal" => self::LEVEL_NORMAL, - "n" => self::LEVEL_NORMAL, - "major" => self::LEVEL_MAJOR, - "m" => self::LEVEL_MAJOR, - "none" => self::LEVEL_NONE, + "major" => self::LEVEL_MAJOR, "quiet" => self::LEVEL_MAJOR, + "none" => self::LEVEL_NONE, "silent" => self::LEVEL_NONE, ]; protected static function verifix_level($level, int $max_level=self::LEVEL_MAJOR): int { @@ -75,23 +73,28 @@ class StdMessenger implements IMessenger { const RESULT_PREFIXES = [ "failure" => ["(FAILURE)", ""], "success" => ["(SUCCESS)", ""], - "neutral" => [null, null], + "done" => [null, null], ]; function __construct(?array $params=null) { $output = cl::get($params, "output"); $color = cl::get($params, "color"); - $debug = boolval(cl::get($params, "debug")); + $indent = cl::get($params, "indent", static::INDENT); + $defaultLevel = cl::get($params, "default_level"); if ($defaultLevel === null) $defaultLevel = self::LEVEL_NORMAL; $defaultLevel = self::verifix_level($defaultLevel); + + $debug = boolval(cl::get($params, "debug")); $minLevel = cl::get($params, "min_level"); - if ($minLevel === null) $minLevel = $debug? self::LEVEL_DEBUG: self::LEVEL_NORMAL; + if ($minLevel === null && $debug) $minLevel = self::LEVEL_DEBUG; + if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias + if ($minLevel === null) $minLevel = self::LEVEL_NORMAL; $minLevel = self::verifix_level($minLevel, self::LEVEL_NONE); $params = [ "color" => $color, - "indent" => static::INDENT, + "indent" => $indent, ]; if ($output !== null) { $this->err = $this->out = new StdOutput($output, $params); @@ -108,6 +111,35 @@ class StdMessenger implements IMessenger { $this->action = null; } + function resetParams(?array $params=null): void { + $output = cl::get($params, "output"); + $color = cl::get($params, "color"); + $indent = cl::get($params, "indent"); + + $defaultLevel = cl::get($params, "default_level"); + if ($defaultLevel !== null) $defaultLevel = self::verifix_level($defaultLevel); + + $debug = cl::get($params, "debug"); + $minLevel = cl::get($params, "min_level"); + if ($minLevel === null && $debug !== null) $minLevel = $debug? self::LEVEL_DEBUG: self::LEVEL_NORMAL; + if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias + if ($minLevel !== null) $minLevel = self::verifix_level($minLevel, self::LEVEL_NONE); + + $params = [ + "output" => $output, + "color" => $color, + "indent" => $indent, + ]; + if ($output !== null) { + $this->out->resetParams($params); + } else { + $this->out->resetParams($params); + $this->err->resetParams($params); + } + if ($defaultLevel !== null) $this->defaultLevel = $defaultLevel; + if ($minLevel !== null) $this->minLevel = $minLevel; + } + /** @var StdOutput la sortie standard */ protected $out; @@ -173,7 +205,7 @@ class StdMessenger implements IMessenger { $color = $out->isColor(); if ($rsuccess === true) $type = "success"; elseif ($rsuccess === false) $type = "failure"; - else $type = "neutral"; + else $type = "done"; $rprefixes = self::RESULT_PREFIXES[$type]; if ($color) { $rprefix = $rprefixes[1]; @@ -219,9 +251,10 @@ class StdMessenger implements IMessenger { $prefix = $prefix2; } } elseif ($printResult) { - if (!$rcontent && $color) { - if ($rsuccess === true) $rcontent = "succès"; - elseif ($rsuccess === false) $rcontent = "échec"; + if (!$rcontent) { + if ($type === "success") $rcontent = $color? "succès": ""; + elseif ($type === "failure") $rcontent = $color? "échec": ""; + elseif ($type === "done") $rcontent = "fait"; } $rprefix = " $rprefix"; $rprefix2 = " $rprefix2"; @@ -409,7 +442,7 @@ class StdMessenger implements IMessenger { $this->_printGeneric($level, "step", $content, $this->getIndentLevel(), $this->err); } - function success($content=null): void { + function asuccess($content=null): void { if (!$this->actions) $this->action(null); $this->action["result_success"] = true; $this->action["result_content"] = $content; @@ -417,7 +450,7 @@ class StdMessenger implements IMessenger { $this->endAction(); } - function failure($content=null): void { + function afailure($content=null): void { if (!$this->actions) $this->action(null); $this->action["result_success"] = false; $this->action["result_content"] = $content; @@ -425,7 +458,7 @@ class StdMessenger implements IMessenger { $this->endAction(); } - function neutral($content=null): void { + function adone($content=null): void { if (!$this->actions) $this->action(null); $this->action["result_success"] = null; $this->action["result_content"] = $content; @@ -473,9 +506,9 @@ class StdMessenger implements IMessenger { $this->_printGeneric($level, "error", $content, $this->getIndentLevel(), $this->err); } - function end(bool $all=false) { + function end(bool $all=false): void { if ($all) { - while ($this->actions) $this->neutral(); + while ($this->actions) $this->adone(); while ($this->titles) $this->endTitle(); $this->endSection(); } elseif ($this->actions) { diff --git a/src/output/StdOutput.php b/src/output/std/StdOutput.php similarity index 75% rename from src/output/StdOutput.php rename to src/output/std/StdOutput.php index 894673d..1945dc1 100644 --- a/src/output/StdOutput.php +++ b/src/output/std/StdOutput.php @@ -1,8 +1,10 @@ resetParams($params); + } + + function resetParams(?array $params=null): void { + $output = cl::get($params, "output"); $color = cl::get($params, "color"); - $filterTags = cl::get($params, "filter_tags", true); - $indent = cl::get($params, "indent", " "); + $filterTags = cl::get($params, "filter_tags"); + $indent = cl::get($params, "indent"); $flush = cl::get($params, "flush"); - if ($output === null) { - $outf = STDOUT; - } elseif ($output === "php://stdout") { - $outf = STDOUT; - } elseif ($output === "php://stderr") { - $outf = STDERR; - } elseif (!is_resource($output)) { - # si $outf est un nom de fichier, vérifier que l'ouverture se fait sans - # erreur. à partir de là, plus aucune gestion d'erreur n'est faite - $outf = @fopen($output, "ab"); - if ($outf === false) { - $error = error_get_last(); - if ($error !== null) $message = $error["message"]; - else $message = "$output: open error"; - throw new Exception($message); + if ($output !== null) { + if ($output === "php://stdout") { + $outf = STDOUT; + } elseif ($output === "php://stderr") { + $outf = STDERR; + } elseif (!is_resource($output)) { + # si $outf est un nom de fichier, vérifier que l'ouverture se fait sans + # erreur. à partir de là, plus aucune gestion d'erreur n'est faite + $outf = @fopen($output, "ab"); + if ($outf === false) { + $error = error_get_last(); + if ($error !== null) $message = $error["message"]; + else $message = "$output: open error"; + throw new Exception($message); + } + if ($flush === null) $flush = true; + } else { + $outf = $output; } - if ($flush === null) $flush = true; - } else { - $outf = $output; + $this->outf = $outf; + if ($color === null) $color = stream_isatty($outf); + if ($flush === null) $flush = false; } - if ($color === null) $color = stream_isatty($outf); - if ($flush === null) $flush = false; - - $this->outf = $outf; - $this->color = boolval($color); - $this->filterTags = boolval($filterTags); - $this->indent = $indent; - $this->flush = boolval($flush); + if ($color !== null) $this->color = boolval($color); + if ($filterTags !== null) $this->filterTags = boolval($filterTags); + if ($indent !== null) $this->indent = strval($indent); + if ($flush !== null) $this->flush = boolval($flush); } /** @var resource */ @@ -195,7 +204,9 @@ class StdOutput { function getLines(bool $withNl, ...$values): array { $values = self::flatten($values); + if (!$values) return []; $text = implode("", $values); + if ($text === "") return [""]; $text = $this->filterContent($text); if (!$this->color) $text = $this->filterColors($text); $lines = explode("\n", $text); diff --git a/tbin/test-console.php b/tbin/test-console.php index e206f40..5304b8c 100755 --- a/tbin/test-console.php +++ b/tbin/test-console.php @@ -2,7 +2,7 @@ print("print"); $c->action("action"); $c->step("step"); -$c->success("action success"); +$c->asuccess("action success"); $c->action("action"); $c->step("step"); -$c->failure("action failure"); +$c->afailure("action failure"); $c->action("action"); -$c->success("action success"); +$c->step("step"); +$c->adone("action neutral"); $c->action("action"); -$c->failure("action failure"); +$c->asuccess("action success"); + +$c->action("action"); +$c->afailure("action failure"); + +$c->action("action"); +$c->adone("action neutral"); $c->action("action0"); $c->action("action1"); $c->action("action2"); -$c->success("action2 success"); -$c->success("action1 success"); -$c->success("action0 success"); +$c->asuccess("action2 success"); +$c->asuccess("action1 success"); +$c->asuccess("action0 success"); $c->action("action"); $c->step("step"); -$c->success(); +$c->asuccess(); $c->action("action"); $c->step("step"); -$c->failure(); +$c->afailure(); $c->action("action"); -$c->success(); +$c->step("step"); +$c->adone(); $c->action("action"); -$c->failure(); +$c->asuccess(); + +$c->action("action"); +$c->afailure(); + +$c->action("action"); +$c->adone(); $c->action("action0"); $c->action("action1"); $c->action("action2"); -$c->success(); -$c->success(); -$c->success(); +$c->asuccess(); +$c->asuccess(); +$c->asuccess(); $c->info("info"); $c->note("note"); @@ -108,11 +122,14 @@ $c->title("another\ntitle"); $c->print("multi-line\nprint"); $c->info("multi-line\ninfo"); $c->action("multi-line\naction"); -$c->success(); +$c->asuccess(); $c->action("multi-line\naction"); $c->step("multi-line\nstep"); -$c->failure(); +$c->afailure(); $c->action("multi-line\naction"); $c->step("multi-line\nstep"); -$c->success("multi-line\nsuccess"); +$c->asuccess("multi-line\nsuccess"); +$c->action("multi-line\naction"); +$c->step("multi-line\nstep"); +$c->adone("multi-line\ndone"); $c->end(true);