// yt - A fully featured command line YouTube client // // Copyright (C) 2025 Benedikt Peetz // SPDX-License-Identifier: GPL-3.0-or-later // // This file is part of Yt. // // You should have received a copy of the License along with this program. // If not, see . // Taken from // at 2025-07-16T18:05:55 CEST. const U8_TO_STR: [[u8; 3]; 256] = generate_lookup(); const fn generate_lookup() -> [[u8; 3]; 256] { let mut table = [[0, 0, 0]; 256]; let mut i = 0; while i < 256 { table[i] = [ b'0' + (i / 100) as u8, b'0' + (i / 10 % 10) as u8, b'0' + (i % 10) as u8, ]; i += 1; } table } #[derive(Clone, Copy)] pub(crate) enum Plane { Fg, Bg, } pub(crate) const fn rgb_to_ansi(r: u8, g: u8, b: u8, plane: Plane) -> [u8; 18] { let mut buf = *b"\x1b[p8;2;rrr;ggg;bbb"; let r = U8_TO_STR[r as usize]; let g = U8_TO_STR[g as usize]; let b = U8_TO_STR[b as usize]; // p 2 buf[2] = match plane { Plane::Fg => b'3', Plane::Bg => b'4', }; // r 7 buf[7] = r[0]; buf[8] = r[1]; buf[9] = r[2]; // g 11 buf[11] = g[0]; buf[12] = g[1]; buf[13] = g[2]; // b 15 buf[15] = b[0]; buf[16] = b[1]; buf[17] = b[2]; buf } /// This exists since [`unwrap()`] isn't const-safe (it invokes formatting infrastructure) pub(crate) const fn bytes_to_str(bytes: &'static [u8]) -> &'static str { match core::str::from_utf8(bytes) { Ok(o) => o, Err(_e) => panic!("Const parsing &[u8] to a string failed!"), } }