Skip to content

Commit ec889c9

Browse files
committed
up: update some for use readline
Signed-off-by: inhere <[email protected]>
1 parent a689bc7 commit ec889c9

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

Diff for: src/Style.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ class Style
8686
/**
8787
* @return Style
8888
*/
89-
public static function global(): self
89+
public static function instance(): self
9090
{
91-
return self::instance();
91+
return self::global();
9292
}
9393

9494
/**
9595
* @return Style
9696
*/
97-
public static function instance(): self
97+
public static function global(): self
9898
{
9999
if (!self::$instance) {
100100
self::$instance = new self();

Diff for: src/Traits/ReadMessageTrait.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
namespace Toolkit\Cli\Traits;
1111

1212
use Toolkit\Cli\Cli;
13+
use Toolkit\Cli\Style;
1314
use Toolkit\Cli\Util\Readline;
1415
use function fopen;
16+
use function implode;
17+
use function is_array;
1518
use function strip_tags;
19+
use const PHP_EOL;
1620
use const STDIN;
1721

1822
/**
@@ -64,16 +68,22 @@ public static function read($message = null, bool $nl = false, array $opts = [])
6468
*/
6569
public static function readln($message = null, bool $nl = false, array $opts = []): string
6670
{
67-
if ($message) {
68-
Cli::write($message, $nl);
69-
}
70-
71-
// TIP: use readline method, support left and right press.
71+
// TIP: use readline method, support left and right keypress.
7272
if (Readline::isSupported()) {
73-
return Readline::readline();
73+
if ($message && is_array($message)) {
74+
$message = implode($nl ? PHP_EOL : '', $message);
75+
}
76+
77+
// $message = Color::render((string)$message);
78+
$message = Style::global()->render((string)$message);
79+
return Readline::readline($message);
7480
}
7581

7682
// read from input stream
83+
if ($message) {
84+
Cli::write($message, $nl);
85+
}
86+
7787
$opts = array_merge([
7888
'length' => 1024,
7989
'stream' => self::$inputStream,

Diff for: src/Traits/WriteMessageTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static function write($messages, $nl = true, $quit = false, array $opts =
147147
$messages = (string)$messages;
148148

149149
if (!isset($opts['color']) || $opts['color']) {
150-
$messages = Style::instance()->render($messages);
150+
$messages = Style::global()->render($messages);
151151
} else {
152152
$messages = Style::stripColor($messages);
153153
}

Diff for: src/Util/Readline.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use function file_get_contents;
88
use function function_exists;
99
use function readline;
10+
use function readline_add_history;
1011
use function readline_callback_handler_install;
1112
use function readline_callback_handler_remove;
1213
use function readline_callback_read_char;
@@ -72,14 +73,14 @@ public static function moveToNewline(): void
7273
* return $matches;
7374
* }
7475
*
75-
* Readline::register($func);
76+
* Readline::registerCompleter($func);
7677
* ```
7778
*
7879
* @param callable $callback
7980
*
8081
* @return bool
8182
*/
82-
public static function register(callable $callback): bool
83+
public static function registerCompleter(callable $callback): bool
8384
{
8485
return readline_completion_function($callback);
8586
}
@@ -192,14 +193,28 @@ public static function listHistory(): array
192193
return $list;
193194
}
194195

196+
/**
197+
* @param string $input
198+
*
199+
* @return bool
200+
*/
201+
public static function addHistory(string $input): bool
202+
{
203+
return readline_add_history($input);
204+
}
205+
195206
/**
196207
* @param string $filepath
197208
*
198209
* @return bool
199210
*/
200211
public static function loadHistory(string $filepath): bool
201212
{
202-
return readline_read_history($filepath);
213+
if ($filepath) {
214+
return readline_read_history($filepath);
215+
}
216+
217+
return false;
203218
}
204219

205220
/**
@@ -209,7 +224,11 @@ public static function loadHistory(string $filepath): bool
209224
*/
210225
public static function dumpHistory(string $filepath): bool
211226
{
212-
return readline_write_history($filepath);
227+
if ($filepath) {
228+
return readline_write_history($filepath);
229+
}
230+
231+
return false;
213232
}
214233

215234
/**

0 commit comments

Comments
 (0)