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

nette / command-line / 3678356221

pending completion
3678356221

push

github

David
updated detectColors()

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

96 of 102 relevant lines covered (94.12%)

0.94 hits per line

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

90.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
        /** @var bool */
19
        private $useColors;
20

21

22
        public function __construct()
23
        {
24
                $this->useColors = self::detectColors();
1✔
25
        }
1✔
26

27

28
        public function useColors(bool $state = true): void
1✔
29
        {
30
                $this->useColors = $state;
1✔
31
        }
1✔
32

33

34
        public function color(?string $color, ?string $s = null): string
1✔
35
        {
36
                $colors = [
37
                        'black' => '0;30', 'gray' => '1;30', 'silver' => '0;37', 'white' => '1;37',
1✔
38
                        'navy' => '0;34', 'blue' => '1;34', 'green' => '0;32', 'lime' => '1;32',
39
                        'teal' => '0;36', 'aqua' => '1;36', 'maroon' => '0;31', 'red' => '1;31',
40
                        'purple' => '0;35', 'fuchsia' => '1;35', 'olive' => '0;33', 'yellow' => '1;33',
41
                        null => '0',
42
                ];
43
                if ($this->useColors) {
1✔
44
                        $c = explode('/', $color ?: '/');
1✔
45
                        return "\033["
46
                                . ($c[0] ? $colors[$c[0]] : '')
1✔
47
                                . (empty($c[1]) ? '' : ';4' . substr($colors[$c[1]], -1))
1✔
48
                                . 'm' . $s
1✔
49
                                . ($s === null ? '' : "\033[0m");
1✔
50
                }
51

52
                return (string) $s;
×
53
        }
54

55

56
        public static function detectColors(): bool
57
        {
58
                return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')
1✔
59
                        && getenv('NO_COLOR') === false // https://no-color.org
1✔
60
                        && (getenv('FORCE_COLOR')
1✔
61
                                || (function_exists('sapi_windows_vt100_support')
1✔
62
                                        ? sapi_windows_vt100_support(STDOUT)
×
63
                                        : @stream_isatty(STDOUT)) // @ may trigger error 'cannot cast a filtered stream on this system'
1✔
64
                        );
65
        }
66
}
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

© 2025 Coveralls, Inc