Skip to content

Commit ebb3ecc

Browse files
committed
Console: Refactor Formatter
- Change default `Console::info()` prefix from `➤ ` to `> ` to address width variations on some platforms - Move `Formatter` methods `escapeTags()`, `unescapeTags()` and `removeTags()` to `ConsoleUtil`, renaming the first two to `escape()` and `removeEscapes()` respectively - Add and implement `ConsoleInterface` methods `removeEscapes()` and `removeTags()` - Adopt `Null` instead of `Default` nomenclature for formats that leave text unformatted
1 parent 1ad0a00 commit ebb3ecc

16 files changed

+341
-301
lines changed

Diff for: docs/Console.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ To override these defaults:
4646
| `error()`, `errorOnce()` | `LEVEL_ERROR` = `3` | ` ! ` |
4747
| `warn()`, `warnOnce()` | `LEVEL_WARNING` = `4` | ` ^ ` |
4848
| `group()`,`groupEnd()` | `LEVEL_NOTICE` = `5` | ` » `, ` « ` |
49-
| `info()`, `infoOnce()` | `LEVEL_NOTICE` = `5` | ` ` |
49+
| `info()`, `infoOnce()` | `LEVEL_NOTICE` = `5` | ` > ` |
5050
| `log()`, `logOnce()` | `LEVEL_INFO` = `6` | ` - ` |
5151
| `logProgress()`, `clearProgress()` | `LEVEL_INFO` = `6` | `` (spinner, TTY only) |
5252
| `debug()`, `debugOnce()` | `LEVEL_DEBUG` = `7` | ` : ` |

Diff for: scripts/check-coverage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'Core' => ['Core', true, true, '2025-02-14: code review finalised, replacement of `Legacy` classes still pending'],
3030
'Iterator' => ['Iterators', true, true, '2025-02-26: code review finalised'],
3131
'Cache' => ['Cache', true, true, '2025-02-23: code review finalised'],
32-
'Console' => ['Console', null, null, null],
32+
'Console' => ['Console', true, true, '2025-03-28: code review finalised except `Formatter` rewrite'],
3333
'Container' => ['Container', true, true, '2025-03-11: code review finalised except `Container` rewrite sans Dice'],
3434
'Http' => ['HTTP', null, null, null],
3535
'Db' => ['Db', null, null, null],

Diff for: src/Toolkit/Cli/CliApplication.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Salient\Cli;
44

55
use Salient\Cli\Exception\CliInvalidArgumentsException;
6-
use Salient\Console\Format\Formatter;
76
use Salient\Console\Format\ManPageFormat;
87
use Salient\Console\Format\MarkdownFormat;
98
use Salient\Container\Application;
@@ -233,7 +232,7 @@ private function getHelp(string $name, $node, ?CliHelpStyle $style = null): ?str
233232
foreach ($node as $childName => $childNode) {
234233
$command = $this->getNodeCommand(trim("$name $childName"), $childNode);
235234
if ($command) {
236-
$synopses[] = '__' . $childName . '__ - ' . Formatter::escapeTags($command->getDescription());
235+
$synopses[] = '__' . $childName . '__ - ' . Console::escape($command->getDescription());
237236
} elseif (is_array($childNode)) {
238237
$synopses[] = '__' . $childName . '__';
239238
}
@@ -260,7 +259,7 @@ private function getUsage(string $name, $node): ?string
260259

261260
if ($command) {
262261
return $command->getSynopsis($style)
263-
. Formatter::escapeTags("\n\nSee '"
262+
. Console::escape("\n\nSee '"
264263
. ($name === '' ? "$progName --help" : "$progName help $name")
265264
. "' for more information.");
266265
}
@@ -279,15 +278,15 @@ private function getUsage(string $name, $node): ?string
279278
$synopsis = $command->getSynopsis($style);
280279
} elseif (is_array($childNode)) {
281280
$synopsis = "$fullName $childName <command>";
282-
$synopsis = Formatter::escapeTags($synopsis);
281+
$synopsis = Console::escape($synopsis);
283282
} else {
284283
continue;
285284
}
286285
$synopses[] = $synopsis;
287286
}
288287

289288
return implode("\n", $synopses)
290-
. Formatter::escapeTags("\n\nSee '"
289+
. Console::escape("\n\nSee '"
291290
. Arr::implode(' ', ["$progName help", $name, '<command>'])
292291
. "' for more information.");
293292
}

