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\Output\ShellOutput;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Input\InputOption;
17 use Symfony\Component\Console\Output\OutputInterface;
20 * Interact with the current code buffer.
22 * Shows and clears the buffer for the current multi-line expression.
24 class BufferCommand extends Command
29 protected function configure()
33 ->setAliases(array('buf'))
34 ->setDefinition(array(
35 new InputOption('clear', '', InputOption::VALUE_NONE, 'Clear the current buffer.'),
37 ->setDescription('Show (or clear) the contents of the code input buffer.')
40 Show the contents of the code buffer for the current multi-line expression.
42 Optionally, clear the buffer by passing the <info>--clear</info> option.
50 protected function execute(InputInterface $input, OutputInterface $output)
52 $buf = $this->getApplication()->getCodeBuffer();
53 if ($input->getOption('clear')) {
54 $this->getApplication()->resetCodeBuffer();
55 $output->writeln($this->formatLines($buf, 'urgent'), ShellOutput::NUMBER_LINES);
57 $output->writeln($this->formatLines($buf), ShellOutput::NUMBER_LINES);
62 * A helper method for wrapping buffer lines in `<urgent>` and `<return>` formatter strings.
65 * @param string $type (default: 'return')
67 * @return array Formatted strings
69 protected function formatLines(array $lines, $type = 'return')
71 $template = sprintf('<%s>%%s</%s>', $type, $type);
73 return array_map(function ($line) use ($template) {
74 return sprintf($template, $line);