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

nette / command-line / 20908785598

12 Jan 2026 05:12AM UTC coverage: 97.419% (+0.6%) from 96.859%
20908785598

push

github

dg
added CLAUDE.md

151 of 155 relevant lines covered (97.42%)

0.97 hits per line

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

96.0
/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
                $color ??= '';
1✔
52
                if (func_num_args() > 1) {
1✔
53
                        return $this->colorize(func_get_arg(1), $color); // back compatibility
1✔
54
                } elseif (!$this->useColors) {
1✔
55
                        return '';
1✔
56
                }
57

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

65

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

77

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