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\Output\ShellOutput;
|
Chris@13
|
15 use Symfony\Component\Console\Input\InputInterface;
|
Chris@13
|
16 use Symfony\Component\Console\Input\InputOption;
|
Chris@13
|
17 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@13
|
18
|
Chris@13
|
19 /**
|
Chris@13
|
20 * Interact with the current code buffer.
|
Chris@13
|
21 *
|
Chris@13
|
22 * Shows and clears the buffer for the current multi-line expression.
|
Chris@13
|
23 */
|
Chris@13
|
24 class BufferCommand extends Command
|
Chris@13
|
25 {
|
Chris@13
|
26 /**
|
Chris@13
|
27 * {@inheritdoc}
|
Chris@13
|
28 */
|
Chris@13
|
29 protected function configure()
|
Chris@13
|
30 {
|
Chris@13
|
31 $this
|
Chris@13
|
32 ->setName('buffer')
|
Chris@13
|
33 ->setAliases(['buf'])
|
Chris@13
|
34 ->setDefinition([
|
Chris@13
|
35 new InputOption('clear', '', InputOption::VALUE_NONE, 'Clear the current buffer.'),
|
Chris@13
|
36 ])
|
Chris@13
|
37 ->setDescription('Show (or clear) the contents of the code input buffer.')
|
Chris@13
|
38 ->setHelp(
|
Chris@13
|
39 <<<'HELP'
|
Chris@13
|
40 Show the contents of the code buffer for the current multi-line expression.
|
Chris@13
|
41
|
Chris@13
|
42 Optionally, clear the buffer by passing the <info>--clear</info> option.
|
Chris@13
|
43 HELP
|
Chris@13
|
44 );
|
Chris@13
|
45 }
|
Chris@13
|
46
|
Chris@13
|
47 /**
|
Chris@13
|
48 * {@inheritdoc}
|
Chris@13
|
49 */
|
Chris@13
|
50 protected function execute(InputInterface $input, OutputInterface $output)
|
Chris@13
|
51 {
|
Chris@13
|
52 $buf = $this->getApplication()->getCodeBuffer();
|
Chris@13
|
53 if ($input->getOption('clear')) {
|
Chris@13
|
54 $this->getApplication()->resetCodeBuffer();
|
Chris@13
|
55 $output->writeln($this->formatLines($buf, 'urgent'), ShellOutput::NUMBER_LINES);
|
Chris@13
|
56 } else {
|
Chris@13
|
57 $output->writeln($this->formatLines($buf), ShellOutput::NUMBER_LINES);
|
Chris@13
|
58 }
|
Chris@13
|
59 }
|
Chris@13
|
60
|
Chris@13
|
61 /**
|
Chris@13
|
62 * A helper method for wrapping buffer lines in `<urgent>` and `<return>` formatter strings.
|
Chris@13
|
63 *
|
Chris@13
|
64 * @param array $lines
|
Chris@13
|
65 * @param string $type (default: 'return')
|
Chris@13
|
66 *
|
Chris@13
|
67 * @return array Formatted strings
|
Chris@13
|
68 */
|
Chris@13
|
69 protected function formatLines(array $lines, $type = 'return')
|
Chris@13
|
70 {
|
Chris@17
|
71 $template = \sprintf('<%s>%%s</%s>', $type, $type);
|
Chris@13
|
72
|
Chris@17
|
73 return \array_map(function ($line) use ($template) {
|
Chris@17
|
74 return \sprintf($template, $line);
|
Chris@13
|
75 }, $lines);
|
Chris@13
|
76 }
|
Chris@13
|
77 }
|