1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP-CLI package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ahc\Cli\Output; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Cli Cursor. |
16
|
|
|
* |
17
|
|
|
* @author Jitendra Adhikari <[email protected]> |
18
|
|
|
* @license MIT |
19
|
|
|
* |
20
|
|
|
* @link static https://github.com/adhocore/cli |
21
|
|
|
*/ |
22
|
|
|
class Cursor |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Returns signal to move cursor up `n` times. |
26
|
|
|
* |
27
|
|
|
* @param int $n Times |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function up(int $n = 1): string |
32
|
|
|
{ |
33
|
|
|
return \sprintf("\e[%dA", \max($n, 1)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns signal to move cursor down `n` times. |
38
|
|
|
* |
39
|
|
|
* @param int $n Times |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function down(int $n = 1): string |
44
|
|
|
{ |
45
|
|
|
return \sprintf("\e[%dB", \max($n, 1)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns signal to move cursor right `n` times. |
50
|
|
|
* |
51
|
|
|
* @param int $n Times |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function right(int $n = 1): string |
56
|
|
|
{ |
57
|
|
|
return \sprintf("\e[%dC", \max($n, 1)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns signal to move cursor left `n` times. |
62
|
|
|
* |
63
|
|
|
* @param int $n Times |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function left(int $n = 1): string |
68
|
|
|
{ |
69
|
|
|
return \sprintf("\e[%dD", \max($n, 1)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns signal to move cursor next line `n` times. |
74
|
|
|
* |
75
|
|
|
* @param int $n Times |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function next(int $n = 1): string |
80
|
|
|
{ |
81
|
|
|
return \str_repeat("\e[E", \max($n, 1)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns signal to move cursor prev line `n` times. |
86
|
|
|
* |
87
|
|
|
* @param int $n Times |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function prev(int $n = 1): string |
92
|
|
|
{ |
93
|
|
|
return \str_repeat("\e[F", \max($n, 1)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns signal to erase current line. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function eraseLine(): string |
102
|
|
|
{ |
103
|
|
|
return "\e[2K"; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns signal to clear string. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function clear(): string |
112
|
|
|
{ |
113
|
|
|
return "\e[2J"; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns signal to erase lines upward. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function clearUp(): string |
122
|
|
|
{ |
123
|
|
|
return "\e[1J"; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns signal to erase lines downward. |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function clearDown(): string |
132
|
|
|
{ |
133
|
|
|
return "\e[J"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns signal to move cursor to given x, y position. |
138
|
|
|
* |
139
|
|
|
* @param int $x |
140
|
|
|
* @param int $y |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function moveTo(int $x, int $y): string |
145
|
|
|
{ |
146
|
|
|
return \sprintf("\e[%d;%dH", $y, $x); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|