Skip to content

Commit 9f3b520

Browse files
committed
some update +
1 parent e8e8d32 commit 9f3b520

File tree

6 files changed

+77
-44
lines changed

6 files changed

+77
-44
lines changed

examples/Controllers/HomeController.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Inhere\Console\IO\Input;
1010
use Inhere\Console\Components\Style\Highlighter;
1111
use Inhere\Console\Components\Style\LiteStyle;
12+
use Inhere\Console\Utils\Symbol\Char;
13+
use Inhere\Console\Utils\Symbol\Emoji;
1214
use Inhere\Console\Utils\Helper;
1315
use Inhere\Console\Utils\Interact;
1416
use Inhere\Console\Utils\ProgressBar;
@@ -113,6 +115,7 @@ public function defArgCommand()
113115

114116
/**
115117
* a command for test throw exception
118+
* @throws \RuntimeException
116119
*/
117120
public function exCommand()
118121
{
@@ -166,8 +169,9 @@ public function highlightCommand($in)
166169
/**
167170
* a example for use color text output by Style::class
168171
* @usage {fullCommand}
172+
* @return int
169173
*/
170-
public function colorCommand()
174+
public function colorCommand(): int
171175
{
172176
if (!$this->output->supportColor()) {
173177
$this->write('Current terminal is not support output color text.');
@@ -207,7 +211,7 @@ public function colorCheckCommand()
207211
/**
208212
* a example for use color text output by LiteStyle::class
209213
*/
210-
public function colorLiteCommand()
214+
public function colorLiteCommand(): int
211215
{
212216
if (!$this->output->supportColor()) {
213217
$this->write('Current terminal is not support output color text.');
@@ -230,7 +234,7 @@ public function colorLiteCommand()
230234
* output block message text
231235
* @return int
232236
*/
233-
public function blockMsgCommand()
237+
public function blockMsgCommand(): int
234238
{
235239
if (!$this->output->supportColor()) {
236240
$this->write('Current terminal is not support output color text.');
@@ -255,7 +259,7 @@ public function blockMsgCommand()
255259
* --style Set the art font style.
256260
* @return int
257261
*/
258-
public function artFontCommand()
262+
public function artFontCommand(): int
259263
{
260264
$name = $this->input->getLongOpt('font', '404');
261265

@@ -271,13 +275,41 @@ public function artFontCommand()
271275
return 0;
272276
}
273277

278+
/**
279+
* display some special chars
280+
* @return int
281+
* @throws \ReflectionException
282+
*/
283+
public function charCommand(): int
284+
{
285+
$this->output->aList(Char::getConstants(), 'some special char', [
286+
'ucFirst' => false,
287+
]);
288+
289+
return 0;
290+
}
291+
292+
/**
293+
* display some special emoji chars
294+
* @return int
295+
* @throws \ReflectionException
296+
*/
297+
public function emojiCommand(): int
298+
{
299+
$this->output->aList(Emoji::getConstants(), 'some emoji char', [
300+
'ucFirst' => false,
301+
]);
302+
303+
return 0;
304+
}
305+
274306
/**
275307
* dynamic notice message show: counterTxt. It is like progress txt, but no max value.
276308
* @example
277309
* {script} {command}
278310
* @return int
279311
*/
280-
public function counterCommand()
312+
public function counterCommand(): int
281313
{
282314
$total = 120;
283315
$ctt = Show::counterTxt('handling ...', 'handled.');
@@ -417,7 +449,7 @@ public function titleCommand()
417449
* @options
418450
* -w, --width WIDTH The split line width. default is current screen width.
419451
*/
420-
public function splitLineCommand()
452+
public function splitLineCommand(): int
421453
{
422454
$this->output->splitLine('', '=', $this->input->getSameOpt(['w', 'width'], 0));
423455
$this->output->splitLine('split Line', '-', $this->input->getSameOpt(['w', 'width'], 0));
@@ -431,7 +463,7 @@ public function splitLineCommand()
431463
/**
432464
* output format message: section
433465
*/
434-
public function sectionCommand()
466+
public function sectionCommand(): int
435467
{
436468
$body = 'If screen size could not be detected, or the indentation is greater than the screen size, the text will not be wrapped.' .
437469
'Word wrap text with indentation to fit the screen size,' .

src/Components/ArrBuffer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace Inhere\Console\Components;
1010

1111
/**
12-
* Class AryBuffer
12+
* Class ArrBuffer
1313
* @package Inhere\Console\Components
1414
*/
15-
final class AryBuffer
15+
final class ArrBuffer
1616
{
1717
/**
1818
* @var string[]

src/Components/Style/Color.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ final class Color
8484
* @param array $options
8585
* @param bool $extra
8686
* @return Color
87+
* @throws \InvalidArgumentException
8788
*/
8889
public static function make($fg = '', $bg = '', array $options = [], $extra = false): Color
8990
{
@@ -95,6 +96,7 @@ public static function make($fg = '', $bg = '', array $options = [], $extra = fa
9596
*
9697
* @param string $string e.g 'fg=white;bg=black;options=bold,underscore;extra=1'
9798
* @return static
99+
* @throws \InvalidArgumentException
98100
* @throws \RuntimeException
99101
*/
100102
public static function makeByString($string)
@@ -233,5 +235,4 @@ public function getKnownOptions($onlyName = true): array
233235
{
234236
return (bool)$onlyName ? array_keys(static::$knownOptions) : static::$knownOptions;
235237
}
236-
237238
}

src/Utils/FormatUtil.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,15 @@ public static function alignmentOptions(array $options): array
201201
public static function runtime($startTime, $startMem, array $info = []): array
202202
{
203203
$info['startTime'] = $startTime;
204-
$info['endTime'] = microtime(true);
205-
$info['endMemory'] = memory_get_usage();
204+
$info['endTime'] = \microtime(true);
205+
$info['endMemory'] = \memory_get_usage();
206206

207207
// 计算运行时间
208-
$info['runtime'] = number_format(($info['endTime'] - $startTime) * 1000, 3) . ' ms';
208+
$info['runtime'] = \number_format(($info['endTime'] - $startTime) * 1000, 3) . ' ms';
209209

210210
if ($startMem) {
211-
$startMem = array_sum(explode(' ', $startMem));
212-
$endMem = array_sum(explode(' ', $info['endMemory']));
211+
$startMem = \array_sum(explode(' ', $startMem));
212+
$endMem = \array_sum(explode(' ', $info['endMemory']));
213213

214214
// $info['memory'] = number_format(($endMem - $startMem) / 1024, 3) . 'kb';
215215
$info['memory'] = self::memoryUsage($endMem - $startMem);
@@ -231,18 +231,18 @@ public static function runtime($startTime, $startMem, array $info = []): array
231231
public static function memoryUsage($memory): string
232232
{
233233
if ($memory >= 1024 * 1024 * 1024) {
234-
return sprintf('%.2f Gb', $memory / 1024 / 1024 / 1024);
234+
return \sprintf('%.2f Gb', $memory / 1024 / 1024 / 1024);
235235
}
236236

237237
if ($memory >= 1024 * 1024) {
238-
return sprintf('%.2f Mb', $memory / 1024 / 1024);
238+
return \sprintf('%.2f Mb', $memory / 1024 / 1024);
239239
}
240240

241241
if ($memory >= 1024) {
242-
return sprintf('%.2f Kb', $memory / 1024);
242+
return \sprintf('%.2f Kb', $memory / 1024);
243243
}
244244

245-
return sprintf('%d B', $memory);
245+
return \sprintf('%d B', $memory);
246246
}
247247

248248
/**
@@ -266,19 +266,19 @@ public static function howLongAgo(int $secs): string
266266

267267
foreach ($timeFormats as $index => $format) {
268268
if ($secs >= $format[0]) {
269-
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
270-
|| $index === \count($timeFormats) - 1
271-
) {
269+
$next = $timeFormats[$index + 1] ?? false;
270+
271+
if (($next && $secs < $next[0]) || $index === \count($timeFormats) - 1) {
272272
if (2 === \count($format)) {
273273
return $format[1];
274274
}
275275

276-
return floor($secs / $format[2]) . ' ' . $format[1];
276+
return \floor($secs / $format[2]) . ' ' . $format[1];
277277
}
278278
}
279279
}
280280

281-
return date('Y-m-d H:i:s', $secs);
281+
return \date('Y-m-d H:i:s', $secs);
282282
}
283283

284284
/**
@@ -291,16 +291,16 @@ public static function splitStringByWidth(string $string, int $width): array
291291
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
292292
// additionally, array_slice() is not enough as some character has doubled width.
293293
// we need a function to split string not by character count but by string width
294-
if (false === $encoding = mb_detect_encoding($string, null, true)) {
295-
return str_split($string, $width);
294+
if (false === $encoding = \mb_detect_encoding($string, null, true)) {
295+
return \str_split($string, $width);
296296
}
297297

298-
$utf8String = mb_convert_encoding($string, 'utf8', $encoding);
299-
$lines = array();
298+
$utf8String = \mb_convert_encoding($string, 'utf8', $encoding);
299+
$lines = [];
300300
$line = '';
301-
foreach (preg_split('//u', $utf8String) as $char) {
301+
foreach (\preg_split('//u', $utf8String) as $char) {
302302
// test if $char could be appended to current line
303-
if (mb_strwidth($line . $char, 'utf8') <= $width) {
303+
if (\mb_strwidth($line . $char, 'utf8') <= $width) {
304304
$line .= $char;
305305
continue;
306306
}
@@ -309,10 +309,10 @@ public static function splitStringByWidth(string $string, int $width): array
309309
$line = $char;
310310
}
311311
if ('' !== $line) {
312-
$lines[] = \count($lines) ? str_pad($line, $width) : $line;
312+
$lines[] = \count($lines) ? \str_pad($line, $width) : $line;
313313
}
314314

315-
mb_convert_variables($encoding, 'utf8', $lines);
315+
\mb_convert_variables($encoding, 'utf8', $lines);
316316

317317
return $lines;
318318
}
@@ -330,7 +330,7 @@ public static function splitStringByWidth(string $string, int $width): array
330330
public static function spliceKeyValue(array $data, array $opts = []): string
331331
{
332332
$text = '';
333-
$opts = array_merge([
333+
$opts = \array_merge([
334334
'leftChar' => '', // e.g ' ', ' * '
335335
'sepChar' => ' ', // e.g ' | ' OUT: key | value
336336
'keyStyle' => '', // e.g 'info','comment'
@@ -340,7 +340,7 @@ public static function spliceKeyValue(array $data, array $opts = []): string
340340
'ucFirst' => true, // upper first char
341341
], $opts);
342342

343-
if (!is_numeric($opts['keyMaxWidth'])) {
343+
if (!\is_numeric($opts['keyMaxWidth'])) {
344344
$opts['keyMaxWidth'] = Helper::getKeyMaxWidth($data);
345345
}
346346

@@ -349,14 +349,14 @@ public static function spliceKeyValue(array $data, array $opts = []): string
349349
$opts['keyMaxWidth'] = $opts['keyMinWidth'];
350350
}
351351

352-
$keyStyle = trim($opts['keyStyle']);
352+
$keyStyle = \trim($opts['keyStyle']);
353353

354354
foreach ($data as $key => $value) {
355355
$hasKey = !\is_int($key);
356356
$text .= $opts['leftChar'];
357357

358358
if ($hasKey && $opts['keyMaxWidth']) {
359-
$key = str_pad($key, $opts['keyMaxWidth'], ' ');
359+
$key = \str_pad($key, $opts['keyMaxWidth'], ' ');
360360
$text .= Helper::wrapTag($key, $keyStyle) . $opts['sepChar'];
361361
}
362362

@@ -369,13 +369,13 @@ public static function spliceKeyValue(array $data, array $opts = []): string
369369
if (\is_bool($val)) {
370370
$val = $val ? '(True)' : '(False)';
371371
} else {
372-
$val = is_scalar($val) ? (string)$val : \gettype($val);
372+
$val = \is_scalar($val) ? (string)$val : \gettype($val);
373373
}
374374

375-
$temp .= (!is_numeric($k) ? "$k: " : '') . "$val, ";
375+
$temp .= (!\is_numeric($k) ? "$k: " : '') . "$val, ";
376376
}
377377

378-
$value = rtrim($temp, ' ,');
378+
$value = \rtrim($temp, ' ,');
379379
} else {
380380
if (\is_bool($value)) {
381381
$value = $value ? '(True)' : '(False)';
@@ -384,7 +384,7 @@ public static function spliceKeyValue(array $data, array $opts = []): string
384384
}
385385
}
386386

387-
$value = $hasKey && $opts['ucFirst'] ? ucfirst($value) : $value;
387+
$value = $hasKey && $opts['ucFirst'] ? \ucfirst($value) : $value;
388388
$text .= Helper::wrapTag($value, $opts['valStyle']) . "\n";
389389
}
390390

src/Utils/Symbol/Char.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* Time: 9:33
77
*/
88

9-
namespace Inhere\Console\Utils;
9+
namespace Inhere\Console\Utils\Symbol;
1010

1111
/**
1212
* Class FontSymbol
1313
* - 字体符号
14-
* @package Inhere\Console\Utils
14+
* @package Inhere\Console\Utils\Symbol
1515
*/
1616
final class Char
1717
{

src/Utils/Symbol/Emoji.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* Time: 9:33
77
*/
88

9-
namespace Inhere\Console\Utils;
9+
namespace Inhere\Console\Utils\Symbol;
1010

1111
/**
1212
* Class Emoji
13-
* @package Inhere\Console\Utils
13+
* @package Inhere\Console\Utils\Symbol
1414
*/
1515
final class Emoji
1616
{

0 commit comments

Comments
 (0)