Skip to content

Commit ef78f23

Browse files
committed
update some for terminal op
1 parent 8a5bd50 commit ef78f23

File tree

7 files changed

+406
-80
lines changed

7 files changed

+406
-80
lines changed

README.md

+49-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
Provide some useful utils for the php CLI application.
99

10-
- Parse CLI arguments and options
11-
- Console color render
10+
- Simple CLI arguments and options parser.
11+
- Terminal console color render
1212
- CLI code highlighter
1313
- Build simple CLI application
1414
- CLI ENV information helper
@@ -33,6 +33,49 @@ echo Color::render('hello world', 'success');
3333

3434
![colors](example/terminal-color.png)
3535

36+
## Terminal control
37+
38+
examples:
39+
40+
```php
41+
use \Toolkit\Cli\Util\Terminal;
42+
43+
Terminal::forward(3);
44+
Terminal::backward(2);
45+
46+
Terminal::clearLine();
47+
48+
Terminal::clearScreen();
49+
```
50+
51+
### Control Methods
52+
53+
```php
54+
/**
55+
* @method static showCursor()
56+
* @method static hideCursor()
57+
* @method static savePosition()
58+
* @method static restorePosition()
59+
* @method static toTop()
60+
* @method static toColumn(int $step)
61+
* @method static up(int $step = 1)
62+
* @method static down(int $step = 1)
63+
* @method static forward(int $step = 1)
64+
* @method static backward(int $step = 1) Moves the terminal cursor backward
65+
* @method static toPrevNLineStart(int $step = 1)
66+
* @method static toNextNLineStart(int $step = 1)
67+
* @method static coordinate(int $col, int $row = 0)
68+
* @method static clearScreen()
69+
* @method static clearLine()
70+
* @method static clearToScreenBegin()
71+
* @method static clearToScreenEnd()
72+
* @method static scrollUp(int $step = 1)
73+
* @method static scrollDown(int $step = 1)
74+
* @method static showSecondaryScreen()
75+
* @method static showPrimaryScreen()
76+
*/
77+
```
78+
3679
## PHP file highlight
3780

3881
> This is inspire `jakub-onderka/php-console-highlighter`
@@ -173,6 +216,10 @@ $down->start();
173216

174217
- https://github.com/inhere/php-console Build rich console application
175218

219+
## Refer
220+
221+
- https://www.sitepoint.com/howd-they-do-it-phpsnake-detecting-keypresses/
222+
176223
## License
177224

178225
[MIT](LICENSE)

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"php": ">7.1.0"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5"
25+
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
26+
"ext-posix": "*",
27+
"ext-readline": "*"
2628
},
2729
"autoload": {
2830
"psr-4": {

example/keyboard.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
function test_run()
5+
{
6+
echo "Hello, I am snake!";
7+
8+
system('stty cbreak -echo');
9+
$stdin = fopen('php://stdin', 'rb');
10+
11+
while (1) {
12+
$c = ord(fgetc($stdin));
13+
14+
echo "Char read: $c\n";
15+
}
16+
}
17+
18+
require dirname(__DIR__) . '/test/bootstrap.php';
19+
20+
test_run();
21+

example/term_move.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
class TermMove {
4+
5+
const KEYS = [
6+
10 => 'enter',
7+
127 => 'backspace',
8+
65 => 'up',
9+
66 => 'down',
10+
67 => 'right',
11+
68 => 'left',
12+
9 => 'tab'
13+
];
14+
15+
public function readChar() : string
16+
{
17+
$c = $this->char();
18+
19+
var_dump($c);
20+
$last = '';
21+
foreach (str_split($c) as $one) {
22+
var_dump($one . ' => num: ' . ord($one));
23+
24+
$last = $one;
25+
}
26+
27+
// var_dump($c);
28+
if (ctype_print($last)) {
29+
return $last;
30+
}
31+
32+
$n = ord($last);
33+
if (
34+
array_key_exists($n, static::KEYS)
35+
&& in_array(static::KEYS[$n], ['enter', 'backspace'])
36+
) {
37+
return static::KEYS[$n];
38+
}
39+
40+
return '';
41+
}
42+
43+
public function char() : string
44+
{
45+
// return fread(STDIN, 4);
46+
47+
if (DIRECTORY_SEPARATOR === "\\") {
48+
$c = stream_get_contents(STDIN, 1);
49+
50+
return $c;
51+
}
52+
53+
readline_callback_handler_install('', function() {});
54+
// $c = $this->read(1);
55+
// $c = fread(STDIN, 4);
56+
$c = stream_get_contents(STDIN, 4);
57+
// $c = readline();
58+
readline_callback_handler_remove();
59+
return $c;
60+
}
61+
}
62+
63+
require dirname(__DIR__) . '/test/bootstrap.php';
64+
65+
$tm = new TermMove();
66+
67+
echo "please input:";
68+
$c = $tm->readChar();
69+
echo "CHAR: $c\n";

src/Util/Keyboard.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace Toolkit\Cli\Util;
5+
6+
/**
7+
* Class Keyboard
8+
*
9+
* @package Toolkit\Cli\Util
10+
*/
11+
class Keyboard
12+
{
13+
/**
14+
* @var self
15+
*/
16+
private static $global;
17+
18+
/**
19+
* @return static
20+
*/
21+
public static function global(): self
22+
{
23+
if (!self::$global) {
24+
self::$global = new self;
25+
}
26+
27+
return self::$global;
28+
}
29+
30+
/**
31+
* @return static
32+
*/
33+
public static function new(): self
34+
{
35+
return new self;
36+
}
37+
38+
}

src/Util/Readline.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static function removeHandler(): bool
109109
/**
110110
* Reads a character and informs the readline callback interface when a line is received
111111
*/
112-
public static function readChar(): void
112+
public static function callbackReadChar(): void
113113
{
114114
// 当一个行被接收时读取一个字符并且通知 readline 调用回调函数
115115
readline_callback_read_char();

0 commit comments

Comments
 (0)