comparison vendor/psy/psysh/src/Psy/Output/ShellOutput.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\Output;
13
14 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
15 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
16 use Symfony\Component\Console\Output\ConsoleOutput;
17
18 /**
19 * A ConsoleOutput subclass specifically for Psy Shell output.
20 */
21 class ShellOutput extends ConsoleOutput
22 {
23 const NUMBER_LINES = 128;
24
25 private $paging = 0;
26 private $pager;
27
28 /**
29 * Construct a ShellOutput instance.
30 *
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)
35 */
36 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null, $pager = null)
37 {
38 parent::__construct($verbosity, $decorated, $formatter);
39
40 $this->initFormatters();
41
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;
48 } else {
49 throw new \InvalidArgumentException('Unexpected pager parameter: ' . $pager);
50 }
51 }
52
53 /**
54 * Page multiple lines of output.
55 *
56 * The output pager is started
57 *
58 * If $messages is callable, it will be called, passing this output instance
59 * for rendering. Otherwise, all passed $messages are paged to output.
60 *
61 * Upon completion, the output pager is flushed.
62 *
63 * @param string|array|\Closure $messages A string, array of strings or a callback
64 * @param int $type (default: 0)
65 */
66 public function page($messages, $type = 0)
67 {
68 if (is_string($messages)) {
69 $messages = (array) $messages;
70 }
71
72 if (!is_array($messages) && !is_callable($messages)) {
73 throw new \InvalidArgumentException('Paged output requires a string, array or callback.');
74 }
75
76 $this->startPaging();
77
78 if (is_callable($messages)) {
79 $messages($this);
80 } else {
81 $this->write($messages, true, $type);
82 }
83
84 $this->stopPaging();
85 }
86
87 /**
88 * Start sending output to the output pager.
89 */
90 public function startPaging()
91 {
92 $this->paging++;
93 }
94
95 /**
96 * Stop paging output and flush the output pager.
97 */
98 public function stopPaging()
99 {
100 $this->paging--;
101 $this->closePager();
102 }
103
104 /**
105 * Writes a message to the output.
106 *
107 * Optionally, pass `$type | self::NUMBER_LINES` as the $type parameter to
108 * number the lines of output.
109 *
110 * @throws \InvalidArgumentException When unknown output type is given
111 *
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
115 */
116 public function write($messages, $newline = false, $type = 0)
117 {
118 if ($this->getVerbosity() === self::VERBOSITY_QUIET) {
119 return;
120 }
121
122 $messages = (array) $messages;
123
124 if ($type & self::NUMBER_LINES) {
125 $pad = strlen((string) count($messages));
126 $template = $this->isDecorated() ? "<aside>%{$pad}s</aside>: %s" : "%{$pad}s: %s";
127
128 if ($type & self::OUTPUT_RAW) {
129 $messages = array_map(array('Symfony\Component\Console\Formatter\OutputFormatter', 'escape'), $messages);
130 }
131
132 foreach ($messages as $i => $line) {
133 $messages[$i] = sprintf($template, $i, $line);
134 }
135
136 // clean this up for super.
137 $type = $type & ~self::NUMBER_LINES & ~self::OUTPUT_RAW;
138 }
139
140 parent::write($messages, $newline, $type);
141 }
142
143 /**
144 * Writes a message to the output.
145 *
146 * Handles paged output, or writes directly to the output stream.
147 *
148 * @param string $message A message to write to the output
149 * @param bool $newline Whether to add a newline or not
150 */
151 public function doWrite($message, $newline)
152 {
153 if ($this->paging > 0) {
154 $this->pager->doWrite($message, $newline);
155 } else {
156 parent::doWrite($message, $newline);
157 }
158 }
159
160 /**
161 * Flush and close the output pager.
162 */
163 private function closePager()
164 {
165 if ($this->paging <= 0) {
166 $this->pager->close();
167 }
168 }
169
170 /**
171 * Initialize output formatter styles.
172 */
173 private function initFormatters()
174 {
175 $formatter = $this->getFormatter();
176
177 $formatter->setStyle('warning', new OutputFormatterStyle('black', 'yellow'));
178 $formatter->setStyle('error', new OutputFormatterStyle('black', 'red', array('bold')));
179 $formatter->setStyle('aside', new OutputFormatterStyle('blue'));
180 $formatter->setStyle('strong', new OutputFormatterStyle(null, null, array('bold')));
181 $formatter->setStyle('return', new OutputFormatterStyle('cyan'));
182 $formatter->setStyle('urgent', new OutputFormatterStyle('red'));
183 $formatter->setStyle('hidden', new OutputFormatterStyle('black'));
184
185 // Visibility
186 $formatter->setStyle('public', new OutputFormatterStyle(null, null, array('bold')));
187 $formatter->setStyle('protected', new OutputFormatterStyle('yellow'));
188 $formatter->setStyle('private', new OutputFormatterStyle('red'));
189 $formatter->setStyle('global', new OutputFormatterStyle('cyan', null, array('bold')));
190 $formatter->setStyle('const', new OutputFormatterStyle('cyan'));
191 $formatter->setStyle('class', new OutputFormatterStyle('blue', null, array('underscore')));
192 $formatter->setStyle('function', new OutputFormatterStyle(null));
193 $formatter->setStyle('default', new OutputFormatterStyle(null));
194
195 // Types
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'));
203 }
204 }