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 cdc37cf commit df46758
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -18,7 +18,7 @@ The library greatly extends the functionality of [Symfony/Console](https://symfo

## Live Demo

[![asciicast](https://asciinema.org/a/486654.svg)](https://asciinema.org/a/486654)
[![asciicast](https://asciinema.org/a/486674.svg)](https://asciinema.org/a/486674)


## Installing
Expand Down
24 changes: 14 additions & 10 deletions demo/Commands/ExamplesOutput.php
Expand Up @@ -17,9 +17,9 @@

namespace DemoApp\Commands;

use JBZoo\Cli\Cli;
use JBZoo\Cli\CliCommand;
use JBZoo\Cli\Codes;
use JBZoo\Cli\OutLvl;
use Symfony\Component\Console\Input\InputOption;

use function JBZoo\Cli\cli;
Expand Down Expand Up @@ -57,7 +57,7 @@ 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 --quiet
$this->_("You can see the line even if {$tag1}--quiet{$tag2} is used. {$code('Cli::Q')}", Cli::Q);
$this->_("You can see the line even if {$tag1}--quiet{$tag2} is used. {$code('OutLvl::Q')}", OutLvl::Q);
$this->_();


Expand All @@ -70,35 +70,39 @@ protected function executeAction(): int

// Info output
// ./my-app examples:output -v
cli("Verbose message #1 {$code('Cli::V')} (-v)", Cli::V);
cli("Verbose message #2 {$code('Cli::INFO')} (-v)", Cli::INFO);
cli("Verbose message #1 {$code('OutLvl::V')} (-v)", OutLvl::V);
cli("Verbose message #2 {$code('OutLvl::INFO')} (-v)", OutLvl::INFO);
if ($this->isInfoLevel()) {
cli();
}


// Warning output
// ./my-app examples:output -vv
cli("Very verbose or warning message #1 {$code('Cli::VV')} (-vv)", Cli::VV);
cli("Very verbose or warning message #2 {$code('Cli::WARNING')} (-vv)", Cli::WARNING);
cli("Very verbose or warning message #1 {$code('OutLvl::VV')} (-vv)", OutLvl::VV);
cli("Very verbose or warning message #2 {$code('OutLvl::WARNING')} (-vv)", OutLvl::WARNING);
if ($this->isWarningLevel()) {
cli();
}


// Debug output
// ./my-app examples:output -vvv
cli("Low-level message for devs #1 {$code('Cli::VVV')} (-vvv)", Cli::VVV);
cli("Low-level message for devs #2 {$code('Cli::DEBUG')} (-vvv)", Cli::DEBUG);
cli("Low-level message for devs #1 {$code('OutLvl::VVV')} (-vvv)", OutLvl::VVV);
cli("Low-level message for devs #2 {$code('OutLvl::DEBUG')} (-vvv)", OutLvl::DEBUG);
if ($this->isDebugLevel()) {
cli();
}


// Error output (StdErr)
// ./my-app examples:output -vvv > /dev/null
cli("Not critical error message in runtime is written to <u>StdErr</u>. {$code('Cli::E')}", Cli::E);
cli("Not critical error message in runtime is written to <u>StdErr</u>. {$code('Cli::ERROR')}", Cli::ERROR);
cli("Not critical error message in runtime is written to <u>StdErr</u>. {$code('OutLvl::E')}",
OutLvl::E);
cli(
"Not critical error message in runtime is written to <u>StdErr</u>. {$code('OutLvl::ERROR')}",
OutLvl::ERROR
);
cli();


Expand Down
2 changes: 1 addition & 1 deletion demo/Commands/ExamplesStyles.php
Expand Up @@ -17,9 +17,9 @@

namespace DemoApp\Commands;

use JBZoo\Cli\Cli;
use JBZoo\Cli\CliCommand;
use JBZoo\Cli\Codes;
use JBZoo\Cli\Cli;

/**
* Class ExamplesStyles
Expand Down
6 changes: 6 additions & 0 deletions demo/movies/ExamplesOutput.sh
Expand Up @@ -48,6 +48,12 @@ wait
pei "clear"


pei "# And pay attentin on old school style output."
pei "./my-app examples:output --stdout-only | grep 'Legacy Output'"
wait
pei "clear"


pei "# Let's increase the output level. Just add the flag '-v'. Look at 'Info:'"
pei "./my-app examples:output -v"
wait
Expand Down
48 changes: 17 additions & 31 deletions src/Cli.php
Expand Up @@ -31,20 +31,6 @@
*/
class Cli
{
public const Q = 'q';
public const DEFAULT = '';
public const V = 'v';
public const VV = 'vv';
public const VVV = 'vvv';
public const E = 'e';

public const DEBUG = 'debug';
public const INFO = 'info';
public const WARNING = 'warning';
public const ERROR = 'error';
public const EXCEPTION = 'exception';
public const LEGACY = 'legacy';

public const TIMESTAMP_FORMAT = 'Y-m-d H:i:s.v';

/**
Expand Down Expand Up @@ -211,7 +197,7 @@ public function getProfileInfo(): array
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function _($messages = '', string $verboseLevel = self::DEFAULT): void
public function _($messages = '', string $verboseLevel = OutLvl::DEFAULT): void
{
$verboseLevel = \strtolower(\trim($verboseLevel));

Expand Down Expand Up @@ -253,31 +239,31 @@ public function _($messages = '', string $verboseLevel = self::DEFAULT): void

$vNormal = OutputInterface::VERBOSITY_NORMAL;

if ($verboseLevel === self::DEFAULT) {
if ($verboseLevel === OutLvl::DEFAULT) {
$this->output->writeln($profilePrefix . $messages, $vNormal);
} elseif ($verboseLevel === self::V) {
} elseif ($verboseLevel === OutLvl::V) {
$this->output->writeln($profilePrefix . $messages, OutputInterface::VERBOSITY_VERBOSE);
} elseif ($verboseLevel === self::VV) {
} elseif ($verboseLevel === OutLvl::VV) {
$this->output->writeln($profilePrefix . $messages, OutputInterface::VERBOSITY_VERY_VERBOSE);
} elseif ($verboseLevel === self::VVV) {
} elseif ($verboseLevel === OutLvl::VVV) {
$this->output->writeln($profilePrefix . $messages, OutputInterface::VERBOSITY_DEBUG);
} elseif ($verboseLevel === self::Q) {
} elseif ($verboseLevel === OutLvl::Q) {
$this->output->writeln($profilePrefix . $messages, OutputInterface::VERBOSITY_QUIET); // Show ALWAYS!
} elseif ($verboseLevel === self::LEGACY) {
$this->_('<yellow>Legacy Output:</yellow> ' . $messages, self::DEFAULT);
} elseif ($verboseLevel === self::DEBUG) {
$this->_('<magenta>Debug:</magenta> ' . $messages, self::VVV);
} elseif ($verboseLevel === self::WARNING) {
$this->_('<yellow>Warning:</yellow> ' . $messages, self::VV);
} elseif ($verboseLevel === self::INFO) {
$this->_('<blue>Info:</blue> ' . $messages, self::V);
} elseif ($verboseLevel === self::E) {
} elseif ($verboseLevel === OutLvl::LEGACY) {
$this->_('<yellow>Legacy Output:</yellow> ' . $messages, OutLvl::DEFAULT);
} elseif ($verboseLevel === OutLvl::DEBUG) {
$this->_('<magenta>Debug:</magenta> ' . $messages, OutLvl::VVV);
} elseif ($verboseLevel === OutLvl::WARNING) {
$this->_('<yellow>Warning:</yellow> ' . $messages, OutLvl::VV);
} elseif ($verboseLevel === OutLvl::INFO) {
$this->_('<blue>Info:</blue> ' . $messages, OutLvl::V);
} elseif ($verboseLevel === OutLvl::E) {
$this->outputHasErrors = true;
$this->errOutput->writeln($profilePrefix . $messages, $vNormal);
} elseif ($verboseLevel === self::ERROR) {
} elseif ($verboseLevel === OutLvl::ERROR) {
$this->outputHasErrors = true;
$this->errOutput->writeln($profilePrefix . '<red-bg>Error:</red-bg> ' . $messages, $vNormal);
} elseif ($verboseLevel === self::EXCEPTION) {
} elseif ($verboseLevel === OutLvl::EXCEPTION) {
$this->outputHasErrors = true;
$this->errOutput->writeln($profilePrefix . '<red-bg>Muted Exception:</red-bg> ' . $messages, $vNormal);
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/CliCommand.php
Expand Up @@ -74,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->helper = new Cli($input, $output);

$this->_('Working Directory is <i>' . \getcwd() . '</i>', Cli::DEBUG);
$this->_('Working Directory is <i>' . \getcwd() . '</i>', OutLvl::DEBUG);

$exitCode = 0;
try {
Expand All @@ -92,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->trigger('exception', [$this, $this->helper, $exception]);

if ($this->getOptBool('mute-errors')) {
$this->_($exception->getMessage(), Cli::EXCEPTION);
$this->_($exception->getMessage(), OutLvl::EXCEPTION);
} else {
$this->showProfiler();
throw $exception;
Expand All @@ -112,7 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$exitCode = 0;
}

$this->_("Exit Code is \"{$exitCode}\"", Cli::DEBUG);
$this->_("Exit Code is \"{$exitCode}\"", OutLvl::DEBUG);

return $exitCode;
}
Expand Down Expand Up @@ -324,9 +324,9 @@ private function showLegacyOutput(?string $echoContent): void
});

if (\count($lines) > 1) {
$this->_($lines, Cli::LEGACY);
$this->_($lines, OutLvl::LEGACY);
} else {
$this->_($echoContent, Cli::LEGACY);
$this->_($echoContent, OutLvl::LEGACY);
}
}
}
2 changes: 1 addition & 1 deletion src/CliCommandMultiProc.php
Expand Up @@ -171,7 +171,7 @@ protected function executeMultiProcessAction(): int

$warningList = $this->getWarningList();
if (\count($warningList) > 0) {
$this->_(\implode("\n" . \str_repeat('-', 60) . "\n", $warningList), Cli::WARNING);
$this->_(\implode("\n" . \str_repeat('-', 60) . "\n", $warningList), OutLvl::WARNING);
}

return 0;
Expand Down
39 changes: 39 additions & 0 deletions src/OutLvl.php
@@ -0,0 +1,39 @@
<?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;

/**
* Class OutLvl
* @package JBZoo\Cli
*/
class OutLvl
{
public const Q = 'q';
public const DEFAULT = '';
public const V = 'v';
public const VV = 'vv';
public const VVV = 'vvv';
public const E = 'e';

public const DEBUG = 'debug';
public const INFO = 'info';
public const WARNING = 'warning';
public const ERROR = 'error';
public const EXCEPTION = 'exception';
public const LEGACY = 'legacy';
}
2 changes: 1 addition & 1 deletion src/functions.php
Expand Up @@ -23,7 +23,7 @@
* @param string $verboseLevel
* @return void
*/
function cli($messages = '', string $verboseLevel = Cli::DEFAULT): void
function cli($messages = '', string $verboseLevel = OutLvl::DEFAULT): void
{
Cli::getInstance()->_($messages, $verboseLevel);
}
23 changes: 12 additions & 11 deletions tests/fake-app/Commands/TestOutput.php
Expand Up @@ -20,6 +20,7 @@
use JBZoo\Cli\CliCommand;
use JBZoo\Cli\Cli;
use JBZoo\Cli\Exception;
use JBZoo\Cli\OutLvl;
use Symfony\Component\Console\Input\InputOption;

/**
Expand Down Expand Up @@ -66,25 +67,25 @@ protected function executeAction(): int
echo "\t";

$this->_(['Normal 1', 'Normal 2']);
$this->_('Message', Cli::ERROR);
$this->_('Message', OutLvl::ERROR);

$this->_('Info1 -v', Cli::V);
$this->_('Info2 -v', Cli::INFO);
$this->_('Info1 -v', OutLvl::V);
$this->_('Info2 -v', OutLvl::INFO);

$this->_('Verbose1 -vv', Cli::VV);
$this->_('Verbose2 -vv', Cli::WARNING);
$this->_('Verbose1 -vv', OutLvl::VV);
$this->_('Verbose2 -vv', OutLvl::WARNING);

$this->_('Debug1 -vvv', Cli::VVV);
$this->_('Debug1 -vvv', OutLvl::VVV);
$this->_([
'Message #1 -vvv',
'Message #2 -vvv'
], Cli::DEBUG);
], OutLvl::DEBUG);

$this->_('Error (e)', Cli::E);
$this->_('Error (error)', Cli::ERROR);
$this->_('Error (exception)', Cli::EXCEPTION);
$this->_('Error (e)', OutLvl::E);
$this->_('Error (error)', OutLvl::ERROR);
$this->_('Error (exception)', OutLvl::EXCEPTION);

$this->_('Quiet -q', Cli::Q);
$this->_('Quiet -q', OutLvl::Q);

if ($exception = $this->getOptString('exception')) {
throw new Exception($exception);
Expand Down
3 changes: 2 additions & 1 deletion tests/fake-app/Commands/TestSleepMulti.php
Expand Up @@ -20,6 +20,7 @@
use JBZoo\Cli\CliCommandMultiProc;
use JBZoo\Cli\Exception;
use JBZoo\Cli\Cli;
use JBZoo\Cli\OutLvl;
use JBZoo\Utils\Env;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -73,7 +74,7 @@ protected function executeOneProcess(string $pmThreadId): int

sleep($sleep);

$this->_("Finished: {$pmThreadId}", Cli::Q);
$this->_("Finished: {$pmThreadId}", OutLvl::Q);

return 0;
}
Expand Down

0 comments on commit df46758

Please sign in to comment.