Skip to content

Commit 1e98837

Browse files
committed
update some message render methods
1 parent 2d093ba commit 1e98837

File tree

13 files changed

+112
-73
lines changed

13 files changed

+112
-73
lines changed

src/AbstractApplication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ abstract class AbstractApplication implements ApplicationInterface
5656
/** @var array Application config data */
5757
private $config = [
5858
'name' => 'My Console Application',
59+
'description' => 'This is my console application',
5960
'debug' => Console::VERB_ERROR,
6061
'profile' => false,
6162
'version' => '0.5.1',

src/AbstractHandler.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function __construct(Input $input, Output $output, InputDefinition $defin
107107
$this->definition = $definition;
108108
}
109109

110-
$this->commentsVars = $this->annotationVars();
110+
$this->commentsVars = $this->annotationVars();
111111

112112
$this->init();
113113
}
@@ -418,15 +418,16 @@ protected function setCommentsVar(string $name, $value): void
418418
protected function parseCommentsVars(string $str): string
419419
{
420420
// not use vars
421-
if (false === \strpos($str, '{')) {
421+
if (false === \strpos($str, self::HELP_VAR_LEFT)) {
422422
return $str;
423423
}
424424

425425
static $map;
426426

427427
if ($map === null) {
428428
foreach ($this->commentsVars as $key => $value) {
429-
$key = \sprintf(self::ANNOTATION_VAR, $key);
429+
$key = self::HELP_VAR_LEFT . $key . self::HELP_VAR_RIGHT;
430+
// save
430431
$map[$key] = $value;
431432
}
432433
}
@@ -559,7 +560,7 @@ protected function showHelpByMethodAnnotations(string $method, string $action =
559560
unset($help['Description:']);
560561
}
561562

562-
$help['Group Options:'] = null;
563+
$help['Group Options:'] = null;
563564
$help['Global Options:'] = FormatUtil::alignOptions(Application::getGlobalOptions());
564565

565566
$this->beforeRenderCommandHelp($help);

src/Component/Formatter/HelpPanel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Console\Component\Formatter;
1010

1111
use Inhere\Console\Component\MessageFormatter;
12+
use Inhere\Console\Console;
1213
use Inhere\Console\Util\FormatUtil;
1314
use Inhere\Console\Util\Show;
1415

@@ -124,7 +125,7 @@ public static function show(array $config): void
124125
}
125126

126127
if ($parts) {
127-
Show::write(\implode("\n", $parts), false);
128+
Console::write(\implode("\n", $parts), false);
128129
}
129130
}
130131
}

src/Component/Formatter/Panel.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Console\Component\Formatter;
1010

1111
use Inhere\Console\Component\MessageFormatter;
12+
use Inhere\Console\Console;
1213
use Inhere\Console\Util\FormatUtil;
1314
use Inhere\Console\Util\Show;
1415
use Toolkit\StrUtil\StrBuffer;
@@ -80,7 +81,7 @@ class Panel extends MessageFormatter
8081
public static function show($data, string $title = 'Information Panel', array $opts = []): int
8182
{
8283
if (!$data) {
83-
Show::write('<info>No data to display!</info>');
84+
Console::write('<info>No data to display!</info>');
8485
return -2;
8586
}
8687

@@ -138,21 +139,21 @@ public static function show($data, string $title = 'Information Panel', array $o
138139

139140
$border = null;
140141
$panelWidth = $labelMaxWidth + $valueMaxWidth;
141-
Show::startBuffer();
142+
Console::startBuffer();
142143

143144
// output title
144145
if ($title) {
145146
$title = \ucwords($title);
146147
$titleLength = \mb_strlen($title, 'UTF-8');
147148
$panelWidth = $panelWidth > $titleLength ? $panelWidth : $titleLength;
148149
$indentSpace = \str_pad(' ', \ceil($panelWidth / 2) - \ceil($titleLength / 2) + 2 * 2, ' ');
149-
Show::write(" {$indentSpace}<bold>{$title}</bold>");
150+
Console::write(" {$indentSpace}<bold>{$title}</bold>");
150151
}
151152

152153
// output panel top border
153154
if ($borderChar) {
154155
$border = \str_pad($borderChar, $panelWidth + (3 * 3), $borderChar);
155-
Show::write(' ' . $border);
156+
Console::write(' ' . $border);
156157
}
157158

158159
// output panel body
@@ -164,14 +165,14 @@ public static function show($data, string $title = 'Information Panel', array $o
164165
]);
165166

166167
// already exists "\n"
167-
Show::write($panelStr, false);
168+
Console::write($panelStr, false);
168169

169170
// output panel bottom border
170171
if ($border) {
171-
Show::write(" $border\n");
172+
Console::write(" $border\n");
172173
}
173174

174-
Show::flushBuffer();
175+
Console::flushBuffer();
175176
unset($panelData);
176177
return 0;
177178
}

