4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Readline;
15 * A Readline interface implementation for GNU Readline.
17 * This is by far the coolest way to do it, but it doesn't work with new PHP.
21 class GNUReadline implements Readline
23 /** @var string|false */
24 protected $historyFile;
26 protected $historySize;
31 * GNU Readline is supported iff `readline_list_history` is defined. PHP
32 * decided it would be awesome to swap out GNU Readline for Libedit, but
33 * they ended up shipping an incomplete implementation. So we've got this.
37 public static function isSupported()
39 return function_exists('readline_list_history');
43 * GNU Readline constructor.
45 * @param string|false $historyFile
46 * @param int $historySize
47 * @param bool $eraseDups
49 public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
51 $this->historyFile = ($historyFile !== null) ? $historyFile : false;
52 $this->historySize = $historySize;
53 $this->eraseDups = $eraseDups;
59 public function addHistory($line)
61 if ($res = readline_add_history($line)) {
62 $this->writeHistory();
71 public function clearHistory()
73 if ($res = readline_clear_history()) {
74 $this->writeHistory();
83 public function listHistory()
85 return readline_list_history();
91 public function readHistory()
93 // Workaround PHP bug #69054
95 // If open_basedir is set, readline_read_history() segfaults. This was fixed in 5.6.7:
97 // https://github.com/php/php-src/blob/423a057023ef3c00d2ffc16a6b43ba01d0f71796/NEWS#L19-L21
99 if (version_compare(PHP_VERSION, '5.6.7', '>=') || !ini_get('open_basedir')) {
100 readline_read_history();
102 readline_clear_history();
104 return readline_read_history($this->historyFile);
110 public function readline($prompt = null)
112 return readline($prompt);
118 public function redisplay()
120 readline_redisplay();
126 public function writeHistory()
128 // We have to write history first, since it is used
129 // by Libedit to list history
130 if ($this->historyFile !== false) {
131 $res = readline_write_history($this->historyFile);
136 if (!$res || !$this->eraseDups && !$this->historySize > 0) {
140 $hist = $this->listHistory();
145 if ($this->eraseDups) {
146 // flip-flip technique: removes duplicates, latest entries win.
147 $hist = array_flip(array_flip($hist));
148 // sort on keys to get the order back
152 if ($this->historySize > 0) {
153 $histsize = count($hist);
154 if ($histsize > $this->historySize) {
155 $hist = array_slice($hist, $histsize - $this->historySize);
159 readline_clear_history();
160 foreach ($hist as $line) {
161 readline_add_history($line);
164 if ($this->historyFile !== false) {
165 return readline_write_history($this->historyFile);