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

nette / command-line / 20911287643

12 Jan 2026 07:28AM UTC coverage: 96.894% (+0.5%) from 96.35%
20911287643

push

github

dg
removed deprecated stuff

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

5 existing lines in 2 files now uncovered.

156 of 161 relevant lines covered (96.89%)

0.97 hits per line

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

95.45
/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

13
/**
14
 * Stupid console writer.
15
 */
16
class Console
17
{
18
        /** ANSI sequence to reset all attributes */
19
        public const Reset = "\e[0m";
20

21
        /** ANSI sequence for bold text */
22
        private const Colors = [
23
                'black' => '0;30', 'gray' => '1;30', 'silver' => '0;37', 'white' => '1;37',
24
                'navy' => '0;34', 'blue' => '1;34', 'green' => '0;32', 'lime' => '1;32',
25
                'teal' => '0;36', 'aqua' => '1;36', 'maroon' => '0;31', 'red' => '1;31',
26
                'purple' => '0;35', 'fuchsia' => '1;35', 'olive' => '0;33', 'yellow' => '1;33',
27
                '' => '0',
28
        ];
29

30
        private bool $useColors;
31

32

33
        public function __construct()
34
        {
35
                $this->useColors = self::detectColors();
1✔
36
        }
1✔
37

38

39
        public function useColors(bool $state = true): void
1✔
40
        {
41
                $this->useColors = $state;
1✔
42
        }
1✔
43

44

45
        /**
46
         * Returns ANSI escape sequence for given color.
47
         * Color format: 'foreground' or 'foreground/background' (e.g. 'red', 'white/blue').
48
         */
49
        public function color(string $color): string
1✔
50
        {
51
                if (!$this->useColors) {
1✔
52
                        return '';
1✔
53
                }
54

55
                $c = explode('/', $color);
1✔
56
                return "\e["
57
                        . self::Colors[$c[0]]
1✔
58
                        . (empty($c[1]) ? '' : ';4' . substr(self::Colors[$c[1]], -1))
1✔
59
                        . 'm';
1✔
60
        }
61

62

63
        /**
64
         * Wraps text with ANSI color sequence and reset.
65
         * Color format: 'foreground' or 'foreground/background' (e.g. 'red', 'white/blue').
66
         */
67
        public function colorize(string $text, string $color): string
1✔
68
        {
69
                return $this->useColors
1✔
70
                        ? $this->color($color) . $text . self::Reset
1✔
71
                        : $text;
1✔
72
        }
73

74

75
        public static function detectColors(): bool
76
        {
77
                return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')
1✔
78
                        && getenv('NO_COLOR') === false // https://no-color.org
1✔
79
                        && (getenv('FORCE_COLOR')
1✔
80
                                || (function_exists('sapi_windows_vt100_support')
1✔
UNCOV
81
                                        ? sapi_windows_vt100_support(STDOUT)
×
82
                                        : @stream_isatty(STDOUT)) // @ may trigger error 'cannot cast a filtered stream on this system'
1✔
83
                        );
84
        }
85
}
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