Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added print method to CLI library so you can print multiple times on same line #1910

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ class CLI
*/
protected static $options = [];

/**
* Helps track internally whether the last
* output was a "write" or a "print" to
* keep the output clean and as expected.
*
* @var string
*/
protected static $lastWrite;

//--------------------------------------------------------------------

/**
Expand Down Expand Up @@ -291,7 +300,27 @@ protected static function validate(string $field, string $value, string $rules):
//--------------------------------------------------------------------

/**
* Outputs a string to the cli.
* Outputs a string to the CLI without any surrounding newlines.
* Useful for showing repeating elements on a single line.
*
* @param string $text
* @param string|null $foreground
* @param string|null $background
*/
public static function print(string $text = '', string $foreground = null, string $background = null)
{
if ($foreground || $background)
{
$text = static::color($text, $foreground, $background);
}

static::$lastWrite = null;

fwrite(STDOUT, $text);
}

/**
* Outputs a string to the cli on it's own line.
*
* @param string $text The text to output
* @param string $foreground
Expand All @@ -304,6 +333,12 @@ public static function write(string $text = '', string $foreground = null, strin
$text = static::color($text, $foreground, $background);
}

if (static::$lastWrite !== 'write')
{
$text = PHP_EOL . $text;
static::$lastWrite = 'write';
}

fwrite(STDOUT, $text . PHP_EOL);
}

Expand Down Expand Up @@ -482,7 +517,7 @@ public static function color(string $text, string $foreground, string $backgroun
* Get the number of characters in string having encoded characters
* and ignores styles set by the color() function
*
* @param ?string $string
* @param string $string
*
* @return integer
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/system/CLI/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,35 @@ public function testColor()
$this->assertEquals("\033[1;37m\033[42m\033[4mtest\033[0m", CLI::color('test', 'white', 'green', 'underline'));
}

public function testPrint()
{
CLI::print('test');
$expected = 'test';

$this->assertEquals($expected, CITestStreamFilter::$buffer);
}

public function testPrintForeground()
{
CLI::print('test', 'red');
$expected = "\033[0;31mtest\033[0m";

$this->assertEquals($expected, CITestStreamFilter::$buffer);
}

public function testPrintBackground()
{
CLI::print('test', 'red', 'green');
$expected = "\033[0;31m\033[42mtest\033[0m";

$this->assertEquals($expected, CITestStreamFilter::$buffer);
}

public function testWrite()
{
CLI::write('test');
$expected = <<<EOT

test

EOT;
Expand Down
12 changes: 12 additions & 0 deletions user_guide_src/source/cli/cli_library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ And a smaller number are available as background colors:
* light_gray
* magenta

**print()**

Print functions identically to the ``write()`` method, except that it does not force a newline either before or after.
Instead it prints it to the screen wherever the cursor is currently. This allows you to print multiple items all on
the same line, from different calls. This is especially helpful when you want to show a status, do something, then
print "Done" on the same line::

for ($i = 0; $i <= 10; $i++)
{
CLI::print($i);
}

**color()**

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