1 | <?php |
||
2 | namespace Garden\Cli; |
||
3 | |||
4 | /** |
||
5 | * Used to write a formatted table to the console. |
||
6 | */ |
||
7 | class Table { |
||
8 | /// Properties /// |
||
9 | |||
10 | /** |
||
11 | * @var array An array of column widths. |
||
12 | */ |
||
13 | protected $columnWidths; |
||
14 | |||
15 | /** |
||
16 | * @var bool Whether or not to format the console commands. |
||
17 | */ |
||
18 | protected $formatOutput = true; |
||
19 | |||
20 | /** |
||
21 | * @var array An array of the row data. |
||
22 | */ |
||
23 | protected $rows; |
||
24 | |||
25 | /** |
||
26 | * @var array|null A pointer to the current row. |
||
27 | */ |
||
28 | protected $currentRow; |
||
29 | |||
30 | /** |
||
31 | * @var int The maximum width of the table. |
||
32 | */ |
||
33 | public $maxWidth = 80; |
||
34 | |||
35 | /** |
||
36 | * @var int The left padding on each cell. |
||
37 | */ |
||
38 | public $padding = 3; |
||
39 | |||
40 | /** |
||
41 | * @var int The left indent on the table. |
||
42 | */ |
||
43 | public $indent = 2; |
||
44 | |||
45 | |||
46 | /// Methods /// |
||
47 | |||
48 | /** |
||
49 | * Initialize an instance of the {@link Table} class. |
||
50 | */ |
||
51 | 7 | public function __construct() { |
|
52 | 7 | $this->formatOutput = Cli::guessFormatOutput(); |
|
53 | 7 | $this->reset(); |
|
54 | 7 | } |
|
55 | |||
56 | /** |
||
57 | * Backwards compatibility for the **format** property. |
||
58 | * |
||
59 | * @param string $name Must be **format**. |
||
60 | * @return bool|null Returns {@link getFormatOutput()} or null if {@link $name} isn't **format**. |
||
61 | */ |
||
62 | public function __get($name) { |
||
63 | if ($name === 'format') { |
||
64 | trigger_error("Cli->format is deprecated. Use Cli->getFormatOutput() instead.", E_USER_DEPRECATED); |
||
65 | return $this->getFormatOutput(); |
||
66 | } |
||
67 | return null; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Backwards compatibility for the **format** property. |
||
72 | * |
||
73 | * @param string $name Must be **format**. |
||
74 | * @param bool $value One of **true** or **false**. |
||
75 | */ |
||
76 | public function __set($name, $value) { |
||
77 | if ($name === 'format') { |
||
78 | trigger_error("Cli->format is deprecated. Use Cli->setFormatOutput() instead.", E_USER_DEPRECATED); |
||
79 | $this->setFormatOutput($value); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get whether or not output should be formatted. |
||
85 | * |
||
86 | * @return boolean Returns **true** if output should be formatted or **false** otherwise. |
||
87 | */ |
||
88 | public function getFormatOutput() { |
||
89 | return $this->formatOutput; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Set whether or not output should be formatted. |
||
94 | * |
||
95 | * @param boolean $formatOutput Whether or not to format output. |
||
96 | * @return LogFormatter Returns `$this` for fluent calls. |
||
97 | */ |
||
98 | 5 | public function setFormatOutput($formatOutput) { |
|
99 | 5 | $this->formatOutput = $formatOutput; |
|
100 | 5 | return $this; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Add a cell to the table. |
||
105 | * |
||
106 | * @param string $text The text of the cell. |
||
107 | * @param array $wrap A two element array used to wrap the text in the cell. |
||
108 | * @return $this |
||
109 | */ |
||
110 | 7 | protected function addCell($text, $wrap = ['', '']) { |
|
111 | 7 | if ($this->currentRow === null) { |
|
112 | $this->row(); |
||
113 | } |
||
114 | |||
115 | 7 | $i = count($this->currentRow); |
|
116 | 7 | $this->columnWidths[$i] = max(strlen($text), Cli::val($i, $this->columnWidths, 0)); // max column width |
|
117 | |||
118 | 7 | $this->currentRow[$i] = [$text, $wrap]; |
|
119 | 7 | return $this; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Adds a cell. |
||
124 | * |
||
125 | * @param string $text The text of the cell. |
||
126 | * @return $this |
||
127 | */ |
||
128 | 7 | public function cell($text) { |
|
129 | 7 | return $this->addCell($text); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Adds a bold cell. |
||
134 | * |
||
135 | * @param string $text The text of the cell. |
||
136 | * @return $this |
||
137 | */ |
||
138 | 4 | public function bold($text) { |
|
139 | 4 | return $this->addCell($text, ["\033[1m", "\033[0m"]); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Adds a red cell. |
||
144 | * |
||
145 | * @param string $text The text of the cell. |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function red($text) { |
||
149 | return $this->addCell($text, ["\033[1;31m", "\033[0m"]); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Adds a green cell. |
||
154 | * |
||
155 | * @param string $text The text of the cell. |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function green($text) { |
||
159 | return $this->addCell($text, ["\033[1;32m", "\033[0m"]); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Adds a blue cell. |
||
164 | * |
||
165 | * @param string $text The text of the cell. |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function blue($text) { |
||
169 | return $this->addCell($text, ["\033[1;34m", "\033[0m"]); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Adds a purple cell. |
||
174 | * |
||
175 | * @param string $text The text of the cell. |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function purple($text) { |
||
179 | return $this->addCell($text, ["\033[0;35m", "\033[0m"]); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Reset the table so another one can be written with the same object. |
||
184 | */ |
||
185 | 7 | public function reset() { |
|
186 | 7 | $this->columnWidths = []; |
|
187 | 7 | $this->rows = []; |
|
188 | 7 | $this->currentRow = null; |
|
189 | 7 | } |
|
190 | |||
191 | /** |
||
192 | * Start a new row. |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | 7 | public function row() { |
|
197 | 7 | $this->rows[] = []; |
|
198 | 7 | $this->currentRow =& $this->rows[count($this->rows) - 1]; |
|
199 | 7 | return $this; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Writes the final table. |
||
204 | */ |
||
205 | 7 | public function write() { |
|
206 | // Determine the width of the last column. |
||
207 | 7 | $columnWidths = array_sum($this->columnWidths); |
|
208 | 7 | $totalWidth = $this->indent + $columnWidths + $this->padding * (count($this->columnWidths) - 1); |
|
209 | |||
210 | 7 | $lastWidth = end($this->columnWidths) + $this->maxWidth - $totalWidth; |
|
211 | 7 | $lastWidth = max($lastWidth, 10); // min width of 10 |
|
212 | 7 | $this->columnWidths[count($this->columnWidths) - 1] = $lastWidth; |
|
213 | |||
214 | // Loop through each row and write it. |
||
215 | 7 | foreach ($this->rows as $row) { |
|
216 | 7 | $rowLines = []; |
|
217 | 7 | $lineCount = 0; |
|
218 | |||
219 | // Split the cells into lines. |
||
220 | 7 | foreach ($row as $i => $cell) { |
|
221 | 7 | list($text,) = $cell; |
|
222 | 7 | $width = $this->columnWidths[$i]; |
|
223 | |||
224 | 7 | $lines = Cli::breakLines($text, $width, $i < count($this->columnWidths) - 1); |
|
225 | 7 | $rowLines[] = $lines; |
|
226 | 7 | $lineCount = max($lineCount, count($lines)); |
|
227 | } |
||
228 | |||
229 | // Write all of the lines. |
||
230 | 7 | for ($i = 0; $i < $lineCount; $i++) { |
|
231 | 7 | foreach ($rowLines as $j => $lines) { |
|
232 | 7 | $padding = $j === 0 ? $this->indent : $this->padding; |
|
233 | |||
234 | 7 | if (isset($lines[$i])) { |
|
235 | 7 | if ($this->formatOutput) { |
|
236 | 3 | if(isset($row[$j])) { |
|
237 | 3 | $wrap = $row[$j][1]; |
|
238 | } else { |
||
239 | // if we're out of array, use the latest wraps |
||
240 | $wrap = $row[count($row)-1][1]; |
||
241 | } |
||
242 | |||
243 | 3 | echo str_repeat(' ', $padding).$wrap[0].$lines[$i].$wrap[1]; |
|
244 | } else { |
||
245 | 7 | echo str_repeat(' ', $padding).$lines[$i]; |
|
246 | } |
||
247 | 2 | } elseif ($j < count($this->columnWidths) - 1) { |
|
248 | // This is an empty line. Write the spaces. |
||
249 | 7 | echo str_repeat(' ', $padding + $this->columnWidths[$j]); |
|
250 | } |
||
251 | } |
||
252 | 7 | echo PHP_EOL; |
|
253 | } |
||
254 | } |
||
255 | 7 | } |
|
256 | } |
||
257 |