src/Component/Interact/LimitedAsk.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static function ask(
102102
return $default;
103103
}
104104

105-
Show::write("\n You've entered incorrectly <danger>$back</danger> times in a row. exit!", true, 1);
105+
Console::write("\n You've entered incorrectly <danger>$back</danger> times in a row. exit!", true, 1);
106106
return '';
107107
}
108108
}

src/Component/Symbol/ArtFont.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Inhere\Console\Component\Symbol;
1010

11+
use Inhere\Console\Console;
1112
use Inhere\Console\Util\Helper;
1213
use Inhere\Console\Util\Show;
1314
use Toolkit\Cli\ColorTag;
@@ -145,14 +146,14 @@ public function show(string $name, string $group = null, array $opts = []): int
145146
} elseif (isset($this->groups[$group])) {
146147
$font = $this->groups[$group] . $name . $pfxType . '.txt';
147148

148-
if (is_file($font)) {
149-
$txt = file_get_contents($font);
149+
if (\is_file($font)) {
150+
$txt = \file_get_contents($font);
150151
}
151152
}
152153

153154
// var_dump($txt, $this);
154155
if ($txt) {
155-
return Show::write(ColorTag::wrap($txt, $opts['style']));
156+
return Console::write(ColorTag::wrap($txt, $opts['style']));
156157
}
157158

158159
return 0;

src/Console.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Console
4646
private static $app;
4747

4848
/** @var string */
49-
private static $buffer;
49+
private static $buffer = '';
5050

5151
/** @var bool */
5252
private static $buffering = false;
@@ -94,7 +94,7 @@ public static function style(): Style
9494
***********************************************************************************/
9595

9696
/**
97-
* Format and write message to terminal
97+
* Format and write message to terminal. like printf()
9898
* @param string $format
9999
* @param mixed ...$args
100100
* @return int
@@ -279,7 +279,7 @@ public static function log(string $msg, array $data = [], int $level = self::VER
279279
* @param array $opts
280280
* @return string
281281
*/
282-
public static function read($message = null, $nl = false, array $opts = []): string
282+
public static function read($message = null, bool $nl = false, array $opts = []): string
283283
{
284284
if ($message) {
285285
self::write($message, $nl);
@@ -405,31 +405,31 @@ public static function setBuffer(string $buffer): void
405405
}
406406

407407
/**
408-
* start buffering
408+
* Start buffering
409409
*/
410410
public static function startBuffer(): void
411411
{
412412
self::$buffering = true;
413413
}
414414

415415
/**
416-
* start buffering
416+
* Clear buffering
417417
*/
418418
public static function clearBuffer(): void
419419
{
420-
self::$buffer = null;
420+
self::$buffer = '';
421421
}
422422

423423
/**
424-
* stop buffering
425-
* @see Show::write()
424+
* Stop buffering
425+
* @see write()
426426
* @param bool $flush Whether flush buffer to output stream
427427
* @param bool $nl Default is False, because the last write() have been added "\n"
428428
* @param bool $quit
429429
* @param array $opts
430-
* @return null|string If flush = False, will return all buffer text.
430+
* @return string If flush = False, will return all buffer text.
431431
*/
432-
public static function stopBuffer($flush = true, $nl = false, $quit = false, array $opts = []): ?string
432+
public static function stopBuffer($flush = true, $nl = false, $quit = false, array $opts = []): string
433433
{
434434
self::$buffering = false;
435435

@@ -441,15 +441,15 @@ public static function stopBuffer($flush = true, $nl = false, $quit = false, arr
441441
self::write(self::$buffer, $nl, $quit, $opts);
442442

443443
// clear buffer
444-
self::$buffer = null;
444+
self::$buffer = '';
445445
}
446446

447447
return self::$buffer;
448448
}
449449

450450
/**
451-
* stop buffering and flush buffer text
452-
* @see Show::write()
451+
* Stop buffering and flush buffer text
452+
* @see write()
453453
* @param bool $nl
454454
* @param bool $quit
455455
* @param array $opts

src/Contract/CommandHandlerInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ interface CommandHandlerInterface
2525
// name -> {name}
2626
public const ANNOTATION_VAR = '{%s}'; // '{$%s}';
2727

28+
// {$%s} name -> {name}
29+
public const HELP_VAR_LEFT = '{';
30+
public const HELP_VAR_RIGHT = '}';
31+
2832
/**
2933
* Run command
3034
* @param string $command

src/IO/Output.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Console\IO;
1010

1111
use Inhere\Console\Component\Style\Style;
12+
use Inhere\Console\Console;
1213
use Inhere\Console\Traits\FormatOutputAwareTrait;
1314
use Inhere\Console\Util\Show;
1415
use Toolkit\Cli\Cli;
@@ -22,14 +23,16 @@ class Output implements OutputInterface
2223
use FormatOutputAwareTrait;
2324

2425
/**
25-
* 正常输出流
26-
* Property outStream.
26+
* Normal output stream
27+
*
28+
* @var resource
2729
*/
2830
protected $outputStream = \STDOUT;
2931

3032
/**
31-
* 错误输出流
32-
* Property errorStream.
33+
* Error output stream
34+
*
35+
* @var resource
3336
*/
3437
protected $errorStream = \STDERR;
3538

@@ -62,25 +65,25 @@ public function __construct($outputStream = null)
6265
*/
6366
public function startBuffer(): void
6467
{
65-
Show::startBuffer();
68+
Console::startBuffer();
6669
}
6770

6871
/**
6972
* clear buffering
7073
*/
7174
public function clearBuffer(): void
7275
{
73-
Show::clearBuffer();
76+
Console::clearBuffer();
7477
}
7578

7679
/**
7780
* stop buffering and flush buffer text
7881
* {@inheritdoc}
79-
* @see Show::stopBuffer()
82+
* @see Console::stopBuffer()
8083
*/
8184
public function stopBuffer(bool $flush = true, $nl = false, $quit = false, array $opts = []): void
8285
{
83-
Show::stopBuffer($flush, $nl, $quit, $opts);
86+
Console::stopBuffer($flush, $nl, $quit, $opts);
8487
}
8588

8689
/**
@@ -89,41 +92,35 @@ public function stopBuffer(bool $flush = true, $nl = false, $quit = false, array
8992
*/
9093
public function flush(bool $nl = false, $quit = false, array $opts = []): void
9194
{
92-
$this->stopBuffer(true, $nl, $quit, $opts);
95+
Console::flushBuffer($nl, $quit, $opts);
9396
}
9497

9598
/***************************************************************************
9699
* Output Message
97100
***************************************************************************/
98101

99102
/**
100-
* 读取输入信息
103+
* Read input information
101104
* @param string $question 若不为空,则先输出文本
102105
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
103106
* @return string
104107
*/
105-
public function read($question = null, $nl = false): string
108+
public function read(string $question = '', bool $nl = false): string
106109
{
107-
if ($question) {
108-
$this->write($question, $nl);
109-
}
110-
111-
return \trim(\fgets(\STDIN));
110+
return Console::read($question, $nl);
112111
}
113112

114113
/**
115114
* Write a message to standard error output stream.
116115
* @param string $text
117116
* @param boolean $nl True (default) to append a new line at the end of the output string.
118-
* @return $this
117+
* @return int
119118
*/
120-
public function stderr($text = '', $nl = true): self
119+
public function stderr(string $text = '', $nl = true): int
121120
{
122-
$text = $this->getStyle()->format($text);
123-
124-
\fwrite($this->errorStream, $text . ($nl ? "\n" : null));
125-
126-
return $this;
121+
return Console::write($text, $nl, [
122+
'steam' => $this->errorStream,
123+
]);
127124
}
128125

129126
/***************************************************************************

0 commit comments

Comments
 (0)