Diff for: src/Toolkit/Cli/CliCommand.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Salient\Cli\Exception\CliInvalidArgumentsException;
66
use Salient\Cli\Exception\CliUnknownValueException;
7-
use Salient\Console\Format\Formatter;
87
use Salient\Contract\Cli\CliApplicationInterface;
98
use Salient\Contract\Cli\CliCommandInterface;
109
use Salient\Contract\Cli\CliHelpSectionName;
@@ -314,7 +313,7 @@ final public function getHelp(?CliHelpStyleInterface $style = null): array
314313
) {
315314
foreach ($option->AllowedValues ?: [true, false] as $optionValue) {
316315
$optionValue = $option->normaliseValueForHelp($optionValue);
317-
$allowed[] = $em . Formatter::escapeTags($optionValue) . $em;
316+
$allowed[] = $em . Console::escape($optionValue) . $em;
318317
}
319318
if (!$option->AllowedValues) {
320319
$booleanValue = true;
@@ -382,7 +381,7 @@ final public function getHelp(?CliHelpStyleInterface $style = null): array
382381
$booleanValue
383382
|| mb_strlen($formatter->wrapsAfterFormatting()
384383
? $formatter->format($indent . $_line)
385-
: Formatter::removeTags($indent . $_line)) <= ($width ?: 76)
384+
: Console::removeTags($indent . $_line)) <= ($width ?: 76)
386385
) {
387386
$allowed = null;
388387
} else {
@@ -428,7 +427,7 @@ final public function getHelp(?CliHelpStyleInterface $style = null): array
428427
continue;
429428
}
430429
$value = $option->normaliseValueForHelp($value);
431-
$default[] = $em . Formatter::escapeTags($value) . $em;
430+
$default[] = $em . Console::escape($value) . $em;
432431
}
433432
$default = implode(Str::coalesce($option->Delimiter, ' '), $default);
434433
if ($default !== '') {
@@ -445,8 +444,8 @@ final public function getHelp(?CliHelpStyleInterface $style = null): array
445444
. ($lines ? $descriptionPrefix . ltrim(implode("\n\n", $lines)) : '');
446445
}
447446

448-
$name = Formatter::escapeTags($this->getNameWithProgram());
449-
$summary = Formatter::escapeTags($this->getDescription());
447+
$name = Console::escape($this->getNameWithProgram());
448+
$summary = Console::escape($this->getDescription());
450449
$synopsis = $this->getSynopsis($style);
451450

452451
$description = $this->getLongDescription() ?? '';

Diff for: src/Toolkit/Cli/CliHelpStyle.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Salient\Cli;
44

5-
use Salient\Console\Format\Formatter;
65
use Salient\Console\Format\LoopbackFormat;
76
use Salient\Console\Format\ManPageFormat;
87
use Salient\Console\Format\MarkdownFormat;
@@ -262,7 +261,7 @@ public function maybeEscapeTags(string $string): string
262261
if ($this->HasMarkup) {
263262
return $string;
264263
}
265-
return Formatter::escapeTags($string);
264+
return Console::escape($string);
266265
}
267266

268267
public static function getConsoleWidth(): ?int

Diff for: src/Toolkit/Console/Console.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Salient\Console;
44

55
use Psr\Log\LoggerInterface;
6-
use Salient\Console\Format\Formatter;
76
use Salient\Console\Target\StreamTarget;
87
use Salient\Contract\Console\Target\HasPrefix;
98
use Salient\Contract\Console\Target\StreamTargetInterface;
@@ -224,7 +223,23 @@ public function getTtyTarget(): StreamTargetInterface
224223
*/
225224
public function escape(string $string, bool $escapeNewlines = false): string
226225
{
227-
return Formatter::escapeTags($string, $escapeNewlines);
226+
return ConsoleUtil::escape($string, $escapeNewlines);
227+
}
228+
229+
/**
230+
* @inheritDoc
231+
*/
232+
public function removeEscapes(string $string): string
233+
{
234+
return ConsoleUtil::removeEscapes($string);
235+
}
236+
237+
/**
238+
* @inheritDoc
239+
*/
240+
public function removeTags(string $string): string
241+
{
242+
return ConsoleUtil::removeTags($string);
228243
}
229244

230245
/**

Diff for: src/Toolkit/Console/ConsoleUtil.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Salient\Console;
4+
5+
use Salient\Console\Format\Formatter;
6+
use Salient\Utility\AbstractUtility;
7+
use Salient\Utility\Regex;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class ConsoleUtil extends AbstractUtility implements HasConsoleRegex
13+
{
14+
/**
15+
* Escape inline formatting tags in a string
16+
*/
17+
public static function escape(string $string, bool $escapeNewlines = false): string
18+
{
19+
$string = addcslashes($string, '\#*<>_`~');
20+
return $escapeNewlines
21+
? str_replace("\n", "\\\n", $string)
22+
: $string;
23+
}
24+
25+
/**
26+
* Remove escapes from inline formatting tags in a string
27+
*/
28+
public static function removeEscapes(string $string): string
29+
{
30+
return Regex::replace(self::ESCAPE_REGEX, '$1', $string);
31+
}
32+
33+
/**
34+
* Remove inline formatting tags from a string
35+
*/
36+
public static function removeTags(string $string): string
37+
{
38+
return Formatter::getNullFormatter()->format($string);
39+
}
40+
}

0 commit comments

Comments
 (0)