@@ -140,6 +140,15 @@ class CLI
140140 */
141141 protected static $ options = [];
142142
143+ /**
144+ * Helps track internally whether the last
145+ * output was a "write" or a "print" to
146+ * keep the output clean and as expected.
147+ *
148+ * @var string
149+ */
150+ protected static $ lastWrite ;
151+
143152 //--------------------------------------------------------------------
144153
145154 /**
@@ -291,7 +300,27 @@ protected static function validate(string $field, string $value, string $rules):
291300 //--------------------------------------------------------------------
292301
293302 /**
294- * Outputs a string to the cli.
303+ * Outputs a string to the CLI without any surrounding newlines.
304+ * Useful for showing repeating elements on a single line.
305+ *
306+ * @param string $text
307+ * @param string|null $foreground
308+ * @param string|null $background
309+ */
310+ public static function print (string $ text = '' , string $ foreground = null , string $ background = null )
311+ {
312+ if ($ foreground || $ background )
313+ {
314+ $ text = static ::color ($ text , $ foreground , $ background );
315+ }
316+
317+ static ::$ lastWrite = null ;
318+
319+ fwrite (STDOUT , $ text );
320+ }
321+
322+ /**
323+ * Outputs a string to the cli on it's own line.
295324 *
296325 * @param string $text The text to output
297326 * @param string $foreground
@@ -304,6 +333,12 @@ public static function write(string $text = '', string $foreground = null, strin
304333 $ text = static ::color ($ text , $ foreground , $ background );
305334 }
306335
336+ if (static ::$ lastWrite !== 'write ' )
337+ {
338+ $ text = PHP_EOL . $ text ;
339+ static ::$ lastWrite = 'write ' ;
340+ }
341+
307342 fwrite (STDOUT , $ text . PHP_EOL );
308343 }
309344
@@ -482,7 +517,7 @@ public static function color(string $text, string $foreground, string $backgroun
482517 * Get the number of characters in string having encoded characters
483518 * and ignores styles set by the color() function
484519 *
485- * @param ? string $string
520+ * @param string $string
486521 *
487522 * @return integer
488523 */
0 commit comments