• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Cli / 5830615199

pending completion
5830615199

push

github

web-flow
Predefined output formats - ELK, cron. New demo and docs. (#15)

745 of 745 new or added lines in 16 files covered. (100.0%)

899 of 1078 relevant lines covered (83.4%)

136.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.84
/src/OutputMods/Text.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Cli.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Cli
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Cli\OutputMods;
18

19
use JBZoo\Cli\CliApplication;
20
use JBZoo\Cli\Exception;
21
use JBZoo\Cli\OutLvl;
22
use JBZoo\Cli\ProgressBars\AbstractProgressBar;
23
use JBZoo\Cli\ProgressBars\ProgressBarSymfony;
24
use JBZoo\Utils\FS;
25
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Output\ConsoleOutput;
28
use Symfony\Component\Console\Output\OutputInterface;
29

30
use function JBZoo\Utils\bool;
31

32
class Text extends AbstractOutputMode
33
{
34
    public function __construct(InputInterface $input, OutputInterface $output, CliApplication $application)
35
    {
36
        parent::__construct($input, $output, $application);
420✔
37

38
        self::addOutputStyles($this->getOutput());
420✔
39
        self::addOutputStyles($this->getErrOutput());
420✔
40

41
        if ($this->output instanceof ConsoleOutput && $this->isStdoutOnly()) {
420✔
42
            $this->output->setErrorOutput($this->output);
28✔
43
        }
44
    }
45

46
    public function onExecBefore(): void
47
    {
48
        $this->_('Working Directory is <i>' . \getcwd() . '</i>', OutLvl::DEBUG);
420✔
49
    }
50

51
    public function onExecAfter(int $exitCode, ?string $outputLevel = null): void
52
    {
53
        $outputLevel ??= OutLvl::DEBUG;
420✔
54
        if ($this->isDisplayProfiling()) {
420✔
55
            $outputLevel = OutLvl::DEFAULT;
20✔
56
        }
57

58
        $totalTime = \number_format(\microtime(true) - $this->getStartTime(), 3);
420✔
59
        $curMemory = FS::format(\memory_get_usage(false));
420✔
60
        $maxMemory = FS::format(\memory_get_peak_usage(true));
420✔
61

62
        $this->_(
420✔
63
            \implode('; ', [
420✔
64
                "Memory Usage/Peak: <green>{$curMemory}</green>/<green>{$maxMemory}</green>",
420✔
65
                "Execution Time: <green>{$totalTime} sec</green>",
420✔
66
            ]),
210✔
67
            $outputLevel,
210✔
68
        );
210✔
69

70
        $this->_("Exit Code is \"{$exitCode}\"", $outputLevel);
420✔
71
    }
72

73
    public function onExecException(\Exception $exception): void
74
    {
75
        if (bool($this->getInput()->getOption('mute-errors'))) {
68✔
76
            $this->_($exception->getMessage(), OutLvl::EXCEPTION);
8✔
77
        }
78
    }
79

80
    public function createProgressBar(): AbstractProgressBar
81
    {
82
        return new ProgressBarSymfony($this);
92✔
83
    }
84

85
    public static function getName(): string
86
    {
87
        return 'text';
572✔
88
    }
89

90
    public static function getDescription(): string
91
    {
92
        return 'Default text output format, userfriendly and easy to read.';
572✔
93
    }
94

95
    public static function addOutputStyles(OutputInterface $output): void
96
    {
97
        $formatter    = $output->getFormatter();
420✔
98
        $defaultColor = 'default';
420✔
99

100
        $colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', $defaultColor];
420✔
101

102
        foreach ($colors as $color) {
420✔
103
            $formatter->setStyle($color, new OutputFormatterStyle($color));
420✔
104
            $formatter->setStyle("{$color}-b", new OutputFormatterStyle($color, null, ['bold']));
420✔
105
            $formatter->setStyle("{$color}-u", new OutputFormatterStyle($color, null, ['underscore']));
420✔
106
            $formatter->setStyle("{$color}-r", new OutputFormatterStyle($color, null, ['reverse']));
420✔
107
            $formatter->setStyle("{$color}-bg", new OutputFormatterStyle(null, $color));
420✔
108
            $formatter->setStyle("{$color}-bl", new OutputFormatterStyle($color, null, ['blink']));
420✔
109
        }
110

111
        $formatter->setStyle('bl', new OutputFormatterStyle($defaultColor, null, ['blink']));
420✔
112
        $formatter->setStyle('b', new OutputFormatterStyle($defaultColor, null, ['bold']));
420✔
113
        $formatter->setStyle('u', new OutputFormatterStyle($defaultColor, null, ['underscore']));
420✔
114
        $formatter->setStyle('r', new OutputFormatterStyle(null, null, ['reverse']));
420✔
115
        $formatter->setStyle('bg', new OutputFormatterStyle('black', 'white'));
420✔
116

117
        // Aliases
118
        $formatter->setStyle('i', new OutputFormatterStyle('green')); // Alias for <info>
420✔
119
        $formatter->setStyle('c', new OutputFormatterStyle('yellow')); // Alias for <comment>
420✔
120
        $formatter->setStyle('q', new OutputFormatterStyle('black', 'cyan')); // Alias for <question>
420✔
121
        $formatter->setStyle('e', new OutputFormatterStyle('white', 'red')); // Alias for <error>
420✔
122

123
        $output->setFormatter($formatter);
420✔
124
    }
125

126
    /**
127
     * Alias to write new line in std output.
128
     */
129
    protected function printMessage(
130
        string $message = '',
131
        string $verboseLevel = OutLvl::DEFAULT,
132
        array $context = [],
133
    ): void {
134
        if (\count($context) > 0) {
420✔
135
            $message .= ' ' . \json_encode($context, \JSON_THROW_ON_ERROR);
96✔
136
        }
137

138
        $profilePrefix = '';
420✔
139

140
        if ($this->isDisplayTimestamp()) {
420✔
141
            $timestamp = (new \DateTimeImmutable())->format($this->timestampFormat);
20✔
142
            $profilePrefix .= "<green>[</green>{$timestamp}<green>]</green> ";
20✔
143
        }
144

145
        if ($this->isDisplayProfiling()) {
420✔
146
            $profile    = $this->getProfileInfo();
20✔
147
            $memoryDiff = FS::format($profile['memory_usage_diff']);
20✔
148
            $totalTime  = \number_format($profile['time_diff_ms'] / 1000, 3);
20✔
149
            $curMemory  = \str_pad($memoryDiff, 10, ' ', \STR_PAD_LEFT);
20✔
150

151
            $profilePrefix .= "<green>[</green>+{$totalTime}s<green>/</green>{$curMemory}<green>]</green> ";
20✔
152
        }
153

154
        $vNormal = OutputInterface::VERBOSITY_NORMAL;
420✔
155

156
        if ($verboseLevel === OutLvl::DEFAULT) {
420✔
157
            $this->getOutput()->writeln($profilePrefix . $message, $vNormal);
400✔
158
        } elseif ($verboseLevel === OutLvl::V) {
420✔
159
            $this->getOutput()->writeln($profilePrefix . $message, OutputInterface::VERBOSITY_VERBOSE);
96✔
160
        } elseif ($verboseLevel === OutLvl::VV) {
420✔
161
            $this->getOutput()->writeln($profilePrefix . $message, OutputInterface::VERBOSITY_VERY_VERBOSE);
112✔
162
        } elseif ($verboseLevel === OutLvl::VVV) {
420✔
163
            $this->getOutput()->writeln($profilePrefix . $message, OutputInterface::VERBOSITY_DEBUG);
420✔
164
        } elseif ($verboseLevel === OutLvl::Q) {
420✔
165
            $this->getOutput()->writeln($profilePrefix . $message, OutputInterface::VERBOSITY_QUIET); // Show ALWAYS!
172✔
166
        } elseif ($verboseLevel === OutLvl::LEGACY) {
420✔
167
            $this->_('<yellow>Legacy Output:</yellow> ' . $message);
96✔
168
        } elseif ($verboseLevel === OutLvl::DEBUG) {
420✔
169
            $this->_('<magenta>Debug:</magenta> ' . $message, OutLvl::VVV);
420✔
170
        } elseif ($verboseLevel === OutLvl::WARNING) {
112✔
171
            $this->_('<yellow>Warning:</yellow> ' . $message, OutLvl::VV);
112✔
172
        } elseif ($verboseLevel === OutLvl::INFO) {
96✔
173
            $this->_('<blue>Info:</blue> ' . $message, OutLvl::V);
96✔
174
        } elseif ($verboseLevel === OutLvl::E) {
96✔
175
            $this->markOutputHasErrors(true);
96✔
176
            $this->getErrOutput()->writeln($profilePrefix . $message, $vNormal);
96✔
177
        } elseif ($verboseLevel === OutLvl::ERROR) {
96✔
178
            $this->markOutputHasErrors(true);
96✔
179
            $this->getErrOutput()->writeln($profilePrefix . '<red-bg>Error:</red-bg> ' . $message, $vNormal);
96✔
180
        } elseif ($verboseLevel === OutLvl::EXCEPTION) {
96✔
181
            $this->markOutputHasErrors(true);
96✔
182
            $this->getErrOutput()->writeln($profilePrefix . '<red-bg>Muted Exception:</red-bg> ' . $message, $vNormal);
96✔
183
        } else {
184
            throw new Exception("Undefined verbose level: \"{$verboseLevel}\"");
×
185
        }
186
    }
187
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc