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\Command;
14 use Psy\Input\FilterOptions;
15 use Psy\Output\ShellOutput;
16 use Psy\Readline\Readline;
17 use Symfony\Component\Console\Formatter\OutputFormatter;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
23 * Psy Shell history command.
25 * Shows, searches and replays readline history. Not too shabby.
27 class HistoryCommand extends Command
34 public function __construct($name = null)
36 $this->filter = new FilterOptions();
38 parent::__construct($name);
42 * Set the Shell's Readline service.
44 * @param Readline $readline
46 public function setReadline(Readline $readline)
48 $this->readline = $readline;
54 protected function configure()
56 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
60 ->setAliases(array('hist'))
61 ->setDefinition(array(
62 new InputOption('show', 's', InputOption::VALUE_REQUIRED, 'Show the given range of lines'),
63 new InputOption('head', 'H', InputOption::VALUE_REQUIRED, 'Display the first N items.'),
64 new InputOption('tail', 'T', InputOption::VALUE_REQUIRED, 'Display the last N items.'),
70 new InputOption('no-numbers', 'N', InputOption::VALUE_NONE, 'Omit line numbers.'),
72 new InputOption('save', '', InputOption::VALUE_REQUIRED, 'Save history to a file.'),
73 new InputOption('replay', '', InputOption::VALUE_NONE, 'Replay'),
74 new InputOption('clear', '', InputOption::VALUE_NONE, 'Clear the history.'),
76 ->setDescription('Show the Psy Shell history.')
79 Show, search, save or replay the Psy Shell history.
82 <return>>>> history --grep /[bB]acon/</return>
83 <return>>>> history --show 0..10 --replay</return>
84 <return>>>> history --clear</return>
85 <return>>>> history --tail 1000 --save somefile.txt</return>
93 protected function execute(InputInterface $input, OutputInterface $output)
95 $this->validateOnlyOne($input, array('show', 'head', 'tail'));
96 $this->validateOnlyOne($input, array('save', 'replay', 'clear'));
98 $history = $this->getHistorySlice(
99 $input->getOption('show'),
100 $input->getOption('head'),
101 $input->getOption('tail')
103 $highlighted = false;
105 $this->filter->bind($input);
106 if ($this->filter->hasFilter()) {
108 $highlighted = array();
109 foreach ($history as $i => $line) {
110 if ($this->filter->match($line, $matches)) {
111 if (isset($matches[0])) {
112 $chunks = explode($matches[0], $history[$i]);
113 $chunks = array_map(array(__CLASS__, 'escape'), $chunks);
114 $glue = sprintf('<urgent>%s</urgent>', self::escape($matches[0]));
116 $highlighted[$i] = implode($glue, $chunks);
124 if ($save = $input->getOption('save')) {
125 $output->writeln(sprintf('Saving history in %s...', $save));
126 file_put_contents($save, implode(PHP_EOL, $history) . PHP_EOL);
127 $output->writeln('<info>History saved.</info>');
128 } elseif ($input->getOption('replay')) {
129 if (!($input->getOption('show') || $input->getOption('head') || $input->getOption('tail'))) {
130 throw new \InvalidArgumentException('You must limit history via --head, --tail or --show before replaying.');
133 $count = count($history);
134 $output->writeln(sprintf('Replaying %d line%s of history', $count, ($count !== 1) ? 's' : ''));
135 $this->getApplication()->addInput($history);
136 } elseif ($input->getOption('clear')) {
137 $this->clearHistory();
138 $output->writeln('<info>History cleared.</info>');
140 $type = $input->getOption('no-numbers') ? 0 : ShellOutput::NUMBER_LINES;
142 $type = $type | ShellOutput::OUTPUT_RAW;
145 $output->page($highlighted ?: $history, $type);
150 * Extract a range from a string.
152 * @param string $range
154 * @return array [ start, end ]
156 private function extractRange($range)
158 if (preg_match('/^\d+$/', $range)) {
159 return array($range, $range + 1);
163 if ($range !== '..' && preg_match('/^(\d*)\.\.(\d*)$/', $range, $matches)) {
164 $start = $matches[1] ? intval($matches[1]) : 0;
165 $end = $matches[2] ? intval($matches[2]) + 1 : PHP_INT_MAX;
167 return array($start, $end);
170 throw new \InvalidArgumentException('Unexpected range: ' . $range);
174 * Retrieve a slice of the readline history.
176 * @param string $show
177 * @param string $head
178 * @param string $tail
180 * @return array A slilce of history
182 private function getHistorySlice($show, $head, $tail)
184 $history = $this->readline->listHistory();
186 // don't show the current `history` invocation
190 list($start, $end) = $this->extractRange($show);
191 $length = $end - $start;
193 if (!preg_match('/^\d+$/', $head)) {
194 throw new \InvalidArgumentException('Please specify an integer argument for --head.');
198 $length = intval($head);
200 if (!preg_match('/^\d+$/', $tail)) {
201 throw new \InvalidArgumentException('Please specify an integer argument for --tail.');
204 $start = count($history) - $tail;
205 $length = intval($tail) + 1;
210 return array_slice($history, $start, $length, true);
214 * Validate that only one of the given $options is set.
216 * @param InputInterface $input
217 * @param array $options
219 private function validateOnlyOne(InputInterface $input, array $options)
222 foreach ($options as $opt) {
223 if ($input->getOption($opt)) {
229 throw new \InvalidArgumentException('Please specify only one of --' . implode(', --', $options));
234 * Clear the readline history.
236 private function clearHistory()
238 $this->readline->clearHistory();
241 public static function escape($string)
243 return OutputFormatter::escape($string);