devela 0.27.0

A development layer of coherence.
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// devela::sys::os::term::ansi::namespace
//
//! Defines the [`Ansi`] namespace for emitting ANSI codes.
//

use crate::{_ansi_consts, Digits, StringNonul, slice, write_at};

#[doc = crate::_tags!(term namespace)]
/// ANSI escape codes.
#[doc = crate::_doc_location!("sys/os/term")]
///
/// # Return type
/// Constants ending with `_B` return a byte array. Those without it return a string slice.
///
/// Functions ending with `_B` return either an array or a byte slice.
/// Those without it return a [`StringNonul`] or a string slice, respectively.
///
/// # Example
/// ```
/// use devela::Ansi;
/// assert_eq![Ansi::ITALIC_B, *b"\x1b[3m"];
/// assert_eq![Ansi::ITALIC, "\x1b[3m"];
/// ```
///
/// # Methods
/// ## Escape codes
/// - [Screen][Self#screen-escape-codes]
/// - [Erase][Self#erase-escape-codes]
/// - [Cursor][Self#cursor-escape-codes]
/// - [Mouse][Self#mouse-escape-codes]
/// - [Font effects][Self#font-effects-escape-codes]
/// - [Color (3-bit)][Self#3-bit-color-escape-codes]
/// - [Color (8-bit)][Self#8-bit-color-escape-codes]
/// - [Grey (8-bit) Palette][Self#8-bit-grey-escape-codes]
/// - [Palette (8-bit)][Self#8-bit-palette-escape-codes]
/// - [Default][Self#default-color-escape-codes]
/// - [Color (rgb)][Self#rgb-color-escape-codes]
///
/// ## Other
/// - [`strip_codes`](Self#strip_codes)
///
/// # Coordinate Order
/// All cursor positioning functions use `(columns, rows)` order, equivalent to `(x, y)`.
/// This follows graphics API conventions but is the **reverse** of the underlying
/// ANSI sequence which uses `[row;column]` order in escape sequences.
///
/// # Related Items
/// See also the [`ansi!`] macro.
#[doc = crate::doclink!(custom devela "[`ansi!`]" "sys/os/term/macro.ansi.html")]
#[derive(Debug)]
pub struct Ansi;

impl Ansi {
    _ansi_consts! {
        /// Control Sequence Introducer (CSI).
        ///
        /// <https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands>
        pub const CSI: [u8; 2] = "\x1b[", *b"\x1b[";
    }

    /* helper functions */

    // Writes an ansi code with a dynamic number of digits as an argument.
    #[must_use]
    const fn write_ansi_code_n(buffer: &mut [u8], n: u16, final_byte: u8) -> &[u8] {
        buffer[0] = b'\x1b';
        buffer[1] = b'[';
        let mut index = 2;
        index += Digits(n).write_digits10(buffer, index);
        buffer[index] = final_byte;
        slice![buffer, ..=index]
    }
}

/// # Screen escape codes
impl Ansi {
    _ansi_consts! {
        /// Code to enable the alternative screen.
        pub const ENABLE_ALT_SCREEN: [u8; 8] = "\x1b[?1049h", *b"\x1b[?1049h";
        /// Code to disable the alternative screen.
        pub const DISABLE_ALT_SCREEN: [u8; 8] = "\x1b[?1049l", *b"\x1b[?1049l";
    }
}

/// # Erase escape codes
impl Ansi {
    _ansi_consts! {
        /// Code to erase from the cursor to the end of the line.
        pub const ERASE_LINE_END: [u8; 3] = "\x1b[K", *b"\x1b[K"; // also "\x1b[0K"
        /// Code to erase from the cursor to the start of the line.
        pub const ERASE_LINE_START: [u8; 4] = "\x1b[1K", *b"\x1b[1K";
        /// Code to erase the entire line.
        pub const ERASE_LINE: [u8; 4] = "\x1b[2K", *b"\x1b[2K";
        /// Code to erase from the cursor to the end of the screen.
        pub const ERASE_SCREEN_END: [u8; 3] = "\x1b[J", *b"\x1b[J"; // also "\x1b[0J"
        /// Code to erase from the cursor to the start of the screen.
        pub const ERASE_SCREEN_START: [u8; 4] = "\x1b[1J", *b"\x1b[1J";
        /// Code to erase the entire screen.
        pub const ERASE_SCREEN: [u8; 4] = "\x1b[2J", *b"\x1b[2J";
    }
}

