Skip to content

Commit e69aea0

Browse files
committed
update, will add command autocomplete
1 parent 649c9cc commit e69aea0

File tree

7 files changed

+188
-0
lines changed

7 files changed

+188
-0
lines changed

examples/baks/.interactive_history

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_HiStOrY_V2_
2+
3+
df-af\040

examples/baks/auto_complete.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-08-11
6+
* Time: 15:47
7+
*/
8+
9+
readline_completion_function('your_callback');
10+
11+
function your_callback($input, $index) {
12+
// Get info about the current buffer
13+
$rl_info = readline_info();
14+
15+
// Figure out what the entire input is
16+
// $full_input = substr($rl_info['line_buffer'], 0, $rl_info['end']);
17+
18+
//var_dump($input, $index, $rl_info);die;
19+
$matches = array();
20+
21+
// Get all matches based on the entire input buffer
22+
foreach (['df', 'df-af', 'df-bb'] as $phrase) {
23+
// Only add the end of the input (where this word begins)
24+
// to the matches array
25+
//$matches[] = substr($phrase, $index);
26+
$matches[] = $phrase;
27+
}
28+
29+
return $matches;
30+
}
31+
32+
$ret = readline('>>');
33+
34+
if ($ret) {
35+
readline_add_history($ret);
36+
echo "Your input: $ret\n";
37+
} else {
38+
echo "NO INPUT\n";
39+
}
40+

examples/baks/readline.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-08-11
6+
* Time: 14:11
7+
*/
8+
9+
printf("readline_list_history() exists: %s\n" , function_exists('readline_list_history') ? 'yes' : 'no');
10+
11+
if (!function_exists('readline')) {
12+
function readline($prefix) {
13+
echo $prefix;
14+
return stream_get_line(STDIN, 1024, PHP_EOL);
15+
}
16+
}
17+
18+
$line = readline('>>> ');
19+
20+
if (!empty($line)) {
21+
readline_add_history($line);
22+
23+
//dump history
24+
// print_r(readline_list_history());
25+
26+
//dump variables
27+
print_r(readline_info());
28+
}

examples/baks/rl_callback.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-08-11
6+
* Time: 15:37
7+
*/
8+
9+
function rl_callback($ret)
10+
{
11+
global $c, $prompting;
12+
13+
echo "You entered: $ret\n";
14+
$c++;
15+
16+
if ($c > 10) {
17+
$prompting = false;
18+
readline_callback_handler_remove();
19+
} else {
20+
readline_callback_handler_install("[$c] Enter something: ", 'rl_callback');
21+
}
22+
}
23+
24+
$c = 1;
25+
$prompting = true;
26+
27+
readline_callback_handler_install("[$c] Enter something: ", 'rl_callback');
28+
29+
while ($prompting) {
30+
$w = NULL;
31+
$e = NULL;
32+
$n = stream_select($r = array(STDIN), $w, $e, null);
33+
34+
if ($n && in_array(STDIN, $r, true)) {
35+
// read a character, will call the callback when a newline is entered
36+
readline_callback_read_char();
37+
}
38+
}
39+
40+
echo "Prompting disabled. All done.\n";

examples/baks/rl_history.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-08-11
6+
* Time: 17:35
7+
*/
8+
9+
$history_file = __DIR__ . '/.interactive_history';
10+
11+
# read history from previous session
12+
if (is_file($history_file)) {
13+
readline_read_history($history_file);
14+
}
15+
16+
require __DIR__ . '/auto_complete.php';
17+
18+
# put this at the end of yur script to save history and take care of $_SERVER['HISTSIZE']
19+
if (!readline_write_history($history_file)) {
20+
exit("write history to file is failed!\n");
21+
}
22+
23+
if (!function_exists('readline_list_history')) {
24+
echo "readline_list_history() not exists\n";
25+
exit;
26+
}
27+
28+
# clean history if too long
29+
$hist = readline_list_history();
30+
31+
if (($histSize = count($hist)) > $_SERVER['HISTSIZE']) {
32+
$hist = array_slice($hist, $histSize - $_SERVER['HISTSIZE']);
33+
34+
# in php5 you can replaces thoose line with a file_puts_content()
35+
if ($fp = fopen($history_file, 'wb')) {
36+
fwrite($fp, implode("\n", $hist));
37+
fclose($fp);
38+
}
39+
}

src/style/Highlighter.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-08-11
6+
* Time: 15:50
7+
*/
8+
9+
namespace inhere\console\style;
10+
11+
/**
12+
* Class Highlighter
13+
* @package inhere\console\style
14+
*
15+
* @referrer jakub-onderka/php-console-highlighter
16+
*/
17+
class Highlighter
18+
{
19+
20+
}

src/utils/AutoCompleter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-08-11
6+
* Time: 17:56
7+
*/
8+
9+
namespace inhere\console\utils;
10+
11+
/**
12+
* Class AutoCompleter
13+
* @package inhere\console\utils
14+
*/
15+
class AutoCompleter
16+
{
17+
18+
}

0 commit comments

Comments
 (0)