devela 0.28.0

A development substrate of coherence.
Documentation
// 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')
    }}
}