forked from onovy/otodo
/
ReadLine.php
271 lines (252 loc) · 6.85 KB
/
ReadLine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/*
Copyright 2014 Ondrej Novy
This file is part of otodo.
otodo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
otodo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with otodo. If not, see <http://www.gnu.org/licenses/>.
*/
class ReadLine {
const INIT = 0;
const READ = 1;
const END = 2;
const ESC_033 = 3; // \033
const ESC_033B = 5; // \033[
private $input = '';
private $line = '';
private $history = array();
private $historyPos = null;
private $historyNew = null;
private $pos = 0;
private $state = self::INIT;
private $prefix = '';
public $timeout = false;
private $completitionCallback = null;
private $completition = '';
public function setCompletitionCallback(callable $callback) {
$this->completitionCallback = $callback;
}
public function historyAdd($line) {
$line = trim($line);
if ($line === '') {
return;
}
if (end($this->history) == $line) {
return;
}
$this->history[] = $line;
}
public function historyLoad($file) {
if (file_exists($file)) {
$this->history = @unserialize(file_get_contents($file));
if ($this->history === FALSE) {
$phpError = error_get_last();
throw new HistoryLoadException('Can\'t load history file: ' .
$phpError['message']);
} elseif (!is_array($this->history)) {
throw new HistoryLoadException('Can\'t load history file: Wrong format');
}
} else {
$this->history = array();
}
}
public function historySave($file) {
file_put_contents($file, serialize($this->history));
}
public function read($prefix = '', $prefill = '', $timeout = null) {
if ($timeout && $this->timeout) {
$this->timeout = false;
} else {
$this->historyPos = null;
$this->historyNew = null;
$this->line = $prefill;
$this->pos = strlen($this->line);
$this->completition = '';
}
$this->state = self::READ;
$this->prefix = $prefix;
$this->repaint();
system('stty -icanon -echo');
while (true) {
$read = array(STDIN);
$write = array();
$except = array();
$count = stream_select($read, $write, $except, $timeout ? $timeout : null);
if (!$count) {
if ($timeout) {
$this->timeout = true;
break;
}
continue;
}
do {
$this->input .= fread(STDIN, 256);
$read = array(STDIN);
$write = array();
$except = array();
} while (stream_select($read, $write, $except, 0));
$this->parseInput();
$this->repaint();
if ($this->state == self::END) {
break;
}
}
system('stty sane');
echo PHP_EOL;
echo "\033[K";
return trim($this->line);
}
private function repaint() {
echo PHP_EOL;
echo "\033[K";
echo $this->completition;
echo "\033[1A";
echo "\033[9999D";
echo "\033[K";
echo $this->prefix;
echo $this->line;
$l = mb_strlen($this->line) - $this->pos;
if ($l) {
echo "\033[" . $l . "D";
}
}
private function parseInput() {
for ($i = 0 ; $i < mb_strlen($this->input) ; $i++) {
$ch = mb_substr($this->input, $i, 1);
switch ($this->state) {
case self::READ:
switch ($ch) {
case "\n":
$this->state = self::END;
break 3;
break;
case "\177": // Backspace
if ($this->pos > 0) {
if ($this->pos == mb_strlen($this->line)) {
$this->line = mb_substr($this->line, 0, -1);
} else {
$this->line =
mb_substr($this->line, 0, $this->pos - 1) .
mb_substr($this->line, $this->pos);
}
$this->pos--;
}
break;
case "\t": // Tab
if (!is_null($this->completitionCallback)) {
$search = mb_substr($this->line, 0, $this->pos);
$pos = strrpos($search, ' ');
if ($pos !== false) {
$search = mb_substr($search, $pos + 1);
}
$ar = call_user_func($this->completitionCallback, $search);
$this->completition = null;
if (count($ar) == 1) {
if ($pos === false) {
$this->line =
$ar[0] .
mb_substr($this->line, mb_strlen($search));
} else {
$this->line =
mb_substr($this->line, 0, $pos + 1) .
$ar[0] .
mb_substr($this->line, $pos + mb_strlen($search) + 1);
}
$this->pos += mb_strlen($ar[0]) - mb_strlen($search);
} elseif (count($ar) > 1) {
$this->completition = implode(' ', $ar);
}
}
break;
case "\033":
$this->state = self::ESC_033;
break;
case "\001": // Ctrl+A
$this->pos = 0;
break;
case "\005": // Ctrl+E
$this->pos = mb_strlen($this->line);
break;
default:
if (strlen($ch) == 1 && (ord($ch) < 32 || ord($ch) > 126)) {
break;
}
if ($this->pos == mb_strlen($this->line)) {
$this->line .= $ch;
} else {
$this->line =
mb_substr($this->line, 0, $this->pos) .
$ch .
mb_substr($this->line, $this->pos);
}
$this->pos++;
break;
}
break;
case self::ESC_033:
switch ($ch) {
case '[':
$this->state = self::ESC_033B;
break;
}
break;
case self::ESC_033B:
switch ($ch) {
case 'A': // Up
$this->state = self::READ;
if ($this->historyPos === null) {
if ($this->line === '' && $this->historyNew !== null) {
$this->line = $this->historyNew;
$this->pos = mb_strlen($this->line);
continue;
} else {
if ($this->line != '') {
$this->historyNew = $this->line;
}
$this->historyPos = count($this->history) - 1;
}
} elseif ($this->historyPos > 0) {
$this->historyPos--;
}
$this->line = $this->history[$this->historyPos];
$this->pos = mb_strlen($this->line);
break;
case 'B': // Down
$this->state = self::READ;
if ($this->historyPos === null) {
$this->line = '';
} elseif ($this->historyPos < count($this->history) - 1) {
$this->historyPos++;
$this->line = $this->history[$this->historyPos];
} else {
$this->historyPos = null;
$this->line = $this->historyNew;
}
$this->pos = mb_strlen($this->line);
break;
case 'C': // Right
$this->state = self::READ;
if (mb_strlen($this->line) > $this->pos) {
$this->pos++;
}
break;
case 'D': // Left
$this->state = self::READ;
if ($this->pos > 0) {
$this->pos--;
}
break;
}
break;
}
}
$this->input = mb_substr($this->input, $i + 1);
}
}