1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/console-diff-renderer. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/console-diff-renderer/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/console-diff-renderer |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DiffRenderer\Terminal; |
15
|
|
|
|
16
|
|
|
class ANSI implements CursorInterface |
17
|
|
|
{ |
18
|
|
|
const ESCAPE = "\e"; |
19
|
|
|
|
20
|
|
|
const CODE_MOVE_POSITION = '[%d;%dH'; // line, column |
21
|
|
|
const CODE_MOVE_UP_LINES = '[%dA'; // lines |
22
|
|
|
const CODE_MOVE_DOWN_LINES = '[%dB'; // lines |
23
|
|
|
const CODE_MOVE_FORWARD = '[%dC'; // columns |
24
|
|
|
const CODE_MOVE_BACKWARDS = '[%dD'; // columns |
25
|
|
|
|
26
|
|
|
const CODE_ERASE_TO_END_OF_LINE = '[K'; |
27
|
|
|
const CODE_ERASE_TO_START_OF_LINE = '[1K'; |
28
|
|
|
const CODE_ERASE_LINE = '[2K'; |
29
|
|
|
const CODE_ERASE_DOWN = '[J'; |
30
|
|
|
const CODE_ERASE_UP = '[1J'; |
31
|
|
|
const CODE_ERASE_SCREEN = '[2J'; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
protected $filter = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int $line |
38
|
|
|
* @param int $column |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function move($line, $column) |
43
|
|
|
{ |
44
|
|
|
return static::ESCAPE . sprintf(static::CODE_MOVE_POSITION, $line, $column); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param int $lines |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function moveUp($lines) |
53
|
|
|
{ |
54
|
|
|
return static::ESCAPE . sprintf(static::CODE_MOVE_UP_LINES, $lines); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param int $lines |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function moveDown($lines) |
63
|
|
|
{ |
64
|
|
|
return static::ESCAPE . sprintf(static::CODE_MOVE_DOWN_LINES, $lines); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $columns |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function moveLeft($columns) |
73
|
|
|
{ |
74
|
|
|
return static::ESCAPE . sprintf(static::CODE_MOVE_BACKWARDS, $columns); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $columns |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function moveRight($columns) |
83
|
|
|
{ |
84
|
|
|
return static::ESCAPE . sprintf(static::CODE_MOVE_FORWARD, $columns); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function eraseToEnd() |
91
|
|
|
{ |
92
|
|
|
return static::ESCAPE . static::CODE_ERASE_TO_END_OF_LINE; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function eraseToStart() |
99
|
|
|
{ |
100
|
|
|
return static::ESCAPE . static::CODE_ERASE_TO_START_OF_LINE; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function eraseDown() |
107
|
|
|
{ |
108
|
|
|
return static::ESCAPE . static::CODE_ERASE_DOWN; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function eraseUp() |
115
|
|
|
{ |
116
|
|
|
return static::ESCAPE . static::CODE_ERASE_UP; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function eraseScreen() |
123
|
|
|
{ |
124
|
|
|
return static::ESCAPE . static::CODE_ERASE_SCREEN; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Filter takes a string with Cursor movements and filters them out |
129
|
|
|
* |
130
|
|
|
* @param string $string |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function filter($string) |
135
|
|
|
{ |
136
|
|
|
if (count($this->filter) === 0) { |
137
|
|
|
$items = [ |
138
|
|
|
self::ESCAPE . self::CODE_MOVE_POSITION, |
139
|
|
|
self::ESCAPE . self::CODE_MOVE_UP_LINES, |
140
|
|
|
self::ESCAPE . self::CODE_MOVE_DOWN_LINES, |
141
|
|
|
self::ESCAPE . self::CODE_MOVE_FORWARD, |
142
|
|
|
self::ESCAPE . self::CODE_MOVE_BACKWARDS, |
143
|
|
|
self::ESCAPE . self::CODE_ERASE_TO_END_OF_LINE, |
144
|
|
|
self::ESCAPE . self::CODE_ERASE_TO_START_OF_LINE, |
145
|
|
|
self::ESCAPE . self::CODE_ERASE_LINE, |
146
|
|
|
self::ESCAPE . self::CODE_ERASE_DOWN, |
147
|
|
|
self::ESCAPE . self::CODE_ERASE_UP, |
148
|
|
|
self::ESCAPE . self::CODE_ERASE_SCREEN, |
149
|
|
|
"\r", |
150
|
|
|
"\n", |
151
|
|
|
]; |
152
|
|
|
$this->filter = array_map(function ($line) { |
153
|
|
|
return '/' . str_replace(['%d', '['], ['\d+', '\['], $line) . '/'; |
154
|
|
|
}, $items); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return preg_replace($this->filter, '', $string); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|