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