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;
14 use Psy\Exception\BreakException;
17 * An array-based Readline emulation implementation.
19 class Transient implements Readline
27 * Transient Readline is always supported.
31 public static function isSupported()
37 * Transient Readline constructor.
39 public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
41 // don't do anything with the history file...
43 $this->historySize = $historySize;
44 $this->eraseDups = $eraseDups;
50 public function addHistory($line)
52 if ($this->eraseDups) {
53 if (($key = array_search($line, $this->history)) !== false) {
54 unset($this->history[$key]);
58 $this->history[] = $line;
60 if ($this->historySize > 0) {
61 $histsize = count($this->history);
62 if ($histsize > $this->historySize) {
63 $this->history = array_slice($this->history, $histsize - $this->historySize);
67 $this->history = array_values($this->history);
75 public function clearHistory()
85 public function listHistory()
87 return $this->history;
93 public function readHistory()
101 * @throws BreakException if user hits Ctrl+D
105 public function readline($prompt = null)
109 return rtrim(fgets($this->getStdin(), 1024));
115 public function redisplay()
123 public function writeHistory()
129 * Get a STDIN file handle.
131 * @throws BreakException if user hits Ctrl+D
135 private function getStdin()
137 if (!isset($this->stdin)) {
138 $this->stdin = fopen('php://stdin', 'r');
141 if (feof($this->stdin)) {
142 throw new BreakException('Ctrl+D');