4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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;
14 use Psy\Exception\BreakException;
17 * An array-based Readline emulation implementation.
19 class Transient implements Readline
26 * Transient Readline is always supported.
30 public static function isSupported()
36 * Transient Readline constructor.
38 public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
40 // don't do anything with the history file...
41 $this->history = array();
42 $this->historySize = $historySize;
43 $this->eraseDups = $eraseDups;
49 public function addHistory($line)
51 if ($this->eraseDups) {
52 if (($key = array_search($line, $this->history)) !== false) {
53 unset($this->history[$key]);
57 $this->history[] = $line;
59 if ($this->historySize > 0) {
60 $histsize = count($this->history);
61 if ($histsize > $this->historySize) {
62 $this->history = array_slice($this->history, $histsize - $this->historySize);
66 $this->history = array_values($this->history);
74 public function clearHistory()
76 $this->history = array();
84 public function listHistory()
86 return $this->history;
92 public function readHistory()
100 * @throws BreakException if user hits Ctrl+D
104 public function readline($prompt = null)
108 return rtrim(fgets($this->getStdin(), 1024));
114 public function redisplay()
122 public function writeHistory()
128 * Get a STDIN file handle.
130 * @throws BreakException if user hits Ctrl+D
134 private function getStdin()
136 if (!isset($this->stdin)) {
137 $this->stdin = fopen('php://stdin', 'r');
140 if (feof($this->stdin)) {
141 throw new BreakException('Ctrl+D');