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

nette / command-line / 20908440499

12 Jan 2026 04:50AM UTC coverage: 96.859% (+2.6%) from 94.231%
20908440499

push

github

dg
added Test class

27 of 27 new or added lines in 1 file covered. (100.0%)

6 existing lines in 2 files now uncovered.

185 of 191 relevant lines covered (96.86%)

0.97 hits per line

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

91.18
/src/CommandLine/Console.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\CommandLine;
11

12
use const PHP_SAPI, STDOUT;
13

14

15
/**
16
 * Terminal/console utilities for ANSI output.
17
 */
18
class Console
19
{
20
        /** ANSI sequence to clear from cursor to end of line */
21
        public const ClearLine = "\e[K";
22

23
        /** ANSI sequence to reset all attributes */
24
        public const Reset = "\e[0m";
25

26
        /** ANSI sequence for bold text */
27
        public const BoldOn = "\e[1m";
28

29
        /** ANSI sequence to turn off bold */
30
        public const BoldOff = "\e[22m";
31

32
        private const Colors = [
33
                'black' => '0;30', 'gray' => '1;30', 'silver' => '0;37', 'white' => '1;37',
34
                'navy' => '0;34', 'blue' => '1;34', 'green' => '0;32', 'lime' => '1;32',
35
                'teal' => '0;36', 'aqua' => '1;36', 'maroon' => '0;31', 'red' => '1;31',
36
                'purple' => '0;35', 'fuchsia' => '1;35', 'olive' => '0;33', 'yellow' => '1;33',
37
                '' => '0',
38
        ];
39

40
        private bool $useColors;
41

42

43
        public function __construct()
44
        {
45
                $this->useColors = self::supportsColors();
1✔
46
        }
1✔
47

48

49
        public function enableColors(bool $enable = true): void
1✔
50
        {
51
                $this->useColors = $enable;
1✔
52
        }
1✔
53

54

55
        /**
56
         * Returns ANSI escape sequence for given color.
57
         * Color format: 'foreground' or 'foreground/background' (e.g. 'red', 'white/blue').
58
         */
59
        public function color(?string $color): string
1✔
60
        {
61
                $color ??= '';
1✔
62
                if (func_num_args() > 1) {
1✔
63
                        return $this->colorize(func_get_arg(1), $color); // back compatibility
1✔
64
                } elseif (!$this->useColors) {
1✔
65
                        return '';
1✔
66
                }
67

68
                $c = explode('/', $color);
1✔
69
                return "\e["
70
                        . self::Colors[$c[0]]
1✔
71
                        . (empty($c[1]) ? '' : ';4' . substr(self::Colors[$c[1]], -1))
1✔
72
                        . 'm';
1✔
73
        }
74

75

76
        /**
77
         * Wraps text with ANSI color sequence and reset.
78
         * Color format: 'foreground' or 'foreground/background' (e.g. 'red', 'white/blue').
79
         */
80
        public function colorize(string $text, string $color): string
1✔
81
        {
82
                return $this->useColors
1✔
83
                        ? $this->color($color) . $text . self::Reset
1✔
84
                        : $text;
1✔
85
        }
86

87

88
        /**
89
         * Returns ANSI sequence to show or hide cursor.
90
         */
91
        public function cursorVisible(bool $state): string
1✔
92
        {
93
                return $this->useColors
1✔
94
                        ? ($state ? "\e[?25h" : "\e[?25l")
1✔
95
                        : '';
1✔
96
        }
97

98

99
        /**
100
         * Returns ANSI sequence to move cursor up.
101
         */
102
        public function cursorUp(int $lines): string
1✔
103
        {
104
                return $this->useColors
1✔
105
                        ? "\e[{$lines}A"
1✔
106
                        : '';
1✔
107
        }
108

109

110
        /**
111
         * Detects whether the terminal supports colored output.
112
         * Respects NO_COLOR standard and FORCE_COLOR env variable.
113
         */
114
        public static function supportsColors(): bool
115
        {
116
                return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')
117
                        && getenv('NO_COLOR') === false // https://no-color.org
1✔
118
                        && (getenv('FORCE_COLOR')
1✔
119
                                || (function_exists('sapi_windows_vt100_support')
1✔
UNCOV
120
                                        ? sapi_windows_vt100_support(STDOUT)
×
121
                                        : @stream_isatty(STDOUT)) // @ may trigger error 'cannot cast a filtered stream on this system'
1✔
122
                        );
123
        }
124

125

126
        #[\Deprecated('use enableColors()')]
127
        public function useColors(bool $state = true): void
128
        {
UNCOV
129
                $this->enableColors($state);
×
130
        }
131

132

133
        #[\Deprecated('use supportsColors()')]
134
        public static function detectColors(): bool
135
        {
UNCOV
136
                return self::supportsColors();
×
137
        }
138
}
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