forked from mlocati/postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminal.php
More file actions
118 lines (100 loc) · 2.85 KB
/
Terminal.php
File metadata and controls
118 lines (100 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace PostCSS;
/**
* @link https://github.com/postcss/postcss/blob/master/lib/terminal-highlight.es6
*/
class Terminal
{
protected static function getHighlightTheme()
{
return [
'brackets' => [36, 39], // cyan
'string' => [31, 39], // red
'at-word' => [31, 39], // red
'comment' => [90, 39], // gray
'{' => [32, 39], // green
'}' => [32, 39], // green
':' => [1, 22], // bold
';' => [1, 22], // bold
'(' => [1, 22], // bold
')' => [1, 22], // bold
];
}
public static function code($color)
{
return "\x1b".$color.'m';
}
public static function highlight($css)
{
$highlightTheme = static::getHighlightTheme();
$tokens = Parser::getTokens(new Input($css), ['ignoreErrors' => true]);
$result = [];
foreach ($tokens as $token) {
if (isset($highlightTheme[$token[0]])) {
$color = $highlightTheme[$token[0]];
$chunks = preg_split('/\r?\n/', $token[1]);
foreach ($chunks as $i => $chunk) {
if ($i > 0) {
$result[] = "\n";
}
$result[] = static::code($color[0]).$chunk.static::code($color[1]);
}
} else {
$result[] = $token[1];
}
}
return implode('', $result);
}
protected static function colorize($text, $before, $after)
{
return static::code($before).$text.static::code($after);
}
public static function black($text)
{
return static::colorize($text, 30, 39);
}
public static function red($text)
{
return static::colorize($text, 31, 39);
}
public static function green($text)
{
return static::colorize($text, 32, 39);
}
public static function yellow($text)
{
return static::colorize($text, 33, 39);
}
public static function blue($text)
{
return static::colorize($text, 34, 39);
}
public static function magenta($text)
{
return static::colorize($text, 35, 39);
}
public static function cyan($text)
{
return static::colorize($text, 36, 39);
}
public static function white($text)
{
return static::colorize($text, 37, 39);
}
public static function gray($text)
{
return static::colorize($text, 90, 39);
}
public static function grey($text)
{
return static::gray($text);
}
public static function stripAnsi($text)
{
$result = (string) $text;
if ($result !== '') {
$result = preg_replace('/[\x1b\x9b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/', $result, '');
}
return $text;
}
}