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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// devela::sys::os::term::ansi::namespace::cursor
use crate::{__ansi_consts, Ansi, Digits, StringNonul, slice, write_at};
/// # Cursor escape codes
impl Ansi {
__ansi_consts! {
/// Code to make the cursor invisible.
pub const CURSOR_INVISIBLE: [u8; 6] = "\x1b[?25l", *b"\x1b[?25l";
/// Code to make the cursor visible.
pub const CURSOR_VISIBLE: [u8; 6] = "\x1b[?25h", *b"\x1b[?25h";
/// Code to save the cursor position.
pub const CURSOR_SAVE: [u8; 3] = "\x1b[s", *b"\x1b[s";
/// Code to restore the cursor position.
pub const CURSOR_RESTORE: [u8; 3] = "\x1b[u", *b"\x1b[u";
/// Code to move the cursor to the home position (1, 1).
pub const CURSOR_HOME: [u8; 3] = "\x1b[H", *b"\x1b[H";
}
__ansi_consts! {
/// Code to move the cursor to the specified 1-digit position (col, row). (CUP)
/// # Panics
/// Panics in debug if either `row` or `col` > 9.
pub const fn CURSOR_MOVE1(col: u8, row: u8) -> [u8; 6] {
[ b'\x1b', b'[', Digits(row).digits10_1(), b';', Digits(col).digits10_1(), b'H' ]
}
/// Code to move the cursor to the specified 2-digit position (col, row).
/// # Panics
/// Panics in debug if either `row` or `col` > 99.
pub const fn CURSOR_MOVE2(col: u8, row: u8) -> [u8; 8] {
let r: [u8; 2] = Digits(row).digits10_2();
let c: [u8; 2] = Digits(col).digits10_2();
[b'\x1b', b'[', r[0], r[1], b';', c[0], c[1], b'H']
}
/// Code to move the cursor to the specified 3-digit position (col,row).
/// # Panics
/// Panics in debug if either `row` or `col` > 999.
pub const fn CURSOR_MOVE3(col: u16, row: u16) -> [u8; 10] {
let r: [u8; 3] = Digits(row).digits10_3();
let c: [u8; 3] = Digits(col).digits10_3();
[b'\x1b', b'[', r[0], r[1], r[2], b';', c[0], c[1], c[2], b'H']
}
/// Code to move the cursor to the specified 4-digit position (col, row).
/// # Panics
/// Panics in debug if either `row` or `col` > 9999.
pub const fn CURSOR_MOVE4(col: u16, row: u16) -> [u8; 12] {
let r: [u8; 4] = Digits(row).digits10_4();
let c: [u8; 4] = Digits(col).digits10_4();
[b'\x1b', b'[', r[0], r[1], r[2], r[3], b';', c[0], c[1], c[2], c[3], b'H']
}
}
/// Returns a string with the code to move the cursor to the specified position (col, row).
pub const fn CURSOR_MOVE(col: u16, row: u16) -> StringNonul<14> {
let mut buf = [0; 14];
let _ = Self::CURSOR_MOVE_N(&mut buf, col, row);
StringNonul::<14>::_from_array_trusted(buf)
}
__ansi_consts! {
/// Returns a slice with the code to move the cursor to the specified position (col, row).
///
/// The `buffer` must have room for the longest `u16` cursor sequence:
/// `ESC [` + 5 row digits + `;` + 5 column digits + `H`, i.e. 14 bytes.
///
/// # Panics
/// Panics if `buffer.len() < 14`.
pub const fn CURSOR_MOVE_N(buffer: &mut [u8], col: u16, row: u16) -> &[u8] {
assert![buffer.len() >= 14];
buffer[0] = b'\x1b';
buffer[1] = b'[';
let mut index = 2;
index += Digits(row).write_digits10_fast(buffer, index);
write_at![buffer, +=index, b';'];
index += Digits(col).write_digits10_fast(buffer, index);
buffer[index] = b'H';
slice![buffer, ..=index]
}
}
__ansi_consts! {
/// Code to move the cursor up by one line. (CUU)
pub const CURSOR_UP: [u8; 3] = "\x1b[A", *b"\x1b[A";
}
__ansi_consts! {
/// Code to move the cursor up by 1-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9.
pub const fn CURSOR_UP1(n: u8) -> [u8; 4] {
[b'\x1b', b'[', Digits(n).digits10_1(), b'A']
}
/// Code to move the cursor up by 2-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 99.
pub const fn CURSOR_UP2(n: u8) -> [u8; 5] {
let n: [u8; 2] = Digits(n).digits10_2();
[b'\x1b', b'[', n[0], n[1], b'A']
}
/// Code to move the cursor up by 3-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_UP3(n: u16) -> [u8; 6] {
let n: [u8; 3] = Digits(n).digits10_3();
[b'\x1b', b'[', n[0], n[1], n[2], b'A']
}
/// Code to move the cursor up by 4-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9999.
pub const fn CURSOR_UP4(n: u16) -> [u8; 7] {
let n: [u8; 4] = Digits(n).digits10_4();
[b'\x1b', b'[', n[0], n[1], n[2], n[3], b'A']
}
}
__ansi_consts! {
/// Returns a slice with the code to move the cursor up by `n` lines.
///
/// The `buffer` must have room for the longest `u16` cursor sequence:
/// `ESC [` + 5 digits + `A`, i.e. 8 bytes.
///
/// # Panics
/// Panics if `buffer.len() < 8`.
pub const fn CURSOR_UP_N(buffer: &mut [u8], n: u16) -> &[u8] {
Self::write_ansi_code_n(buffer, n, b'A')
}
}
__ansi_consts! {
/// Code to move the cursor down by one line. (CUD)
pub const CURSOR_DOWN: [u8; 3] = "\x1b[B", *b"\x1b[B";
}
__ansi_consts! {
/// Code to move the cursor down by 1-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9.
pub const fn CURSOR_DOWN1(n: u8) -> [u8; 4] {
[b'\x1b', b'[', Digits(n).digits10_1(), b'B']
}
/// Code to move the cursor down by 2-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 99.
pub const fn CURSOR_DOWN2(n: u8) -> [u8; 5] {
let n: [u8; 2] = Digits(n).digits10_2();
[b'\x1b', b'[', n[0], n[1], b'B']
}
/// Code to move the cursor down by 3-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_DOWN3(n: u16) -> [u8; 6] {
let n: [u8; 3] = Digits(n).digits10_3();
[b'\x1b', b'[', n[0], n[1], n[2], b'B']
}
/// Code to move the cursor down by 4-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_DOWN4(n: u16) -> [u8; 7] {
let n: [u8; 4] = Digits(n).digits10_4();
[b'\x1b', b'[', n[0], n[1], n[2], n[3], b'B']
}
}
__ansi_consts! {
/// Returns a slice with the code to move the cursor down by `n` lines.
///
/// The `buffer` must have room for the longest `u16` cursor sequence:
/// `ESC [` + 5 digits + `A`, i.e. 8 bytes.
///
/// # Panics
/// Panics if `buffer.len() < 8`.
pub const fn CURSOR_DOWN_N(buffer: &mut [u8], n: u16) -> &[u8] {
Self::write_ansi_code_n(buffer, n, b'B')
}
}
__ansi_consts! {
/// Code to move the cursor right by one column. (CUF)
pub const CURSOR_RIGHT: [u8; 3] = "\x1b[C", *b"\x1b[C";
}
__ansi_consts! {
/// Code to move the cursor right by 1-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9.
pub const fn CURSOR_RIGHT1(n: u8) -> [u8; 4] {
[b'\x1b', b'[', Digits(n).digits10_1(), b'C']
}
/// Code to move the cursor right by 2-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 99.
pub const fn CURSOR_RIGHT2(n: u8) -> [u8; 5] {
let n: [u8; 2] = Digits(n).digits10_2();
[b'\x1b', b'[', n[0], n[1], b'C']
}
/// Code to move the cursor right by 3-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_RIGHT3(n: u16) -> [u8; 6] {
let n: [u8; 3] = Digits(n).digits10_3();
[b'\x1b', b'[', n[0], n[1], n[2], b'C']
}
/// Code to move the cursor right by 4-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9999.
pub const fn CURSOR_RIGHT4(n: u16) -> [u8; 7] {
let n: [u8; 4] = Digits(n).digits10_4();
[b'\x1b', b'[', n[0], n[1], n[2], n[3], b'C']
}
}
__ansi_consts! {
/// Returns a slice with the code to move the cursor right by `n` lines.
///
/// The `buffer` must have room for the longest `u16` cursor sequence:
/// `ESC [` + 5 digits + `A`, i.e. 8 bytes.
///
/// # Panics
/// Panics if `buffer.len() < 8`.
pub const fn CURSOR_RIGHT_N(buffer: &mut [u8], n: u16) -> &[u8] {
Self::write_ansi_code_n(buffer, n, b'C')
}
}
__ansi_consts! {
/// Code to move the cursor left by one column. (CUB)
pub const CURSOR_LEFT: [u8; 3] = "\x1b[D", *b"\x1b[D";
}
__ansi_consts! {
/// Code to move the cursor left by 1-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9.
pub const fn CURSOR_LEFT1(n: u8) -> [u8; 4] {
[b'\x1b', b'[', Digits(n).digits10_1(), b'D']
}
/// Code to move the cursor left by 3-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 99.
pub const fn CURSOR_LEFT2(n: u8) -> [u8; 5] {
let n: [u8; 2] = Digits(n).digits10_2();
[b'\x1b', b'[', n[0], n[1], b'D']
}
/// Code to move the cursor left by 3-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_LEFT3(n: u16) -> [u8; 6] {
let n: [u8; 3] = Digits(n).digits10_3();
[b'\x1b', b'[', n[0], n[1], n[2], b'D']
}
/// Code to move the cursor left by 4-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9999.
pub const fn CURSOR_LEFT4(n: u16) -> [u8; 7] {
let n: [u8; 4] = Digits(n).digits10_4();
[b'\x1b', b'[', n[0], n[1], n[2], n[3], b'D']
}
}
__ansi_consts! {
/// Returns a slice with the code to move the cursor left by `n` lines.
///
/// The `buffer` must have room for the longest `u16` cursor sequence:
/// `ESC [` + 5 digits + `A`, i.e. 8 bytes.
///
/// # Panics
/// Panics if `buffer.len() < 8`.
pub const fn CURSOR_LEFT_N(buffer: &mut [u8], n: u16) -> &[u8] {
Self::write_ansi_code_n(buffer, n, b'D')
}
}
__ansi_consts! {
/// Code to move the cursor to the beginning of the next line. (CNL)
pub const CURSOR_NEXT_LINE: [u8; 3] = "\x1b[E", *b"\x1b[E";
}
__ansi_consts! {
/// Code to move the cursor to the beginning of the next 1-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9.
pub const fn CURSOR_NEXT_LINE1(n: u8) -> [u8; 4] {
[b'\x1b', b'[', Digits(n).digits10_1(), b'E']
}
/// Code to move the cursor to the beginning of the next 2-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 99.
pub const fn CURSOR_NEXT_LINE2(n: u8) -> [u8; 5] {
let n: [u8; 2] = Digits(n).digits10_2();
[b'\x1b', b'[', n[0], n[1], b'E']
}
/// Code to move the cursor to the beginning of the next 3-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_NEXT_LINE3(n: u16) -> [u8; 6] {
let n: [u8; 3] = Digits(n).digits10_3();
[b'\x1b', b'[', n[0], n[1], n[2], b'E']
}
/// Code to move the cursor to the beginning of the next 4-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_NEXT_LINE4(n: u16) -> [u8; 7] {
let n: [u8; 4] = Digits(n).digits10_4();
[b'\x1b', b'[', n[0], n[1], n[2], n[3], b'E']
}
}
__ansi_consts! {
/// Returns a slice with the code to move the cursor to the beginning of the next `n` lines.
///
/// The `buffer` must have room for the longest `u16` cursor sequence:
/// `ESC [` + 5 digits + `A`, i.e. 8 bytes.
///
/// # Panics
/// Panics if `buffer.len() < 8`.
pub const fn CURSOR_NEXT_LINE_N(buffer: &mut [u8], n: u16) -> &[u8] {
Self::write_ansi_code_n(buffer, n, b'F')
}
}
__ansi_consts! {
/// Code to move the cursor to the beginning of the previous line. (CPL)
pub const CURSOR_PREV_LINE: [u8; 3] = "\x1b[E", *b"\x1b[E";
}
__ansi_consts! {
/// Code to move the cursor to the beginning of the previous 1-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 9.
pub const fn CURSOR_PREV_LINE1(n: u8) -> [u8; 4] {
[b'\x1b', b'[', Digits(n).digits10_1(), b'E']
}
/// Code to move the cursor to the beginning of the previous 2-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 99.
pub const fn CURSOR_PREV_LINE2(n: u8) -> [u8; 5] {
let n: [u8; 2] = Digits(n).digits10_2();
[b'\x1b', b'[', n[0], n[1], b'E']
}
/// Code to move the cursor to the beginning of the previous 3-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_PREV_LINE3(n: u16) -> [u8; 6] {
let n: [u8; 3] = Digits(n).digits10_3();
[b'\x1b', b'[', n[0], n[1], n[2], b'E']
}
/// Code to move the cursor to the beginning of the previous 4-digit `n` lines.
/// # Panics
/// Panics in debug if `n` > 999.
pub const fn CURSOR_PREV_LINE4(n: u16) -> [u8; 7] {
let n: [u8; 4] = Digits(n).digits10_4();
[b'\x1b', b'[', n[0], n[1], n[2], n[3], b'E']
}
}
__ansi_consts! {
/// Returns a slice with the code to move the cursor
/// to the beginning of the previous `n` lines.
///
/// The `buffer` must have room for the longest `u16` cursor sequence:
/// `ESC [` + 5 digits + `A`, i.e. 8 bytes.
///
/// # Panics
/// Panics if `buffer.len() < 8`.
pub const fn CURSOR_PREV_LINE_N(buffer: &mut [u8], n: u16) -> &[u8] {
Self::write_ansi_code_n(buffer, n, b'E')
}}
}