Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Apr 12, 2022
1 parent 0e34a1e commit d6125d2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
35 changes: 17 additions & 18 deletions demo/Commands/ExamplesOutput.php
Expand Up @@ -22,6 +22,8 @@
use JBZoo\Cli\Codes;
use Symfony\Component\Console\Input\InputOption;

use function JBZoo\Cli\cli;

/**
* Class ExamplesOutput
*/
Expand Down Expand Up @@ -53,46 +55,43 @@ protected function executeAction(): int

// If output is hidden, we can use this method to show the message. It's like "always"
// ./my-app examples:output -q
$this->_("You can see this line even if {$t1}--quie{$t2} or {$t1}-q{$t2} is used.", CliHelper::QUIET);
$this->_("You can see this line even if {$t1}--quiet{$t2} or {$t1}-q{$t2} is used.", CliHelper::QUIET);
$this->_();


// ./my-app examples:output
$this->_('Regular message');
$this->_([
'Several',
' lines',
' message.'
]);
//$this->_(['Several', ' lines', ' message.']);
$this->_(); // Break the line


// `cli($text)` is global alias for `$this->_();`

// Info output
// ./my-app examples:output -v
$this->_("Verbose message #1 {$t1}CliHelper::V{$t2} (-v)", CliHelper::V);
$this->_("Verbose message #2 {$t1}CliHelper::INFO{$t2} (-v)", CliHelper::INFO);
cli("Verbose message #1 {$t1}CliHelper::V{$t2} (-v)", CliHelper::V);
cli("Verbose message #2 {$t1}CliHelper::INFO{$t2} (-v)", CliHelper::INFO);
$this->isInfoLevel() && $this->_();


// Warning output
// ./my-app examples:output -vv
$this->_("Very verbose or warning message #1 {$t1}CliHelper::VV{$t2} (-vv)", CliHelper::VV);
$this->_("Very verbose or warning message #2 {$t1}CliHelper::WARNING{$t2} (-vv)", CliHelper::WARNING);
$this->isWarningLevel() && $this->_();
cli("Very verbose or warning message #1 {$t1}CliHelper::VV{$t2} (-vv)", CliHelper::VV);
cli("Very verbose or warning message #2 {$t1}CliHelper::WARNING{$t2} (-vv)", CliHelper::WARNING);
$this->isWarningLevel() && cli();


// Debug output
// ./my-app examples:output -vvv
$this->_("Low-level message for devs #1 {$t1}CliHelper::VVV{$t2} (-vvv)", CliHelper::VVV);
$this->_("Low-level message for devs #2 {$t1}CliHelper::DEBUG{$t2} (-vvv)", CliHelper::DEBUG);
$this->isDebugLevel() && $this->_();
cli("Low-level message for devs #1 {$t1}CliHelper::VVV{$t2} (-vvv)", CliHelper::VVV);
cli("Low-level message for devs #2 {$t1}CliHelper::DEBUG{$t2} (-vvv)", CliHelper::DEBUG);
$this->isDebugLevel() && cli();


// Error output (StdErr)
// ./my-app examples:output -vvv > /dev/null
$this->_("Not critical error message in runtime to StdErr. {$t1}CliHelper::E{$t2}", CliHelper::E);
$this->_("Not critical error message in runtime to StdErr. {$t1}CliHelper::ERROR{$t2}", CliHelper::ERROR);
$this->_();
cli("Not critical error message in runtime to <r>StdErr</r>. {$t1}CliHelper::E{$t2}", CliHelper::E);
cli("Not critical error message in runtime to <bg>StdErr</bg>. {$t1}CliHelper::ERROR{$t2}", CliHelper::ERROR);
cli();


// If we want to throw an exception, we can use this way
Expand Down
2 changes: 1 addition & 1 deletion src/CliCommand.php
Expand Up @@ -52,7 +52,7 @@ protected function configure(): void
null,
InputOption::VALUE_NONE,
"Mute any sort of errors. So exit code will be always \"0\" (if it's possible).\n" .
"It has major priority then <info>--strict</info>. It's on your own risk!"
"It has major priority then <info>--non-zero-on-error</info>. It's on your own risk!"
)
->addOption(
'stdout-only',
Expand Down
10 changes: 9 additions & 1 deletion src/CliHelper.php
Expand Up @@ -168,8 +168,9 @@ public static function getBinPath(): string
public static function addOutputStyles(OutputInterface $output): OutputInterface
{
$formatter = $output->getFormatter();
$defaultColor = 'default';

$colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default'];
$colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', $defaultColor];

foreach ($colors as $color) {
$formatter->setStyle($color, new OutputFormatterStyle($color));
Expand All @@ -179,6 +180,13 @@ public static function addOutputStyles(OutputInterface $output): OutputInterface
$formatter->setStyle("{$color}-bg", new OutputFormatterStyle(null, $color));
}

$formatter->setStyle("bl", new OutputFormatterStyle($defaultColor, null, ['blink']));
$formatter->setStyle("b", new OutputFormatterStyle($defaultColor, null, ['bold']));
$formatter->setStyle("u", new OutputFormatterStyle($defaultColor, null, ['underscore']));
$formatter->setStyle("r", new OutputFormatterStyle(null, null, ['reverse']));
$formatter->setStyle("bg", new OutputFormatterStyle('black', 'white'));
$formatter->setStyle("i", new OutputFormatterStyle('green')); // Alias for <info>

return $output;
}

Expand Down
29 changes: 29 additions & 0 deletions src/functions.php
@@ -0,0 +1,29 @@
<?php

/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/

declare(strict_types=1);

namespace JBZoo\Cli;

/**
* Shortcut method
* @param array|mixed $messages
* @param string $verboseLevel
* @return void
*/
function cli($messages = '', string $verboseLevel = CliHelper::DEFAULT): void
{
CliHelper::getInstance()->_($messages, $verboseLevel);
}
8 changes: 4 additions & 4 deletions tests/Cli/CliOutputTest.php
Expand Up @@ -230,12 +230,12 @@ public function testTimestamp()
{
// Redirect common message
[$exitCode, $stdOut, $errOut] = Helper::executeReal('test:output', ['timestamp' => null]);
isContain(':00] Error: Message', $errOut);
isContain('] Error: Message', $errOut);
isSame(0, $exitCode);

isContain(':00] Normal 1', $stdOut);
isContain(':00] Normal 2', $stdOut);
isContain(':00] Quiet -q', $stdOut);
isContain('] Normal 1', $stdOut);
isContain('] Normal 2', $stdOut);
isContain('] Quiet -q', $stdOut);
}

public function testTypeOfVars()
Expand Down

0 comments on commit d6125d2

Please sign in to comment.