comparison vendor/psy/psysh/src/Psy/Command/BufferCommand.php @ 0:4c8ae668cc8c

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