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

nette / command-line / 22834539707

09 Mar 2026 01:23AM UTC coverage: 96.226%. Remained the same
22834539707

push

github

dg
removed deprecated stuff

153 of 159 relevant lines covered (96.23%)

0.96 hits per line

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

90.0
/src/CommandLine/Console.php
1
<?php declare(strict_types=1);
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
namespace Nette\CommandLine;
9

10

11
/**
12
 * Stupid console writer.
13
 */
14
class Console
15
{
16
        private bool $useColors;
17

18

19
        public function __construct()
20
        {
21
                $this->useColors = self::detectColors();
1✔
22
        }
1✔
23

24

25
        public function useColors(bool $state = true): void
1✔
26
        {
27
                $this->useColors = $state;
1✔
28
        }
1✔
29

30

31
        /**
32
         * Wraps string in ANSI color codes, or returns plain string when colors are disabled.
33
         * Color format: 'foreground' or 'foreground/background' (e.g. 'red', 'white/blue').
34
         * When $s is null, emits the escape code without a reset sequence.
35
         * Available colors: black, gray, silver, white, navy, blue, green, lime,
36
         * teal, aqua, maroon, red, purple, fuchsia, olive, yellow.
37
         */
38
        public function color(?string $color, ?string $s = null): string
1✔
39
        {
40
                $colors = [
1✔
41
                        'black' => '0;30', 'gray' => '1;30', 'silver' => '0;37', 'white' => '1;37',
42
                        'navy' => '0;34', 'blue' => '1;34', 'green' => '0;32', 'lime' => '1;32',
43
                        'teal' => '0;36', 'aqua' => '1;36', 'maroon' => '0;31', 'red' => '1;31',
44
                        'purple' => '0;35', 'fuchsia' => '1;35', 'olive' => '0;33', 'yellow' => '1;33',
45
                        '' => '0',
46
                ];
47
                if ($this->useColors) {
1✔
48
                        $c = explode('/', $color ?: '/');
1✔
49
                        return "\033["
50
                                . ($c[0] ? $colors[$c[0]] : '')
1✔
51
                                . (empty($c[1]) ? '' : ';4' . substr($colors[$c[1]], -1))
1✔
52
                                . 'm' . $s
1✔
53
                                . ($s === null ? '' : "\033[0m");
1✔
54
                }
55

56
                return (string) $s;
×
57
        }
58

59

60
        /**
61
         * Detects whether the terminal supports ANSI colors.
62
         * Returns false when NO_COLOR is set, or when not running in a CLI TTY.
63
         * On Windows, checks VT100 support via sapi_windows_vt100_support().
64
         */
65
        public static function detectColors(): bool
66
        {
67
                return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')
1✔
68
                        && getenv('NO_COLOR') === false // https://no-color.org
1✔
69
                        && (getenv('FORCE_COLOR')
1✔
70
                                || (function_exists('sapi_windows_vt100_support')
1✔
71
                                        ? sapi_windows_vt100_support(STDOUT)
×
72
                                        : @stream_isatty(STDOUT)) // @ may trigger error 'cannot cast a filtered stream on this system'
1✔
73
                        );
74
        }
75
}
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