Skip to content

Commit 4e64910

Browse files
committed
update some logic for write read message
1 parent 919e986 commit 4e64910

File tree

9 files changed

+901
-46
lines changed

9 files changed

+901
-46
lines changed

.php_cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
$header = <<<'EOF'
44
This file is part of toolkit/cli-utils.
55
6-
@link https://github.com/inhere
6+
@link https://github.com/php-toolkit/cli-utils
77
@author https://github.com/inhere
88
@license MIT
99
EOF;
@@ -17,7 +17,10 @@ return PhpCsFixer\Config::create()
1717
],
1818
'class_attributes_separation' => true,
1919
'declare_strict_types' => true,
20-
'global_namespace_import' => true,
20+
'global_namespace_import' => [
21+
'import_constants' => true,
22+
'import_functions' => true,
23+
],
2124
'header_comment' => [
2225
'comment_type' => 'PHPDoc',
2326
'header' => $header,

src/App.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function __construct(array $config = [], array $argv = null)
108108
self::$i = $this;
109109

110110
// get current dir
111-
$this->pwd = getcwd();
111+
$this->pwd = (string)getcwd();
112112

113113
// parse cli argv
114114
$argv = $argv ?? (array)$_SERVER['argv'];
@@ -120,9 +120,10 @@ public function __construct(array $config = [], array $argv = null)
120120
$this->script = array_shift($argv);
121121

122122
// parse flags
123-
[$this->args, $this->opts] = Flags::parseArgv(array_values($argv), [
124-
'mergeOpts' => true
125-
]);
123+
[
124+
$this->args,
125+
$this->opts
126+
] = Flags::parseArgv(array_values($argv), ['mergeOpts' => true]);
126127
}
127128

128129
/**

src/Cli.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Toolkit\Cli;
1111

12+
use Toolkit\Cli\Traits\ReadMessageTrait;
13+
use Toolkit\Cli\Traits\WriteMessageTrait;
1214
use function date;
1315
use function defined;
1416
use function fflush;
@@ -34,7 +36,6 @@
3436
use const PHP_WINDOWS_VERSION_MAJOR;
3537
use const PHP_WINDOWS_VERSION_MINOR;
3638
use const STDERR;
37-
use const STDIN;
3839
use const STDOUT;
3940

4041
/**
@@ -44,6 +45,8 @@
4445
*/
4546
class Cli
4647
{
48+
// use ReadMessageTrait, WriteMessageTrait;
49+
4750
public const LOG_LEVEL2TAG = [
4851
'info' => 'info',
4952
'warn' => 'warning',
@@ -54,23 +57,12 @@ class Cli
5457
];
5558

5659
/*******************************************************************************
57-
* read/write message
60+
* read message
5861
******************************************************************************/
5962

60-
/**
61-
* @param string $message
62-
* @param bool $nl
63-
*
64-
* @return string
65-
*/
66-
public static function read(string $message = '', bool $nl = false): string
67-
{
68-
if ($message) {
69-
self::write($message, $nl);
70-
}
71-
72-
return trim(fgets(STDIN));
73-
}
63+
/*******************************************************************************
64+
* write message
65+
******************************************************************************/
7466

7567
/**
7668
* @param string $format
@@ -138,7 +130,7 @@ public static function stderr(string $message, $nl = true, $quit = -1): void
138130
******************************************************************************/
139131

140132
/**
141-
* @param $text
133+
* @param string $text
142134
* @param string|int|array $style
143135
*
144136
* @return string

src/Highlighter.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@
1313
use function array_merge;
1414
use function array_slice;
1515
use function defined;
16-
use function end;
1716
use function explode;
1817
use function file_get_contents;
1918
use function function_exists;
2019
use function implode;
2120
use function is_array;
22-
use function key;
2321
use function max;
2422
use function str_pad;
2523
use function str_replace;
26-
use function strlen;
2724
use function token_get_all;
2825
use const PHP_EOL;
2926
use const STR_PAD_LEFT;
@@ -82,8 +79,10 @@ class Highlighter
8279
/** @var self */
8380
private static $instance;
8481

85-
/** @var array */
86-
private $defaultTheme = [
82+
/**
83+
* @var array
84+
*/
85+
private $codeTheme = [
8786
self::TOKEN_STRING => 'green',
8887
self::TOKEN_COMMENT => 'italic',
8988
self::TOKEN_KEYWORD => 'yellow',
@@ -336,7 +335,7 @@ private function colorLines(array $tokenLines): array
336335
foreach ($tokenLines as $lineCount => $tokenLine) {
337336
$line = '';
338337
foreach ($tokenLine as [$tokenType, $tokenValue]) {
339-
$style = $this->defaultTheme[$tokenType];
338+
$style = $this->codeTheme[$tokenType];
340339

341340
if (Color::hasStyle($style)) {
342341
$line .= Color::apply($style, $tokenValue);
@@ -359,22 +358,22 @@ private function colorLines(array $tokenLines): array
359358
*/
360359
private function lineNumbers(array $lines, $markLine = null): string
361360
{
362-
end($lines);
363-
364361
$snippet = '';
365-
$lineLen = strlen(key($lines) + 1);
366-
$lmStyle = $this->defaultTheme[self::ACTUAL_LINE_MARK];
367-
$lnStyle = $this->defaultTheme[self::LINE_NUMBER];
362+
$lineLen = count($lines) + 1;
363+
$lmStyle = $this->codeTheme[self::ACTUAL_LINE_MARK];
364+
$lnStyle = $this->codeTheme[self::LINE_NUMBER];
368365

369366
foreach ($lines as $i => $line) {
367+
$lineNum = $i + 1;
368+
$lineStr = (string)($lineNum);
370369
if ($markLine !== null) {
371-
$snippet .= ($markLine === $i + 1 ? Color::apply($lmStyle, ' > ') : ' ');
370+
$snippet .= ($markLine === $lineNum ? Color::apply($lmStyle, ' > ') : ' ');
372371
$snippet .= Color::apply(
373-
$markLine === $i + 1 ? $lmStyle : $lnStyle,
374-
str_pad($i + 1, $lineLen, ' ', STR_PAD_LEFT) . '| '
372+
$markLine === $lineNum ? $lmStyle : $lnStyle,
373+
str_pad($lineStr, $lineLen, ' ', STR_PAD_LEFT) . '| '
375374
);
376375
} else {
377-
$snippet .= Color::apply($lnStyle, str_pad($i + 1, $lineLen, ' ', STR_PAD_LEFT) . '| ');
376+
$snippet .= Color::apply($lnStyle, str_pad($lineStr, $lineLen, ' ', STR_PAD_LEFT) . '| ');
378377
}
379378

380379
$snippet .= $line . PHP_EOL;
@@ -386,16 +385,16 @@ private function lineNumbers(array $lines, $markLine = null): string
386385
/**
387386
* @return array
388387
*/
389-
public function getDefaultTheme(): array
388+
public function getCodeTheme(): array
390389
{
391-
return $this->defaultTheme;
390+
return $this->codeTheme;
392391
}
393392

394393
/**
395-
* @param array $defaultTheme
394+
* @param array $codeTheme
396395
*/
397-
public function setDefaultTheme(array $defaultTheme): void
396+
public function setCodeTheme(array $codeTheme): void
398397
{
399-
$this->defaultTheme = array_merge($this->defaultTheme, $defaultTheme);
398+
$this->codeTheme = array_merge($this->codeTheme, $codeTheme);
400399
}
401400
}

0 commit comments

Comments
 (0)