Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / HistoryCommand.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Command;
13
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;
21
22 /**
23  * Psy Shell history command.
24  *
25  * Shows, searches and replays readline history. Not too shabby.
26  */
27 class HistoryCommand extends Command
28 {
29     private $filter;
30
31     /**
32      * {@inheritdoc}
33      */
34     public function __construct($name = null)
35     {
36         $this->filter = new FilterOptions();
37
38         parent::__construct($name);
39     }
40
41     /**
42      * Set the Shell's Readline service.
43      *
44      * @param Readline $readline
45      */
46     public function setReadline(Readline $readline)
47     {
48         $this->readline = $readline;
49     }
50
51     /**
52      * {@inheritdoc}
53      */
54     protected function configure()
55     {
56         list($grep, $insensitive, $invert) = FilterOptions::getOptions();
57
58         $this
59             ->setName('history')
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.'),
65
66                 $grep,
67                 $insensitive,
68                 $invert,
69
70                 new InputOption('no-numbers',  'N', InputOption::VALUE_NONE,     'Omit line numbers.'),
71
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.'),
75             ))
76             ->setDescription('Show the Psy Shell history.')
77             ->setHelp(
78                 <<<'HELP'
79 Show, search, save or replay the Psy Shell history.
80
81 e.g.
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>
86 HELP
87             );
88     }
89
90     /**
91      * {@inheritdoc}
92      */
93     protected function execute(InputInterface $input, OutputInterface $output)
94     {
95         $this->validateOnlyOne($input, array('show', 'head', 'tail'));
96         $this->validateOnlyOne($input, array('save', 'replay', 'clear'));
97
98         $history = $this->getHistorySlice(
99             $input->getOption('show'),
100             $input->getOption('head'),
101             $input->getOption('tail')
102         );
103         $highlighted = false;
104
105         $this->filter->bind($input);
106         if ($this->filter->hasFilter()) {
107             $matches     = array();
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]));
115
116                         $highlighted[$i] = implode($glue, $chunks);
117                     }
118                 } else {
119                     unset($history[$i]);
120                 }
121             }
122         }
123
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.');
131             }
132
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>');
139         } else {
140             $type = $input->getOption('no-numbers') ? 0 : ShellOutput::NUMBER_LINES;
141             if (!$highlighted) {
142                 $type = $type | ShellOutput::OUTPUT_RAW;
143             }
144
145             $output->page($highlighted ?: $history, $type);
146         }
147     }
148
149     /**
150      * Extract a range from a string.
151      *
152      * @param string $range
153      *
154      * @return array [ start, end ]
155      */
156     private function extractRange($range)
157     {
158         if (preg_match('/^\d+$/', $range)) {
159             return array($range, $range + 1);
160         }
161
162         $matches = array();
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;
166
167             return array($start, $end);
168         }
169
170         throw new \InvalidArgumentException('Unexpected range: ' . $range);
171     }
172
173     /**
174      * Retrieve a slice of the readline history.
175      *
176      * @param string $show
177      * @param string $head
178      * @param string $tail
179      *
180      * @return array A slilce of history
181      */
182     private function getHistorySlice($show, $head, $tail)
183     {
184         $history = $this->readline->listHistory();
185
186         // don't show the current `history` invocation
187         array_pop($history);
188
189         if ($show) {
190             list($start, $end) = $this->extractRange($show);
191             $length = $end - $start;
192         } elseif ($head) {
193             if (!preg_match('/^\d+$/', $head)) {
194                 throw new \InvalidArgumentException('Please specify an integer argument for --head.');
195             }
196
197             $start  = 0;
198             $length = intval($head);
199         } elseif ($tail) {
200             if (!preg_match('/^\d+$/', $tail)) {
201                 throw new \InvalidArgumentException('Please specify an integer argument for --tail.');
202             }
203
204             $start  = count($history) - $tail;
205             $length = intval($tail) + 1;
206         } else {
207             return $history;
208         }
209
210         return array_slice($history, $start, $length, true);
211     }
212
213     /**
214      * Validate that only one of the given $options is set.
215      *
216      * @param InputInterface $input
217      * @param array          $options
218      */
219     private function validateOnlyOne(InputInterface $input, array $options)
220     {
221         $count = 0;
222         foreach ($options as $opt) {
223             if ($input->getOption($opt)) {
224                 $count++;
225             }
226         }
227
228         if ($count > 1) {
229             throw new \InvalidArgumentException('Please specify only one of --' . implode(', --', $options));
230         }
231     }
232
233     /**
234      * Clear the readline history.
235      */
236     private function clearHistory()
237     {
238         $this->readline->clearHistory();
239     }
240
241     public static function escape($string)
242     {
243         return OutputFormatter::escape($string);
244     }
245 }