4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
14 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
15 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
16 use Symfony\Component\Console\Output\ConsoleOutput;
19 * A ConsoleOutput subclass specifically for Psy Shell output.
21 class ShellOutput extends ConsoleOutput
23 const NUMBER_LINES = 128;
29 * Construct a ShellOutput instance.
31 * @param mixed $verbosity (default: self::VERBOSITY_NORMAL)
32 * @param bool $decorated (default: null)
33 * @param OutputFormatterInterface $formatter (default: null)
34 * @param null|string|OutputPager $pager (default: null)
36 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null, $pager = null)
38 parent::__construct($verbosity, $decorated, $formatter);
40 $this->initFormatters();
42 if ($pager === null) {
43 $this->pager = new PassthruPager($this);
44 } elseif (is_string($pager)) {
45 $this->pager = new ProcOutputPager($this, $pager);
46 } elseif ($pager instanceof OutputPager) {
47 $this->pager = $pager;
49 throw new \InvalidArgumentException('Unexpected pager parameter: ' . $pager);
54 * Page multiple lines of output.
56 * The output pager is started
58 * If $messages is callable, it will be called, passing this output instance
59 * for rendering. Otherwise, all passed $messages are paged to output.
61 * Upon completion, the output pager is flushed.
63 * @param string|array|\Closure $messages A string, array of strings or a callback
64 * @param int $type (default: 0)
66 public function page($messages, $type = 0)
68 if (is_string($messages)) {
69 $messages = (array) $messages;
72 if (!is_array($messages) && !is_callable($messages)) {
73 throw new \InvalidArgumentException('Paged output requires a string, array or callback');
78 if (is_callable($messages)) {
81 $this->write($messages, true, $type);
88 * Start sending output to the output pager.
90 public function startPaging()
96 * Stop paging output and flush the output pager.
98 public function stopPaging()
105 * Writes a message to the output.
107 * Optionally, pass `$type | self::NUMBER_LINES` as the $type parameter to
108 * number the lines of output.
110 * @throws \InvalidArgumentException When unknown output type is given
112 * @param string|array $messages The message as an array of lines or a single string
113 * @param bool $newline Whether to add a newline or not
114 * @param int $type The type of output
116 public function write($messages, $newline = false, $type = 0)
118 if ($this->getVerbosity() === self::VERBOSITY_QUIET) {
122 $messages = (array) $messages;
124 if ($type & self::NUMBER_LINES) {
125 $pad = strlen((string) count($messages));
126 $template = $this->isDecorated() ? "<aside>%{$pad}s</aside>: %s" : "%{$pad}s: %s";
128 if ($type & self::OUTPUT_RAW) {
129 $messages = array_map(['Symfony\Component\Console\Formatter\OutputFormatter', 'escape'], $messages);
132 foreach ($messages as $i => $line) {
133 $messages[$i] = sprintf($template, $i, $line);
136 // clean this up for super.
137 $type = $type & ~self::NUMBER_LINES & ~self::OUTPUT_RAW;
140 parent::write($messages, $newline, $type);
144 * Writes a message to the output.
146 * Handles paged output, or writes directly to the output stream.
148 * @param string $message A message to write to the output
149 * @param bool $newline Whether to add a newline or not
151 public function doWrite($message, $newline)
153 if ($this->paging > 0) {
154 $this->pager->doWrite($message, $newline);
156 parent::doWrite($message, $newline);
161 * Flush and close the output pager.
163 private function closePager()
165 if ($this->paging <= 0) {
166 $this->pager->close();
171 * Initialize output formatter styles.
173 private function initFormatters()
175 $formatter = $this->getFormatter();
177 $formatter->setStyle('warning', new OutputFormatterStyle('black', 'yellow'));
178 $formatter->setStyle('error', new OutputFormatterStyle('black', 'red', ['bold']));
179 $formatter->setStyle('aside', new OutputFormatterStyle('blue'));
180 $formatter->setStyle('strong', new OutputFormatterStyle(null, null, ['bold']));
181 $formatter->setStyle('return', new OutputFormatterStyle('cyan'));
182 $formatter->setStyle('urgent', new OutputFormatterStyle('red'));
183 $formatter->setStyle('hidden', new OutputFormatterStyle('black'));
186 $formatter->setStyle('public', new OutputFormatterStyle(null, null, ['bold']));
187 $formatter->setStyle('protected', new OutputFormatterStyle('yellow'));
188 $formatter->setStyle('private', new OutputFormatterStyle('red'));
189 $formatter->setStyle('global', new OutputFormatterStyle('cyan', null, ['bold']));
190 $formatter->setStyle('const', new OutputFormatterStyle('cyan'));
191 $formatter->setStyle('class', new OutputFormatterStyle('blue', null, ['underscore']));
192 $formatter->setStyle('function', new OutputFormatterStyle(null));
193 $formatter->setStyle('default', new OutputFormatterStyle(null));
196 $formatter->setStyle('number', new OutputFormatterStyle('magenta'));
197 $formatter->setStyle('string', new OutputFormatterStyle('green'));
198 $formatter->setStyle('bool', new OutputFormatterStyle('cyan'));
199 $formatter->setStyle('keyword', new OutputFormatterStyle('yellow'));
200 $formatter->setStyle('comment', new OutputFormatterStyle('blue'));
201 $formatter->setStyle('object', new OutputFormatterStyle('blue'));
202 $formatter->setStyle('resource', new OutputFormatterStyle('yellow'));