/// # 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).
        ///
        /// It needs a `buffer` where to store the bytes.
        ///
        /// # Panics
        /// Panics if the buffer is not big enough.
        pub const fn CURSOR_MOVE_N(buffer: &mut [u8], col: u16, row: u16) -> &[u8] {
            buffer[0] = b'\x1b';
            buffer[1] = b'[';
            let mut index = 2;
            index += Digits(row).write_digits10(buffer, index);
            write_at![buffer, +=index, b';'];
            index += Digits(col).write_digits10(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.
        ///
        /// It needs a `buffer` where to store the bytes.
        ///
        /// # Panics
        /// Panics if the buffer is not big enough.
        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.
        ///
        /// It needs a `buffer` where to store the bytes.
        ///
        /// # Panics
        /// Panics if the buffer is not big enough.
        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.
        ///
        /// It needs a `buffer` where to store the bytes.
        ///
        /// # Panics
        /// Panics if the buffer is not big enough.
        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.
        ///
        /// It needs a `buffer` where to store the bytes.
        ///
        /// # Panics
        /// Panics if the buffer is not big enough.
        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.
        ///
        /// It needs a `buffer` where to store the bytes.
        ///
        /// # Panics
        /// Panics if the buffer is not big enough.
        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.
    ///
    /// It needs a `buffer` where to store the bytes.
    ///
    /// # Panics
    /// Panics if the buffer is not big enough.
    pub const fn CURSOR_PREV_LINE_N(buffer: &mut [u8], n: u16) -> &[u8] {
        Self::write_ansi_code_n(buffer, n, b'E')
    }}
}

/// # Mouse escape codes
///
/// Example SGR mouse event received:
/// `ESC [ < 0 ; 45 ; 10 M`, where:
/// - `0`: button state
/// - `45`: column
/// - `10`: row
/// -  `M`: press (`m` indicates release)
impl Ansi {
    _ansi_consts! {
        /// Code to enable X10 compatibility mode for mouse click reporting.
        ///
        /// Response: `CSI M Cb Cx Cy` (fixed-length, byte-encoded).
        /// - Cb is button (1, 2 or 3) plus 32.
        /// - Cx and Cy are column and row plus 32.
        pub const MOUSE_X10_ENABLE: [u8; 5] = "\x1b[?9h", *b"\x1b[?9h";
        /// Code to disable X10 compatibility mode.
        pub const MOUSE_X10_DISABLE: [u8; 5] = "\x1b[?9l", *b"\x1b[?9l";

        // /// Code to enable basic mouse click reporting.
        // pub const MOUSE_NORMAL: [u8; 8] = "\x1b[?1000h", *b"\x1b[?1000h";
        // /// Code to enable all motion tracking.
        // pub const MOUSE_TRACKING: [u8; 8] = "\x1b[?1003h", *b"\x1b[?1003h";
        /// Code to enable UTF-8 extended mouse coordinates (legacy) with report in cells.
        pub const MOUSE_UTF8: [u8; 8] = "\x1b[?1005h", *b"\x1b[?1005h";
        /// Code to enable SGR extended mouse coordinates with report in cells.
        pub const MOUSE_SGR: [u8; 8] = "\x1b[?1006h", *b"\x1b[?1006h";
        /// Code to enable SGR extended coordinates with report in pixels.
        pub const MOUSE_SGR_PIXELS: [u8; 8] = "\x1b[?1016h", *b"\x1b[?1016h"; // = SGR + pixel mode
    }
}

