1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* See class comment |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* @category Netresearch |
8
|
|
|
* @package Netresearch\Kite\Console\Output |
9
|
|
|
* @author Christian Opitz <[email protected]> |
10
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
11
|
|
|
* @link http://www.netresearch.de |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Netresearch\Kite\Console\Output; |
15
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
16
|
|
|
|
17
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
18
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Output |
22
|
|
|
* |
23
|
|
|
* @category Netresearch |
24
|
|
|
* @package Netresearch\Kite\Console\Output |
25
|
|
|
* @author Christian Opitz <[email protected]> |
26
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
27
|
|
|
* @link http://www.netresearch.de |
28
|
|
|
*/ |
29
|
|
|
class Output extends StreamOutput |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $indention = 0; |
35
|
|
|
|
36
|
|
|
protected $actualIndention = 0; |
37
|
|
|
|
38
|
|
|
protected $lastIndention = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool Whether previous, actually output line had newline ending |
42
|
|
|
*/ |
43
|
|
|
protected $previousWasNewLine = true; |
44
|
|
|
|
45
|
|
|
protected $outputOnCurrentIndention = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $terminalDimensions; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor. |
54
|
|
|
* |
55
|
|
|
* @param mixed $stream A stream resource |
56
|
|
|
* @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) |
57
|
|
|
* @param bool|null $decorated Whether to decorate messages (null for auto-guessing) |
58
|
|
|
* @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) |
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException When first argument is not a real stream |
61
|
|
|
* |
62
|
|
|
* @api |
63
|
|
|
*/ |
64
|
|
|
public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null) |
65
|
|
|
{ |
66
|
|
|
parent::__construct($stream, $verbosity, $decorated, $formatter); |
67
|
|
|
$formatter = $this->getFormatter(); |
68
|
|
|
$formatter->setStyle('cmd', new OutputFormatterStyle('black', null, array('bold'))); |
69
|
|
|
$formatter->setStyle('warning', new OutputFormatterStyle('black', 'yellow')); |
70
|
|
|
$formatter->setStyle('step', new OutputFormatterStyle('green', 'black')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set terminal dimensions |
75
|
|
|
* |
76
|
|
|
* @param array $terminalDimensions Dimensions |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function setTerminalDimensions($terminalDimensions) |
81
|
|
|
{ |
82
|
|
|
$this->terminalDimensions = $terminalDimensions; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Increase indention for all following lines |
87
|
|
|
* |
88
|
|
|
* @param int $tabs The tabs |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function indent($tabs = 1) |
93
|
|
|
{ |
94
|
|
|
$this->indention += $tabs; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Decrease indention for all following lines |
99
|
|
|
* |
100
|
|
|
* @param int $tabs The tabs |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function outdent($tabs = 1) |
105
|
|
|
{ |
106
|
|
|
$this->indention -= $tabs; |
107
|
|
|
if ($this->indention < 0) { |
108
|
|
|
$this->indention = 0; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the current indention (this actually indents further/back when required) |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function getIndention() |
118
|
|
|
{ |
119
|
|
|
if ($this->lastIndention !== null) { |
120
|
|
|
if ($this->indention > $this->lastIndention) { |
121
|
|
|
$this->actualIndention++; |
122
|
|
|
} elseif ($this->indention < $this->lastIndention) { |
123
|
|
|
$this->actualIndention = max(0, min($this->actualIndention - 1, $this->indention)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
$this->lastIndention = $this->indention; |
127
|
|
|
return str_repeat(" ", $this->actualIndention * 2); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Write to output |
132
|
|
|
* |
133
|
|
|
* @param array|string $messages Messages |
134
|
|
|
* @param bool $newline Whether to append a newline |
135
|
|
|
* @param int $type The output type |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function write($messages, $newline = false, $type = \Symfony\Component\Console\Output\Output::OUTPUT_NORMAL) |
140
|
|
|
{ |
141
|
|
|
$messages = (array) $messages; |
142
|
|
|
|
143
|
|
|
foreach ($messages as &$message) { |
144
|
|
|
$l = strlen($message) - 1; |
145
|
|
|
if ($l >= 0) { |
146
|
|
|
if ($message[$l] === "\n") { |
147
|
|
|
$message = substr($message, 0, $l); |
148
|
|
|
$l--; |
149
|
|
|
$newline = true; |
150
|
|
|
} |
151
|
|
|
if ($this->previousWasNewLine && $l >= 0 && $message[0] !== "\n") { |
152
|
|
|
$message = $this->getIndention() . $message; |
153
|
|
|
} |
154
|
|
|
if (strpos($message, "\n") !== false) { |
155
|
|
|
$message = str_replace("\n", "\n" . $this->getIndention(), $message); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// TODO: Indent wrapped lines - that's just not that easy because of the ANSI color escape codes |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
parent::write($messages, $newline, $type); |
163
|
|
|
|
164
|
|
|
$this->previousWasNewLine = $newline; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
?> |
171
|
|
|
|