|
11 | 11 |
|
12 | 12 | use RuntimeException;
|
13 | 13 | use Toolkit\Cli\Color\Alert;
|
14 |
| -use Toolkit\Cli\Color\Prompt; |
15 | 14 | use Toolkit\Cli\Color\ColorTag;
|
| 15 | +use Toolkit\Cli\Color\Prompt; |
16 | 16 | use Toolkit\Cli\Traits\ReadMessageTrait;
|
17 | 17 | use Toolkit\Cli\Traits\WriteMessageTrait;
|
| 18 | +use function array_shift; |
18 | 19 | use function count;
|
19 | 20 | use function date;
|
20 | 21 | use function defined;
|
|
26 | 27 | use function json_encode;
|
27 | 28 | use function preg_replace;
|
28 | 29 | use function sprintf;
|
| 30 | +use function str_ends_with; |
29 | 31 | use function stream_isatty;
|
30 | 32 | use function strtoupper;
|
| 33 | +use function substr; |
31 | 34 | use function trim;
|
32 | 35 | use const DIRECTORY_SEPARATOR;
|
33 | 36 | use const JSON_PRETTY_PRINT;
|
|
44 | 47 | * @method static prompt(string|array|mixed $message, string $style = 'info')
|
45 | 48 | *
|
46 | 49 | * @method static red(string ...$message) Print red color message line.
|
| 50 | + * @method static redf(string $format, ...$args) Print red color message, use like sprintf. |
47 | 51 | * @method static cyan(string ...$message) Print cyan color message line.
|
| 52 | + * @method static cyanf(string $format, ...$args) Print cyan color message, use like sprintf. |
48 | 53 | * @method static blue(string ...$message) Print blue color message line.
|
49 | 54 | * @method static green(string ...$message) Print green color message line.
|
50 | 55 | * @method static magenta(string ...$message) Print cyan color message line.
|
51 | 56 | * @method static yellow(string ...$message) Print yellow color message line.
|
52 | 57 | *
|
53 | 58 | * @method static error(string ...$message) Print error style message line.
|
| 59 | + * @method static errorf(string $format, ...$args) Print error style message, use like sprintf. |
54 | 60 | * @method static warn(string ...$message) Print warn style message line.
|
| 61 | + * @method static warnf(string $format, ...$args) Print warn style message, use like sprintf. |
55 | 62 | * @method static info(string ...$message) Print info style message line.
|
| 63 | + * @method static infof(string $format, ...$args) Print info style message, use like sprintf. |
56 | 64 | * @method static note(string ...$message) Print note style message line.
|
| 65 | + * @method static notef(string $format, ...$args) Print note style message, use like sprintf. |
57 | 66 | * @method static notice(string ...$message) Print notice style message line.
|
58 | 67 | * @method static success(string ...$message) Print success style message line.
|
| 68 | + * @method static successf(string $format, ...$args) Print success style message, use like sprintf. |
59 | 69 | */
|
60 | 70 | class Cli
|
61 | 71 | {
|
@@ -83,6 +93,18 @@ public static function __callStatic(string $method, array $args): void
|
83 | 93 | return;
|
84 | 94 | }
|
85 | 95 |
|
| 96 | + // use like sprintf |
| 97 | + if (str_ends_with($method, 'f') ) { |
| 98 | + $realName = substr($method, 0, -1); |
| 99 | + |
| 100 | + if (isset(Color::STYLES[$realName]) && count($args) > 1) { |
| 101 | + $fmt = (string)array_shift($args); |
| 102 | + $msg = count($args) > 1 ? sprintf($fmt, ...$args) : $fmt; |
| 103 | + echo Color::render($msg, $realName); |
| 104 | + return; |
| 105 | + } |
| 106 | + } |
| 107 | + |
86 | 108 | throw new RuntimeException('call unknown method: ' . $method);
|
87 | 109 | }
|
88 | 110 |
|
@@ -147,7 +169,7 @@ public static function color(string $text, array|string $style = null): string
|
147 | 169 | * @param array $data
|
148 | 170 | * @param string $type
|
149 | 171 | * @param array{writeOpts:array} $labels
|
150 |
| - * @deprecated please use Clog::info(); |
| 172 | + * @deprecated please use Util\Clog::log() |
151 | 173 | */
|
152 | 174 | public static function clog(string $msg, array $data = [], string $type = 'info', array $labels = []): void
|
153 | 175 | {
|
|
0 commit comments