|
1 | | -use owo_colors::{colors, Color}; |
2 | | - |
3 | | -pub const COLOR_NULL: &[u8] = colors::BrightBlack::ANSI_FG.as_bytes(); |
4 | | -pub const COLOR_OFFSET: &[u8] = colors::BrightBlack::ANSI_FG.as_bytes(); |
5 | | -pub const COLOR_ASCII_PRINTABLE: &[u8] = colors::Cyan::ANSI_FG.as_bytes(); |
6 | | -pub const COLOR_ASCII_WHITESPACE: &[u8] = colors::Green::ANSI_FG.as_bytes(); |
7 | | -pub const COLOR_ASCII_OTHER: &[u8] = colors::Green::ANSI_FG.as_bytes(); |
8 | | -pub const COLOR_NONASCII: &[u8] = colors::Yellow::ANSI_FG.as_bytes(); |
9 | | -pub const COLOR_RESET: &[u8] = colors::Default::ANSI_FG.as_bytes(); |
| 1 | +use owo_colors::{colors, AnsiColors, Color, DynColors, OwoColorize}; |
| 2 | +use std::str::FromStr; |
| 3 | +use std::sync::LazyLock; |
| 4 | + |
| 5 | +pub static COLOR_NULL: LazyLock<String> = |
| 6 | + LazyLock::new(|| init_color("NULL", AnsiColors::BrightBlack)); |
| 7 | +pub static COLOR_OFFSET: LazyLock<String> = |
| 8 | + LazyLock::new(|| init_color("OFFSET", AnsiColors::BrightBlack)); |
| 9 | +pub static COLOR_ASCII_PRINTABLE: LazyLock<String> = |
| 10 | + LazyLock::new(|| init_color("ASCII_PRINTABLE", AnsiColors::Cyan)); |
| 11 | +pub static COLOR_ASCII_WHITESPACE: LazyLock<String> = |
| 12 | + LazyLock::new(|| init_color("ASCII_WHITESPACE", AnsiColors::Green)); |
| 13 | +pub static COLOR_ASCII_OTHER: LazyLock<String> = |
| 14 | + LazyLock::new(|| init_color("ASCII_OTHER", AnsiColors::Green)); |
| 15 | +pub static COLOR_NONASCII: LazyLock<String> = |
| 16 | + LazyLock::new(|| init_color("NONASCII", AnsiColors::Yellow)); |
| 17 | +pub const COLOR_RESET: &str = colors::Default::ANSI_FG; |
| 18 | + |
| 19 | +fn init_color(name: &str, default_ansi: AnsiColors) -> String { |
| 20 | + let default = DynColors::Ansi(default_ansi); |
| 21 | + let env_var = format!("HEXYL_COLOR_{name}"); |
| 22 | + let color = match std::env::var(env_var).as_deref() { |
| 23 | + Ok(color) => match DynColors::from_str(color) { |
| 24 | + Ok(color) => color, |
| 25 | + _ => default, |
| 26 | + }, |
| 27 | + _ => default, |
| 28 | + }; |
| 29 | + // owo_colors' API isn't designed to get the terminal codes directly for |
| 30 | + // dynamic colors, so we use this hack to get them from the LHS of some text. |
| 31 | + format!("{}", "|".color(color)) |
| 32 | + .split_once("|") |
| 33 | + .unwrap() |
| 34 | + .0 |
| 35 | + .to_owned() |
| 36 | +} |
| 37 | + |
| 38 | +pub const COLOR_NULL_RGB: &[u8] = &rgb_bytes(100, 100, 100); |
| 39 | + |
| 40 | +pub const COLOR_DEL: &[u8] = &rgb_bytes(64, 128, 0); |
| 41 | + |
| 42 | +pub const COLOR_GRADIENT_NONASCII: [[u8; 19]; 128] = |
| 43 | + generate_color_gradient(&[(255, 0, 0, 0.0), (255, 255, 0, 0.66), (255, 255, 255, 1.0)]); |
| 44 | + |
| 45 | +pub const COLOR_GRADIENT_ASCII_NONPRINTABLE: [[u8; 19]; 31] = |
| 46 | + generate_color_gradient(&[(255, 0, 255, 0.0), (128, 0, 255, 1.0)]); |
| 47 | + |
| 48 | +pub const COLOR_GRADIENT_ASCII_PRINTABLE: [[u8; 19]; 95] = |
| 49 | + generate_color_gradient(&[(0, 128, 255, 0.0), (0, 255, 128, 1.0)]); |
| 50 | + |
| 51 | +const fn as_dec(byte: u8) -> [u8; 3] { |
| 52 | + [ |
| 53 | + b'0' + (byte / 100), |
| 54 | + b'0' + ((byte % 100) / 10), |
| 55 | + b'0' + (byte % 10), |
| 56 | + ] |
| 57 | +} |
| 58 | + |
| 59 | +const fn rgb_bytes(r: u8, g: u8, b: u8) -> [u8; 19] { |
| 60 | + let mut buf = *b"\x1b[38;2;rrr;ggg;bbbm"; |
| 61 | + |
| 62 | + // r 7 |
| 63 | + buf[7] = as_dec(r)[0]; |
| 64 | + buf[8] = as_dec(r)[1]; |
| 65 | + buf[9] = as_dec(r)[2]; |
| 66 | + |
| 67 | + // g 11 |
| 68 | + buf[11] = as_dec(g)[0]; |
| 69 | + buf[12] = as_dec(g)[1]; |
| 70 | + buf[13] = as_dec(g)[2]; |
| 71 | + |
| 72 | + // b 15 |
| 73 | + buf[15] = as_dec(b)[0]; |
| 74 | + buf[16] = as_dec(b)[1]; |
| 75 | + buf[17] = as_dec(b)[2]; |
| 76 | + |
| 77 | + buf |
| 78 | +} |
| 79 | + |
| 80 | +const fn generate_color_gradient<const N: usize>(stops: &[(u8, u8, u8, f64)]) -> [[u8; 19]; N] { |
| 81 | + let mut out = [rgb_bytes(0, 0, 0); N]; |
| 82 | + |
| 83 | + assert!(stops.len() >= 2, "need at least two stops for the gradient"); |
| 84 | + |
| 85 | + let mut byte = 0; |
| 86 | + while byte < N { |
| 87 | + let relative_byte = byte as f64 / N as f64; |
| 88 | + |
| 89 | + let mut i = 1; |
| 90 | + while i < stops.len() && stops[i].3 < relative_byte { |
| 91 | + i += 1; |
| 92 | + } |
| 93 | + if i >= stops.len() { |
| 94 | + i = stops.len() - 1; |
| 95 | + } |
| 96 | + let prev_stop = stops[i - 1]; |
| 97 | + let stop = stops[i]; |
| 98 | + let diff = stop.3 - prev_stop.3; |
| 99 | + let t = (relative_byte - prev_stop.3) / diff; |
| 100 | + |
| 101 | + let r = (prev_stop.0 as f64 + (t * (stop.0 as f64 - prev_stop.0 as f64))) as u8; |
| 102 | + let g = (prev_stop.1 as f64 + (t * (stop.1 as f64 - prev_stop.1 as f64))) as u8; |
| 103 | + let b = (prev_stop.2 as f64 + (t * (stop.2 as f64 - prev_stop.2 as f64))) as u8; |
| 104 | + |
| 105 | + out[byte] = rgb_bytes(r, g, b); |
| 106 | + |
| 107 | + byte += 1; |
| 108 | + } |
| 109 | + |
| 110 | + out |
| 111 | +} |
10 | 112 |
|
11 | 113 | #[rustfmt::skip] |
12 | 114 | pub const CP437: [char; 256] = [ |
|
0 commit comments