33namespace PhpDevCommunity \Console \Output ;
44
55
6+ use InvalidArgumentException ;
67use PhpDevCommunity \Console \OutputInterface ;
8+ use RuntimeException ;
79
810final class ConsoleOutput implements OutputInterface
911{
@@ -80,20 +82,20 @@ public function title(string $message): void
8082 $ titleLength = mb_strlen ($ message );
8183 $ underline = str_repeat ('= ' , min ($ consoleWidth , $ titleLength ));
8284
83- $ this ->writeColor (PHP_EOL );
84- $ this ->writeColor ($ message );
85- $ this ->writeColor (PHP_EOL );
86- $ this ->writeColor ($ underline );
87- $ this ->writeColor (PHP_EOL );
85+ $ this ->write (PHP_EOL );
86+ $ this ->write ($ message );
87+ $ this ->write (PHP_EOL );
88+ $ this ->write ($ underline );
89+ $ this ->write (PHP_EOL );
8890 }
8991
9092 public function list (array $ items ): void
9193 {
9294 foreach ($ items as $ item ) {
93- $ this ->writeColor ('- ' . $ item );
94- $ this ->writeColor (PHP_EOL );
95+ $ this ->write ('- ' . $ item );
96+ $ this ->write (PHP_EOL );
9597 }
96- $ this ->writeColor (PHP_EOL );
98+ $ this ->write (PHP_EOL );
9799 }
98100
99101 public function listKeyValues (array $ items , bool $ inlined = false ): void
@@ -111,17 +113,17 @@ public function listKeyValues(array $items, bool $inlined = false): void
111113 foreach ($ items as $ key => $ value ) {
112114 $ key = str_pad ($ key , $ maxKeyLength , ' ' , STR_PAD_RIGHT );
113115 $ this ->writeColor ($ key , 'green ' );
114- $ this ->writeColor (' : ' );
116+ $ this ->write (' : ' );
115117 $ this ->writeColor ($ value , 'white ' );
116- $ this ->writeColor (PHP_EOL );
118+ $ this ->write (PHP_EOL );
117119 }
118- $ this ->writeColor (PHP_EOL );
120+ $ this ->write (PHP_EOL );
119121 }
120122
121123 public function indentedList (array $ items , int $ indentLevel = 1 ): void
122124 {
123125 if ($ indentLevel == 1 ) {
124- $ this ->writeColor (PHP_EOL );
126+ $ this ->write (PHP_EOL );
125127 }
126128
127129 foreach ($ items as $ item ) {
@@ -134,12 +136,12 @@ public function indentedList(array $items, int $indentLevel = 1): void
134136 }
135137 $ this ->writeColor ($ indentation . '- ' , 'red ' );
136138 $ this ->writeColor ($ item , 'white ' );
137- $ this ->writeColor (PHP_EOL );
139+ $ this ->write (PHP_EOL );
138140 }
139141 }
140142
141143 if ($ indentLevel == 1 ) {
142- $ this ->writeColor (PHP_EOL );
144+ $ this ->write (PHP_EOL );
143145 }
144146 }
145147
@@ -148,36 +150,57 @@ public function numberedList(array $items)
148150 foreach ($ items as $ index => $ item ) {
149151 $ this ->writeColor (($ index + 1 ) . '. ' , 'white ' );
150152 $ this ->writeColor ($ item , 'green ' );
151- $ this ->writeColor (PHP_EOL );
153+ $ this ->write (PHP_EOL );
152154 }
153- $ this ->writeColor (PHP_EOL );
155+ $ this ->write (PHP_EOL );
154156 }
155157
156158 public function table (array $ headers , array $ rows ): void
157159 {
160+ $ maxWidth = $ this ->geTerminalWidth ();
161+ $ maxWidthPerColumn = $ maxWidth / count ($ headers );
158162 $ columnWidths = array_map (function ($ header ) {
159163 return mb_strlen ($ header );
160164 }, $ headers );
161165
166+ $ processedRows = [];
162167 foreach ($ rows as $ row ) {
168+ $ row = array_values ($ row );
169+ $ processedRow = [];
163170 foreach ($ row as $ index => $ column ) {
164- $ columnWidths [$ index ] = max ($ columnWidths [$ index ], mb_strlen ($ column ));
171+ $ column = $ this ->variableToString ($ column );
172+ $ lines = explode (PHP_EOL , trim ($ column ));
173+ foreach ($ lines as $ i => $ line ) {
174+ $ maxWidth = max ($ columnWidths [$ index ], mb_strlen ($ line ));
175+ if ($ maxWidth > $ maxWidthPerColumn ) {
176+ $ maxWidth = $ maxWidthPerColumn ;
177+ }
178+ $ columnWidths [$ index ] = $ maxWidth ;
179+ if (mb_strlen ($ line ) > $ maxWidth ) {
180+ $ lines [$ i ] = mb_substr ($ line , 0 , $ maxWidth ) . '... ' ;
181+ }
182+ }
183+ $ processedRow [$ index ] = $ lines ;
165184 }
185+ $ processedRows [] = $ processedRow ;
166186 }
167187
168188 foreach ($ headers as $ index => $ header ) {
169- $ this ->writeColor (str_pad ($ header , $ columnWidths [$ index ] + 2 ));
189+ $ this ->write (str_pad ($ header , $ columnWidths [$ index ] + 2 ));
170190 }
171- $ this ->writeColor (PHP_EOL );
172-
173- $ this ->writeColor (str_repeat ('- ' , array_sum ($ columnWidths ) + count ($ columnWidths ) * 2 ));
174- $ this ->writeColor (PHP_EOL );
175-
176- foreach ($ rows as $ row ) {
177- foreach ($ row as $ index => $ column ) {
178- $ this ->writeColor (str_pad ($ column , $ columnWidths [$ index ] + 2 ));
191+ $ this ->write (PHP_EOL );
192+ $ this ->write (str_repeat ('- ' , array_sum ($ columnWidths ) + count ($ columnWidths ) * 2 ));
193+ $ this ->write (PHP_EOL );
194+
195+ foreach ($ processedRows as $ row ) {
196+ $ maxLines = max (array_map ('count ' , $ row ));
197+ for ($ lineIndex = 0 ; $ lineIndex < $ maxLines ; $ lineIndex ++) {
198+ foreach ($ row as $ index => $ lines ) {
199+ $ line = $ lines [$ lineIndex ] ?? '' ; // Récupère la ligne actuelle ou une chaîne vide
200+ $ this ->write (str_pad ($ line , $ columnWidths [$ index ] + 2 ));
201+ }
202+ $ this ->write (PHP_EOL );
179203 }
180- $ this ->writeColor (PHP_EOL );
181204 }
182205 }
183206
@@ -186,9 +209,9 @@ public function progressBar(int $total, int $current): void
186209 $ barWidth = 50 ;
187210 $ progress = ($ current / $ total ) * $ barWidth ;
188211 $ bar = str_repeat ('# ' , (int )$ progress ) . str_repeat (' ' , $ barWidth - (int )$ progress );
189- $ this ->writeColor (sprintf ("\r[%s] %d%% " , $ bar , ($ current / $ total ) * 100 ));
212+ $ this ->write (sprintf ("\r[%s] %d%% " , $ bar , ($ current / $ total ) * 100 ));
190213 if ($ current === $ total ) {
191- $ this ->writeColor (PHP_EOL );
214+ $ this ->write (PHP_EOL );
192215 }
193216 }
194217
@@ -209,12 +232,12 @@ public function ask(string $question, bool $hidden = false, bool $required = fal
209232
210233 if ($ hidden ) {
211234 if (strncasecmp (PHP_OS , 'WIN ' , 3 ) == 0 ) {
212- throw new \ RuntimeException ('Windows platform is not supported for hidden input ' );
235+ throw new RuntimeException ('Windows platform is not supported for hidden input ' );
213236 } else {
214237 system ('stty -echo ' );
215238 $ input = trim (fgets (STDIN ));
216239 system ('stty echo ' );
217- $ this ->writeColor (PHP_EOL );
240+ $ this ->write (PHP_EOL );
218241 }
219242 } else {
220243 $ handle = fopen ('php://stdin ' , 'r ' );
@@ -223,7 +246,7 @@ public function ask(string $question, bool $hidden = false, bool $required = fal
223246 }
224247
225248 if ($ required && empty ($ input )) {
226- throw new \ InvalidArgumentException ('Response cannot be empty ' );
249+ throw new InvalidArgumentException ('Response cannot be empty ' );
227250 }
228251
229252 return $ input ;
@@ -235,11 +258,11 @@ public function spinner(int $duration = 3): void
235258 $ time = microtime (true );
236259 while ((microtime (true ) - $ time ) < $ duration ) {
237260 foreach ($ spinnerChars as $ char ) {
238- $ this ->writeColor ("\r$ char " );
261+ $ this ->write ("\r$ char " );
239262 usleep (100000 );
240263 }
241264 }
242- $ this ->writeColor ("\r" );
265+ $ this ->write ("\r" );
243266 }
244267
245268 public function json (array $ data ): void
@@ -250,12 +273,12 @@ public function json(array $data): void
250273 $ this ->writeColor ("Error encoding JSON: " . json_last_error_msg () . PHP_EOL , 'red ' );
251274 return ;
252275 }
253- $ this ->writeColor ($ jsonOutput . PHP_EOL );
276+ $ this ->write ($ jsonOutput . PHP_EOL );
254277 }
255278
256279 public function boxed (string $ message , string $ borderChar = '* ' , int $ padding = 1 ): void
257280 {
258- $ this ->writeColor (PHP_EOL );
281+ $ this ->write (PHP_EOL );
259282 $ lineLength = mb_strlen ($ message );
260283 $ boxWidth = $ this ->geTerminalWidth ();
261284 if ($ lineLength > $ boxWidth ) {
@@ -264,13 +287,13 @@ public function boxed(string $message, string $borderChar = '*', int $padding =
264287 $ lines = explode ('| ' , wordwrap ($ message , $ lineLength , '| ' , true ));
265288 $ border = str_repeat ($ borderChar , $ lineLength + ($ padding * 2 ) + 2 );
266289
267- $ this ->writeColor ($ border . PHP_EOL );
290+ $ this ->write ($ border . PHP_EOL );
268291 foreach ($ lines as $ line ) {
269292 $ strPad = str_repeat (' ' , $ padding );
270- $ this ->writeColor ($ borderChar . $ strPad . str_pad ($ line , $ lineLength ) . $ strPad . $ borderChar . PHP_EOL );
293+ $ this ->write ($ borderChar . $ strPad . str_pad ($ line , $ lineLength ) . $ strPad . $ borderChar . PHP_EOL );
271294 }
272- $ this ->writeColor ($ border . PHP_EOL );
273- $ this ->writeColor (PHP_EOL );
295+ $ this ->write ($ border . PHP_EOL );
296+ $ this ->write (PHP_EOL );
274297 }
275298
276299 public function writeColor (string $ message , ?string $ color = null , ?string $ background = null ): void
@@ -302,9 +325,9 @@ public function writeln(string $message): void
302325
303326 private function outputMessage ($ formattedMessage , int $ lineLength , string $ color ): void
304327 {
305- $ this ->writeColor (PHP_EOL );
328+ $ this ->write (PHP_EOL );
306329 $ this ->writeColor (str_repeat (' ' , $ lineLength ), 'white ' , $ color );
307- $ this ->writeColor (PHP_EOL );
330+ $ this ->write (PHP_EOL );
308331
309332 if (is_string ($ formattedMessage )) {
310333 $ formattedMessage = [$ formattedMessage ];
@@ -314,10 +337,10 @@ private function outputMessage($formattedMessage, int $lineLength, string $color
314337 $ this ->writeColor ($ line , 'white ' , $ color );
315338 }
316339
317- $ this ->writeColor (PHP_EOL );
340+ $ this ->write (PHP_EOL );
318341 $ this ->writeColor (str_repeat (' ' , $ lineLength ), 'white ' , $ color );
319- $ this ->writeColor (PHP_EOL );
320- $ this ->writeColor (PHP_EOL );
342+ $ this ->write (PHP_EOL );
343+ $ this ->write (PHP_EOL );
321344 }
322345
323346 private function formatMessage (string $ prefix , string $ message , string $ color ): array
@@ -335,8 +358,30 @@ private function formatMessage(string $prefix, string $message, string $color):
335358 }
336359 return [$ formattedMessage , $ lineLength , $ color ];
337360 }
361+
338362 private function geTerminalWidth (): int
339363 {
340364 return ((int )exec ('tput cols ' ) ?? 85 - 5 );
341365 }
366+
367+ private function variableToString ($ variable ): string
368+ {
369+ if (is_object ($ variable )) {
370+ return 'Object: ' . get_class ($ variable );
371+ } elseif (is_array ($ variable )) {
372+ $ variables = [];
373+ foreach ($ variable as $ item ) {
374+ $ variables [] = $ this ->variableToString ($ item );
375+ }
376+ return var_export ($ variables , true );
377+ } elseif (is_resource ($ variable )) {
378+ return (string )$ variable ;
379+ } elseif (is_null ($ variable )) {
380+ return 'NULL ' ;
381+ } elseif (is_string ($ variable )) {
382+ return $ variable ;
383+ }
384+
385+ return var_export ($ variable , true );
386+ }
342387}
0 commit comments