From 9045e56c8e342b3eb3069274e6f0211238f7ca24 Mon Sep 17 00:00:00 2001 From: mia Date: Sun, 7 Apr 2024 05:27:59 -0700 Subject: theme generation cleanups --- src/main.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index e353666..9cc7b85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,10 +43,17 @@ fn theme() { let mut scopes_gen = Vec::new(); let mut global_css = String::new(); + let credit = match (theme.name, theme.author) { + (None, None) => "".to_string(), + (None, Some(author)) => format!("/* theme by {author} */\n"), + (Some(name), None) => format!("/* {name} theme */\n"), + (Some(name), Some(author)) => format!("/* {name} theme by {author} */\n"), + }; + global_css.push_str(&credit); global_css.push_str(".highlight {\n"); if let Some(bg) = theme.settings.background { global_css.push_str(" background-color: #"); - global_css.extend(hex_color(bg)); + global_css.push_str(&hex_color(bg)); global_css.push_str(";\n"); } global_css.push_str("}\n"); @@ -62,13 +69,13 @@ fn theme() { if let Some(fg) = item.style.foreground { css.push_str(" color: #"); - css.extend(hex_color(fg)); + css.push_str(&hex_color(fg)); css.push_str(";\n"); } if let Some(bg) = item.style.background { css.push_str(" background-color: #"); - css.extend(hex_color(bg)); + css.push_str(&hex_color(bg)); css.push_str(";\n"); } @@ -108,18 +115,20 @@ fn selectors_to_string(selectors: ScopeSelectors) -> String { scopes.join(", ") } -fn hex_color(color: Color) -> [char; 8] { +fn hex_color(color: Color) -> String { const HEX_DIGIT_LOOKUP: &[u8; 16] = b"0123456789abcdef"; - [ - HEX_DIGIT_LOOKUP[(color.r >> 4) as usize] as char, - HEX_DIGIT_LOOKUP[(color.r & 0xf) as usize] as char, - HEX_DIGIT_LOOKUP[(color.g >> 4) as usize] as char, - HEX_DIGIT_LOOKUP[(color.g & 0xf) as usize] as char, - HEX_DIGIT_LOOKUP[(color.b >> 4) as usize] as char, - HEX_DIGIT_LOOKUP[(color.b & 0xf) as usize] as char, - HEX_DIGIT_LOOKUP[(color.a >> 4) as usize] as char, - HEX_DIGIT_LOOKUP[(color.a & 0xf) as usize] as char, - ] + let mut hex = String::with_capacity(8); + hex.push(HEX_DIGIT_LOOKUP[(color.r >> 4) as usize] as char); + hex.push(HEX_DIGIT_LOOKUP[(color.r & 0xf) as usize] as char); + hex.push(HEX_DIGIT_LOOKUP[(color.g >> 4) as usize] as char); + hex.push(HEX_DIGIT_LOOKUP[(color.g & 0xf) as usize] as char); + hex.push(HEX_DIGIT_LOOKUP[(color.b >> 4) as usize] as char); + hex.push(HEX_DIGIT_LOOKUP[(color.b & 0xf) as usize] as char); + if color.a != 0xff { + hex.push(HEX_DIGIT_LOOKUP[(color.a >> 4) as usize] as char); + hex.push(HEX_DIGIT_LOOKUP[(color.a & 0xf) as usize] as char); + } + hex } fn highlight() { -- cgit 1.4.1