1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Cli\Tools; |
4
|
|
|
|
5
|
|
|
use const AlecRabbit\CSI; |
6
|
|
|
use const AlecRabbit\ESC; |
7
|
|
|
|
8
|
|
|
class Cursor |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Show cursor sequence |
12
|
|
|
* |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
1 |
|
public static function show(): string |
16
|
|
|
{ |
17
|
1 |
|
return CSI . '?25h' . CSI . '?0c'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Hide cursor sequence |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
1 |
|
public static function hide(): string |
26
|
|
|
{ |
27
|
1 |
|
return CSI . '?25l'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Move cursor up sequence |
32
|
|
|
* |
33
|
|
|
* @param int $rows |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
1 |
|
public static function up(int $rows = 1): string |
37
|
|
|
{ |
38
|
1 |
|
return CSI . "{$rows}A"; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Move cursor up to begin of the line sequence |
43
|
|
|
* |
44
|
|
|
* @param int $rows |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
1 |
|
public static function upLine(int $rows = 1): string |
48
|
|
|
{ |
49
|
1 |
|
return CSI . "{$rows}F"; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Move cursor down to begin of the line sequence |
54
|
|
|
* |
55
|
|
|
* @param int $rows |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
1 |
|
public static function downLine(int $rows = 1): string |
59
|
|
|
{ |
60
|
1 |
|
return CSI . "{$rows}E"; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Move cursor down sequence |
65
|
|
|
* |
66
|
|
|
* @param int $rows |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
1 |
|
public static function down(int $rows = 1): string |
70
|
|
|
{ |
71
|
1 |
|
return CSI . "{$rows}B"; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Move cursor forward sequence |
76
|
|
|
* |
77
|
|
|
* @param int $cols |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
1 |
|
public static function forward(int $cols = 1): string |
81
|
|
|
{ |
82
|
1 |
|
return CSI . "{$cols}C"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Move cursor back sequence |
87
|
|
|
* |
88
|
|
|
* @param int $cols |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
1 |
|
public static function back(int $cols = 1): string |
92
|
|
|
{ |
93
|
1 |
|
return CSI . "{$cols}D"; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Move cursor to sequence |
98
|
|
|
* |
99
|
|
|
* @param int $col |
100
|
|
|
* @param int $row |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
1 |
|
public static function goTo(int $col = 1, int $row = 1): string |
104
|
|
|
{ |
105
|
1 |
|
return CSI . "{$row};{$col}f"; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Move cursor to position in current line sequence |
110
|
|
|
* |
111
|
|
|
* @param int $col |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
1 |
|
public static function absX(int $col = 1): string |
115
|
|
|
{ |
116
|
1 |
|
return CSI . "{$col}G"; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Move cursor to position in current column sequence |
121
|
|
|
* |
122
|
|
|
* @param int $row |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
1 |
|
public static function absY(int $row = 1): string |
126
|
|
|
{ |
127
|
1 |
|
return CSI . "{$row}d"; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Save cursor position sequence |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
1 |
|
public static function savePosition(): string |
136
|
|
|
{ |
137
|
1 |
|
return CSI . 's'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Restore cursor position sequence |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
1 |
|
public static function restorePosition(): string |
146
|
|
|
{ |
147
|
1 |
|
return CSI . 'u'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Save cursor position and attributes sequence |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
1 |
|
public static function save(): string |
156
|
|
|
{ |
157
|
1 |
|
return ESC . '7'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Restore cursor position and attributes sequence |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
1 |
|
public static function restore(): string |
166
|
|
|
{ |
167
|
1 |
|
return ESC . '8'; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|