-
Notifications
You must be signed in to change notification settings - Fork 26
/
OutputStyle.php
197 lines (171 loc) · 4.44 KB
/
OutputStyle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Illuminate\Console;
use Illuminate\Console\Contracts\NewLineAware;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
class OutputStyle extends SymfonyStyle implements NewLineAware
{
/**
* The output instance.
*
* @var \Symfony\Component\Console\Output\OutputInterface
*/
private $output;
/**
* The number of trailing new lines written by the last output.
*
* This is initialized as 1 to account for the new line written by the shell after executing a command.
*
* @var int
*/
protected $newLinesWritten = 1;
/**
* If the last output written wrote a new line.
*
* @var bool
*
* @deprecated use $newLinesWritten
*/
protected $newLineWritten = false;
/**
* Create a new Console OutputStyle instance.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
public function __construct(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
parent::__construct($input, $output);
}
/**
* {@inheritdoc}
*/
#[\Override]
public function askQuestion(Question $question): mixed
{
try {
return parent::askQuestion($question);
} finally {
$this->newLinesWritten++;
}
}
/**
* {@inheritdoc}
*/
#[\Override]
public function write(string|iterable $messages, bool $newline = false, int $options = 0): void
{
$this->newLinesWritten = $this->trailingNewLineCount($messages) + (int) $newline;
$this->newLineWritten = $this->newLinesWritten > 0;
parent::write($messages, $newline, $options);
}
/**
* {@inheritdoc}
*/
#[\Override]
public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL): void
{
$this->newLinesWritten = $this->trailingNewLineCount($messages) + 1;
$this->newLineWritten = true;
parent::writeln($messages, $type);
}
/**
* {@inheritdoc}
*/
#[\Override]
public function newLine(int $count = 1): void
{
$this->newLinesWritten += $count;
$this->newLineWritten = $this->newLinesWritten > 0;
parent::newLine($count);
}
/**
* {@inheritdoc}
*/
public function newLinesWritten()
{
if ($this->output instanceof static) {
return $this->output->newLinesWritten();
}
return $this->newLinesWritten;
}
/**
* {@inheritdoc}
*
* @deprecated use newLinesWritten
*/
public function newLineWritten()
{
if ($this->output instanceof static && $this->output->newLineWritten()) {
return true;
}
return $this->newLineWritten;
}
/*
* Count the number of trailing new lines in a string.
*
* @param string|iterable $messages
* @return int
*/
protected function trailingNewLineCount($messages)
{
if (is_iterable($messages)) {
$string = '';
foreach ($messages as $message) {
$string .= $message.PHP_EOL;
}
} else {
$string = $messages;
}
return strlen($string) - strlen(rtrim($string, PHP_EOL));
}
/**
* Returns whether verbosity is quiet (-q).
*
* @return bool
*/
public function isQuiet(): bool
{
return $this->output->isQuiet();
}
/**
* Returns whether verbosity is verbose (-v).
*
* @return bool
*/
public function isVerbose(): bool
{
return $this->output->isVerbose();
}
/**
* Returns whether verbosity is very verbose (-vv).
*
* @return bool
*/
public function isVeryVerbose(): bool
{
return $this->output->isVeryVerbose();
}
/**
* Returns whether verbosity is debug (-vvv).
*
* @return bool
*/
public function isDebug(): bool
{
return $this->output->isDebug();
}
/**
* Get the underlying Symfony output implementation.
*
* @return \Symfony\Component\Console\Output\OutputInterface
*/
public function getOutput()
{
return $this->output;
}
}