Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\Readline;
|
Chris@13
|
13
|
Chris@13
|
14 use Psy\Util\Str;
|
Chris@13
|
15
|
Chris@13
|
16 /**
|
Chris@13
|
17 * A Libedit-based Readline implementation.
|
Chris@13
|
18 *
|
Chris@13
|
19 * This is largely the same as the Readline implementation, but it emulates
|
Chris@13
|
20 * support for `readline_list_history` since PHP decided it was a good idea to
|
Chris@13
|
21 * ship a fake Readline implementation that is missing history support.
|
Chris@13
|
22 */
|
Chris@13
|
23 class Libedit extends GNUReadline
|
Chris@13
|
24 {
|
Chris@13
|
25 /**
|
Chris@13
|
26 * Let's emulate GNU Readline by manually reading and parsing the history file!
|
Chris@13
|
27 *
|
Chris@13
|
28 * @return bool
|
Chris@13
|
29 */
|
Chris@13
|
30 public static function isSupported()
|
Chris@13
|
31 {
|
Chris@17
|
32 return \function_exists('readline') && !\function_exists('readline_list_history');
|
Chris@13
|
33 }
|
Chris@13
|
34
|
Chris@13
|
35 /**
|
Chris@13
|
36 * {@inheritdoc}
|
Chris@13
|
37 */
|
Chris@13
|
38 public function listHistory()
|
Chris@13
|
39 {
|
Chris@17
|
40 $history = \file_get_contents($this->historyFile);
|
Chris@13
|
41 if (!$history) {
|
Chris@13
|
42 return [];
|
Chris@13
|
43 }
|
Chris@13
|
44
|
Chris@13
|
45 // libedit doesn't seem to support non-unix line separators.
|
Chris@17
|
46 $history = \explode("\n", $history);
|
Chris@13
|
47
|
Chris@13
|
48 // shift the history signature, ensure it's valid
|
Chris@17
|
49 if (\array_shift($history) !== '_HiStOrY_V2_') {
|
Chris@13
|
50 return [];
|
Chris@13
|
51 }
|
Chris@13
|
52
|
Chris@13
|
53 // decode the line
|
Chris@17
|
54 $history = \array_map([$this, 'parseHistoryLine'], $history);
|
Chris@13
|
55 // filter empty lines & comments
|
Chris@17
|
56 return \array_values(\array_filter($history));
|
Chris@13
|
57 }
|
Chris@13
|
58
|
Chris@13
|
59 /**
|
Chris@13
|
60 * From GNUReadline (readline/histfile.c & readline/histexpand.c):
|
Chris@13
|
61 * lines starting with "\0" are comments or timestamps;
|
Chris@13
|
62 * if "\0" is found in an entry,
|
Chris@13
|
63 * everything from it until the next line is a comment.
|
Chris@13
|
64 *
|
Chris@13
|
65 * @param string $line The history line to parse
|
Chris@13
|
66 *
|
Chris@13
|
67 * @return string | null
|
Chris@13
|
68 */
|
Chris@13
|
69 protected function parseHistoryLine($line)
|
Chris@13
|
70 {
|
Chris@13
|
71 // empty line, comment or timestamp
|
Chris@13
|
72 if (!$line || $line[0] === "\0") {
|
Chris@13
|
73 return;
|
Chris@13
|
74 }
|
Chris@13
|
75 // if "\0" is found in an entry, then
|
Chris@13
|
76 // everything from it until the end of line is a comment.
|
Chris@17
|
77 if (($pos = \strpos($line, "\0")) !== false) {
|
Chris@17
|
78 $line = \substr($line, 0, $pos);
|
Chris@13
|
79 }
|
Chris@13
|
80
|
Chris@13
|
81 return ($line !== '') ? Str::unvis($line) : null;
|
Chris@13
|
82 }
|
Chris@13
|
83 }
|