/// # Font effects escape codes
impl Ansi {
    _ansi_consts! {
        /// Code to turn off all effects and colors.
        pub const RESET: [u8; 4] = "\x1b[0m", *b"\x1b[0m";

        /// Code to set bold effect.
        pub const BOLD: [u8; 4] = "\x1b[1m", *b"\x1b[1m";
        /// Code to unset bold and dim effects.
        pub const BOLD_OFF: [u8; 5] = "\x1b[22m", *b"\x1b[22m";

        /// Code to set italic effect.
        pub const ITALIC: [u8; 4] = "\x1b[3m", *b"\x1b[3m";
        /// Code to unset italic and fraktur effects.
        pub const ITALIC_OFF: [u8; 5] = "\x1b[23m", *b"\x1b[23m";

        /// Code to set dim effect.
        pub const DIM: [u8; 4] = "\x1b[2m", *b"\x1b[2m";
        /// Code to unset bold and dim effects.
        pub const DIM_OFF: [u8; 5] = "\x1b[22m", *b"\x1b[22m";

        /// Code to set underline effect.
        pub const UNDERLINE: [u8; 4] = "\x1b[4m", *b"\x1b[4m";
        /// Code to unset underline effect.
        pub const UNDERLINE_OFF: [u8; 5] = "\x1b[24m", *b"\x1b[24m";

        /// Code to set blink effect.
        pub const BLINK: [u8; 4] = "\x1b[5m", *b"\x1b[5m";
        /// Code to unset blink effect.
        pub const BLINK_OFF: [u8; 5] = "\x1b[25m", *b"\x1b[25m";

        /// Code to set inverse effect.
        pub const INVERSE: [u8; 4] = "\x1b[7m", *b"\x1b[7m";
        /// Code to unset inverse effect.
        pub const INVERSE_OFF: [u8; 5] = "\x1b[27m", *b"\x1b[27m";

        /// Code to set crossed effect.
        pub const CROSSED: [u8; 4] = "\x1b[9m", *b"\x1b[9m";
        /// Code to unset crossed effect.
        pub const CROSSED_OFF: [u8; 5] = "\x1b[29m", *b"\x1b[29m";
    }
}

// /// # Not very well supported font effects escape codes
// impl Ansi {
//     _ansi_consts! {
//         pub const UNDERLINE_DOUBLE: [u8; 5] = "\x1b[21m", *b"\x1b[21m"; // or bold_off
//         pub const BLINK_FAST: &[u8] = "\x1b[6m", *b"\x1b[6m";
//     }
// }

#[cfg(test)]
mod tests {
    use super::Ansi;

    #[test]
    fn write_ansi_code_n() {
        let mut buffer = [0u8; 16];
        let result = Ansi::write_ansi_code_n(&mut buffer, 0, b'J');
        assert_eq!(result, b"\x1b[0J");
        let result = Ansi::write_ansi_code_n(&mut buffer, 5, b'm');
        assert_eq!(result, b"\x1b[5m");
        let result = Ansi::write_ansi_code_n(&mut buffer, 255, b'B');
        assert_eq!(result, b"\x1b[255B");
        let result = Ansi::write_ansi_code_n(&mut buffer, 15000, b'C');
        assert_eq!(result, b"\x1b[15000C");
    }

    #[test]
    fn cursor_move_n() {
        let mut buffer = [0u8; 32];
        let result = Ansi::CURSOR_MOVE_N_B(&mut buffer, 0, 0);
        assert_eq!(result, b"\x1b[0;0H");
        let result = Ansi::CURSOR_MOVE_N_B(&mut buffer, 1, 2);
        assert_eq!(result, b"\x1b[2;1H");
        let result = Ansi::CURSOR_MOVE_N_B(&mut buffer, 5, 10);
        assert_eq!(result, b"\x1b[10;5H");
        let result = Ansi::CURSOR_MOVE_N_B(&mut buffer, 123, 456);
        assert_eq!(result, b"\x1b[456;123H");
        let result = Ansi::CURSOR_MOVE_N_B(&mut buffer, 1999, 10999);
        assert_eq!(result, b"\x1b[10999;1999H");
        let result = Ansi::CURSOR_MOVE(30_000, 20_000); // StringNonul
        assert_eq!(result, "\x1b[20000;30000H");
    }
}