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 Terminal implements TerminalInterface |
17
|
|
|
{ |
18
|
|
|
/** @var CursorInterface */ |
19
|
|
|
protected $cursor; |
20
|
|
|
/** @var DimensionsInterface */ |
21
|
|
|
private $dimensions; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Terminal will provide cursor and dimension information to the outputter |
25
|
|
|
* |
26
|
|
|
* @param CursorInterface|null $cursor |
27
|
|
|
* @param DimensionsInterface $dimensions |
28
|
|
|
*/ |
29
|
|
|
public function __construct(CursorInterface $cursor = null, DimensionsInterface $dimensions = null) |
30
|
|
|
{ |
31
|
|
|
$this->cursor = $cursor ?: new ANSI(); |
32
|
|
|
$this->dimensions = $dimensions ?: new TerminalDimensions(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return int |
37
|
|
|
*/ |
38
|
|
|
public function getWidth() |
39
|
|
|
{ |
40
|
|
|
return $this->dimensions->getWidth(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function getHeight() |
47
|
|
|
{ |
48
|
|
|
return $this->dimensions->getHeight(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Move the cursor to y,x |
53
|
|
|
* |
54
|
|
|
* @param int $line |
55
|
|
|
* @param int $column |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function move($line, $column) |
60
|
|
|
{ |
61
|
|
|
return $this->cursor->move($line, $column); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Move up n Lines |
66
|
|
|
* |
67
|
|
|
* @param int $lines |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function moveUp($lines) |
72
|
|
|
{ |
73
|
|
|
return $this->cursor->moveUp($lines); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Move down n Lines |
78
|
|
|
* |
79
|
|
|
* @param int $lines |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function moveDown($lines) |
84
|
|
|
{ |
85
|
|
|
return $this->cursor->moveDown($lines); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Move left n Columns |
90
|
|
|
* |
91
|
|
|
* @param int $columns |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function moveLeft($columns) |
96
|
|
|
{ |
97
|
|
|
return $this->cursor->moveLeft($columns); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Move right n Columns |
102
|
|
|
* |
103
|
|
|
* @param int $columns |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function moveRight($columns) |
108
|
|
|
{ |
109
|
|
|
return $this->cursor->moveRight($columns); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Erase to the end of the line |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function eraseToEnd() |
118
|
|
|
{ |
119
|
|
|
return $this->cursor->eraseToEnd(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Erase to the start of the line |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function eraseToStart() |
128
|
|
|
{ |
129
|
|
|
return $this->cursor->eraseToStart(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Erase Down |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function eraseDown() |
138
|
|
|
{ |
139
|
|
|
return $this->cursor->eraseDown(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Erase Up |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function eraseUp() |
148
|
|
|
{ |
149
|
|
|
return $this->cursor->eraseUp(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Erase entire screen |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function eraseScreen() |
158
|
|
|
{ |
159
|
|
|
return $this->cursor->eraseScreen(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function hideCursor() |
166
|
|
|
{ |
167
|
|
|
return $this->cursor->hideCursor(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function showCursor() |
174
|
|
|
{ |
175
|
|
|
return $this->cursor->showCursor(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Filter takes a string with Cursor movements and filters them out |
180
|
|
|
* |
181
|
|
|
* @param string $string |
182
|
|
|
* @param string $replacement Optional replacement for each item |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function filter($string, $replacement = '') |
187
|
|
|
{ |
188
|
|
|
return $this->cursor->filter($string, $replacement); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the current formatting for this string |
193
|
|
|
* |
194
|
|
|
* @param string $string |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getCurrentFormatting($string) |
199
|
|
|
{ |
200
|
|
|
return $this->cursor->getCurrentFormatting($string); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|