Skip to content

Commit 79809d7

Browse files
authored
Merge pull request #1910 from codeigniter4/cliprint
Added print method to CLI library so you can print multiple times on same line
2 parents ac5fb10 + 8ba6258 commit 79809d7

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

system/CLI/CLI.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/

tests/system/CLI/CLITest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,35 @@ public function testColor()
9999
$this->assertEquals("\033[1;37m\033[42m\033[4mtest\033[0m", CLI::color('test', 'white', 'green', 'underline'));
100100
}
101101

102+
public function testPrint()
103+
{
104+
CLI::print('test');
105+
$expected = 'test';
106+
107+
$this->assertEquals($expected, CITestStreamFilter::$buffer);
108+
}
109+
110+
public function testPrintForeground()
111+
{
112+
CLI::print('test', 'red');
113+
$expected = "\033[0;31mtest\033[0m";
114+
115+
$this->assertEquals($expected, CITestStreamFilter::$buffer);
116+
}
117+
118+
public function testPrintBackground()
119+
{
120+
CLI::print('test', 'red', 'green');
121+
$expected = "\033[0;31m\033[42mtest\033[0m";
122+
123+
$this->assertEquals($expected, CITestStreamFilter::$buffer);
124+
}
125+
102126
public function testWrite()
103127
{
104128
CLI::write('test');
105129
$expected = <<<EOT
130+
106131
test
107132
108133
EOT;

user_guide_src/source/cli/cli_library.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ And a smaller number are available as background colors:
106106
* light_gray
107107
* magenta
108108

109+
**print()**
110+
111+
Print functions identically to the ``write()`` method, except that it does not force a newline either before or after.
112+
Instead it prints it to the screen wherever the cursor is currently. This allows you to print multiple items all on
113+
the same line, from different calls. This is especially helpful when you want to show a status, do something, then
114+
print "Done" on the same line::
115+
116+
for ($i = 0; $i <= 10; $i++)
117+
{
118+
CLI::print($i);
119+
}
120+
109121
**color()**
110122

111123
While the ``write()`` command will write a single line to the terminal, ending it with a EOL character, you can

0 commit comments

Comments
 (0)