annotate vendor/psy/psysh/src/Command/HistoryCommand.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
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\Command;
Chris@13 13
Chris@13 14 use Psy\Input\FilterOptions;
Chris@13 15 use Psy\Output\ShellOutput;
Chris@13 16 use Psy\Readline\Readline;
Chris@13 17 use Symfony\Component\Console\Formatter\OutputFormatter;
Chris@13 18 use Symfony\Component\Console\Input\InputInterface;
Chris@13 19 use Symfony\Component\Console\Input\InputOption;
Chris@13 20 use Symfony\Component\Console\Output\OutputInterface;
Chris@13 21
Chris@13 22 /**
Chris@13 23 * Psy Shell history command.
Chris@13 24 *
Chris@13 25 * Shows, searches and replays readline history. Not too shabby.
Chris@13 26 */
Chris@13 27 class HistoryCommand extends Command
Chris@13 28 {
Chris@13 29 private $filter;
Chris@13 30 private $readline;
Chris@13 31
Chris@13 32 /**
Chris@13 33 * {@inheritdoc}
Chris@13 34 */
Chris@13 35 public function __construct($name = null)
Chris@13 36 {
Chris@13 37 $this->filter = new FilterOptions();
Chris@13 38
Chris@13 39 parent::__construct($name);
Chris@13 40 }
Chris@13 41
Chris@13 42 /**
Chris@13 43 * Set the Shell's Readline service.
Chris@13 44 *
Chris@13 45 * @param Readline $readline
Chris@13 46 */
Chris@13 47 public function setReadline(Readline $readline)
Chris@13 48 {
Chris@13 49 $this->readline = $readline;
Chris@13 50 }
Chris@13 51
Chris@13 52 /**
Chris@13 53 * {@inheritdoc}
Chris@13 54 */
Chris@13 55 protected function configure()
Chris@13 56 {
Chris@13 57 list($grep, $insensitive, $invert) = FilterOptions::getOptions();
Chris@13 58
Chris@13 59 $this
Chris@13 60 ->setName('history')
Chris@13 61 ->setAliases(['hist'])
Chris@13 62 ->setDefinition([
Chris@13 63 new InputOption('show', 's', InputOption::VALUE_REQUIRED, 'Show the given range of lines.'),
Chris@13 64 new InputOption('head', 'H', InputOption::VALUE_REQUIRED, 'Display the first N items.'),
Chris@13 65 new InputOption('tail', 'T', InputOption::VALUE_REQUIRED, 'Display the last N items.'),
Chris@13 66
Chris@13 67 $grep,
Chris@13 68 $insensitive,
Chris@13 69 $invert,
Chris@13 70
Chris@13 71 new InputOption('no-numbers', 'N', InputOption::VALUE_NONE, 'Omit line numbers.'),
Chris@13 72
Chris@13 73 new InputOption('save', '', InputOption::VALUE_REQUIRED, 'Save history to a file.'),
Chris@13 74 new InputOption('replay', '', InputOption::VALUE_NONE, 'Replay.'),
Chris@13 75 new InputOption('clear', '', InputOption::VALUE_NONE, 'Clear the history.'),
Chris@13 76 ])
Chris@13 77 ->setDescription('Show the Psy Shell history.')
Chris@13 78 ->setHelp(
Chris@13 79 <<<'HELP'
Chris@13 80 Show, search, save or replay the Psy Shell history.
Chris@13 81
Chris@13 82 e.g.
Chris@13 83 <return>>>> history --grep /[bB]acon/</return>
Chris@13 84 <return>>>> history --show 0..10 --replay</return>
Chris@13 85 <return>>>> history --clear</return>
Chris@13 86 <return>>>> history --tail 1000 --save somefile.txt</return>
Chris@13 87 HELP
Chris@13 88 );
Chris@13 89 }
Chris@13 90
Chris@13 91 /**
Chris@13 92 * {@inheritdoc}
Chris@13 93 */
Chris@13 94 protected function execute(InputInterface $input, OutputInterface $output)
Chris@13 95 {
Chris@13 96 $this->validateOnlyOne($input, ['show', 'head', 'tail']);
Chris@13 97 $this->validateOnlyOne($input, ['save', 'replay', 'clear']);
Chris@13 98
Chris@13 99 $history = $this->getHistorySlice(
Chris@13 100 $input->getOption('show'),
Chris@13 101 $input->getOption('head'),
Chris@13 102 $input->getOption('tail')
Chris@13 103 );
Chris@13 104 $highlighted = false;
Chris@13 105
Chris@13 106 $this->filter->bind($input);
Chris@13 107 if ($this->filter->hasFilter()) {
Chris@13 108 $matches = [];
Chris@13 109 $highlighted = [];
Chris@13 110 foreach ($history as $i => $line) {
Chris@13 111 if ($this->filter->match($line, $matches)) {
Chris@13 112 if (isset($matches[0])) {
Chris@17 113 $chunks = \explode($matches[0], $history[$i]);
Chris@17 114 $chunks = \array_map([__CLASS__, 'escape'], $chunks);
Chris@17 115 $glue = \sprintf('<urgent>%s</urgent>', self::escape($matches[0]));
Chris@13 116
Chris@17 117 $highlighted[$i] = \implode($glue, $chunks);
Chris@13 118 }
Chris@13 119 } else {
Chris@13 120 unset($history[$i]);
Chris@13 121 }
Chris@13 122 }
Chris@13 123 }
Chris@13 124
Chris@13 125 if ($save = $input->getOption('save')) {
Chris@17 126 $output->writeln(\sprintf('Saving history in %s...', $save));
Chris@17 127 \file_put_contents($save, \implode(PHP_EOL, $history) . PHP_EOL);
Chris@13 128 $output->writeln('<info>History saved.</info>');
Chris@13 129 } elseif ($input->getOption('replay')) {
Chris@13 130 if (!($input->getOption('show') || $input->getOption('head') || $input->getOption('tail'))) {
Chris@13 131 throw new \InvalidArgumentException('You must limit history via --head, --tail or --show before replaying');
Chris@13 132 }
Chris@13 133
Chris@17 134 $count = \count($history);
Chris@17 135 $output->writeln(\sprintf('Replaying %d line%s of history', $count, ($count !== 1) ? 's' : ''));
Chris@13 136 $this->getApplication()->addInput($history);
Chris@13 137 } elseif ($input->getOption('clear')) {
Chris@13 138 $this->clearHistory();
Chris@13 139 $output->writeln('<info>History cleared.</info>');
Chris@13 140 } else {
Chris@13 141 $type = $input->getOption('no-numbers') ? 0 : ShellOutput::NUMBER_LINES;
Chris@13 142 if (!$highlighted) {
Chris@13 143 $type = $type | ShellOutput::OUTPUT_RAW;
Chris@13 144 }
Chris@13 145
Chris@13 146 $output->page($highlighted ?: $history, $type);
Chris@13 147 }
Chris@13 148 }
Chris@13 149
Chris@13 150 /**
Chris@13 151 * Extract a range from a string.
Chris@13 152 *
Chris@13 153 * @param string $range
Chris@13 154 *
Chris@13 155 * @return array [ start, end ]
Chris@13 156 */
Chris@13 157 private function extractRange($range)
Chris@13 158 {
Chris@17 159 if (\preg_match('/^\d+$/', $range)) {
Chris@13 160 return [$range, $range + 1];
Chris@13 161 }
Chris@13 162
Chris@13 163 $matches = [];
Chris@17 164 if ($range !== '..' && \preg_match('/^(\d*)\.\.(\d*)$/', $range, $matches)) {
Chris@17 165 $start = $matches[1] ? \intval($matches[1]) : 0;
Chris@17 166 $end = $matches[2] ? \intval($matches[2]) + 1 : PHP_INT_MAX;
Chris@13 167
Chris@13 168 return [$start, $end];
Chris@13 169 }
Chris@13 170
Chris@13 171 throw new \InvalidArgumentException('Unexpected range: ' . $range);
Chris@13 172 }
Chris@13 173
Chris@13 174 /**
Chris@13 175 * Retrieve a slice of the readline history.
Chris@13 176 *
Chris@13 177 * @param string $show
Chris@13 178 * @param string $head
Chris@13 179 * @param string $tail
Chris@13 180 *
Chris@13 181 * @return array A slilce of history
Chris@13 182 */
Chris@13 183 private function getHistorySlice($show, $head, $tail)
Chris@13 184 {
Chris@13 185 $history = $this->readline->listHistory();
Chris@13 186
Chris@13 187 // don't show the current `history` invocation
Chris@17 188 \array_pop($history);
Chris@13 189
Chris@13 190 if ($show) {
Chris@13 191 list($start, $end) = $this->extractRange($show);
Chris@13 192 $length = $end - $start;
Chris@13 193 } elseif ($head) {
Chris@17 194 if (!\preg_match('/^\d+$/', $head)) {
Chris@13 195 throw new \InvalidArgumentException('Please specify an integer argument for --head');
Chris@13 196 }
Chris@13 197
Chris@13 198 $start = 0;
Chris@17 199 $length = \intval($head);
Chris@13 200 } elseif ($tail) {
Chris@17 201 if (!\preg_match('/^\d+$/', $tail)) {
Chris@13 202 throw new \InvalidArgumentException('Please specify an integer argument for --tail');
Chris@13 203 }
Chris@13 204
Chris@17 205 $start = \count($history) - $tail;
Chris@17 206 $length = \intval($tail) + 1;
Chris@13 207 } else {
Chris@13 208 return $history;
Chris@13 209 }
Chris@13 210
Chris@17 211 return \array_slice($history, $start, $length, true);
Chris@13 212 }
Chris@13 213
Chris@13 214 /**
Chris@13 215 * Validate that only one of the given $options is set.
Chris@13 216 *
Chris@13 217 * @param InputInterface $input
Chris@13 218 * @param array $options
Chris@13 219 */
Chris@13 220 private function validateOnlyOne(InputInterface $input, array $options)
Chris@13 221 {
Chris@13 222 $count = 0;
Chris@13 223 foreach ($options as $opt) {
Chris@13 224 if ($input->getOption($opt)) {
Chris@13 225 $count++;
Chris@13 226 }
Chris@13 227 }
Chris@13 228
Chris@13 229 if ($count > 1) {
Chris@17 230 throw new \InvalidArgumentException('Please specify only one of --' . \implode(', --', $options));
Chris@13 231 }
Chris@13 232 }
Chris@13 233
Chris@13 234 /**
Chris@13 235 * Clear the readline history.
Chris@13 236 */
Chris@13 237 private function clearHistory()
Chris@13 238 {
Chris@13 239 $this->readline->clearHistory();
Chris@13 240 }
Chris@13 241
Chris@13 242 public static function escape($string)
Chris@13 243 {
Chris@13 244 return OutputFormatter::escape($string);
Chris@13 245 }
Chris@13 246 }