CliRenderer::escape()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11
 * this software and associated documentation files (the "Software"), to deal in
12
 * the Software without restriction, including without limitation the rights to
13
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14
 * the Software, and to permit persons to whom the Software is furnished to do so,
15
 * subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in all
18
 * copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
 */
27
28
namespace Kint\Renderer;
29
30
use Kint\Value\AbstractValue;
31
use Throwable;
32
33
class CliRenderer extends TextRenderer
34
{
35
    /**
36
     * @var bool enable colors
37
     */
38
    public static bool $cli_colors = true;
39
40
    /**
41
     * Detects the terminal width on startup.
42
     */
43
    public static bool $detect_width = true;
44
45
    /**
46
     * The minimum width to detect terminal size as.
47
     *
48
     * Less than this is ignored and falls back to default width.
49
     */
50
    public static int $min_terminal_width = 40;
51
52
    /**
53
     * Forces utf8 output on windows.
54
     */
55
    public static bool $force_utf8 = false;
56
57
    /**
58
     * Which stream to check for VT100 support on windows.
59
     *
60
     * uses STDOUT by default if it's defined
61
     *
62
     * @psalm-var ?resource
63
     */
64
    public static $windows_stream = null;
65
66
    protected static ?int $terminal_width = null;
67
68
    protected bool $windows_output = false;
69
70
    protected bool $colors = false;
71
72
    public function __construct()
73
    {
74
        parent::__construct();
75
76
        if (!self::$force_utf8 && KINT_WIN) {
77
            if (!\function_exists('sapi_windows_vt100_support')) {
78
                $this->windows_output = true;
79
            } else {
80
                $stream = self::$windows_stream;
81
82
                if (!$stream && \defined('STDOUT')) {
83
                    $stream = STDOUT;
84
                }
85
86
                if (!$stream) {
87
                    $this->windows_output = true;
88
                } else {
89
                    $this->windows_output = !\sapi_windows_vt100_support($stream);
90
                }
91
            }
92
        }
93
94
        if (null === self::$terminal_width) {
95
            if (self::$detect_width) {
96
                try {
97
                    $tput = KINT_WIN ? \exec('tput cols 2>nul') : \exec('tput cols 2>/dev/null');
98
                    if ((bool) $tput) {
99
                        /**
100
                         * @psalm-suppress InvalidCast
101
                         * Psalm bug #11080
102
                         */
103
                        self::$terminal_width = (int) $tput;
104
                    }
105
                } catch (Throwable $t) {
106
                    self::$terminal_width = self::$default_width;
107
                }
108
            }
109
110
            if (!isset(self::$terminal_width) || self::$terminal_width < self::$min_terminal_width) {
111
                self::$terminal_width = self::$default_width;
112
            }
113
        }
114
115
        $this->colors = $this->windows_output ? false : self::$cli_colors;
116
117
        $this->header_width = self::$terminal_width;
118
    }
119
120
    public function colorValue(string $string): string
121
    {
122
        if (!$this->colors) {
123
            return $string;
124
        }
125
126
        return "\x1b[32m".\str_replace("\n", "\x1b[0m\n\x1b[32m", $string)."\x1b[0m";
127
    }
128
129
    public function colorType(string $string): string
130
    {
131
        if (!$this->colors) {
132
            return $string;
133
        }
134
135
        return "\x1b[35;1m".\str_replace("\n", "\x1b[0m\n\x1b[35;1m", $string)."\x1b[0m";
136
    }
137
138
    public function colorTitle(string $string): string
139
    {
140
        if (!$this->colors) {
141
            return $string;
142
        }
143
144
        return "\x1b[36m".\str_replace("\n", "\x1b[0m\n\x1b[36m", $string)."\x1b[0m";
145
    }
146
147
    public function renderTitle(AbstractValue $v): string
148
    {
149
        if ($this->windows_output) {
150
            return $this->utf8ToWindows(parent::renderTitle($v));
151
        }
152
153
        return parent::renderTitle($v);
154
    }
155
156
    public function preRender(): string
157
    {
158
        return PHP_EOL;
159
    }
160
161
    public function postRender(): string
162
    {
163
        if ($this->windows_output) {
164
            return $this->utf8ToWindows(parent::postRender());
165
        }
166
167
        return parent::postRender();
168
    }
169
170
    public function escape(string $string, $encoding = false): string
171
    {
172
        return \str_replace("\x1b", '\\x1b', $string);
173
    }
174
175
    protected function utf8ToWindows(string $string): string
176
    {
177
        return \str_replace(
178
            ['┌', '═', '┐', '│', '└', '─', '┘'],
179
            [' ', '=', ' ', '|', ' ', '-', ' '],
180
            $string
181
        );
182
    }
183
}
184