use crate::ported::colorscheme::ATTR_UNDERLINE;
use std::collections::HashMap;
pub struct LemonbarRenderer;
impl LemonbarRenderer {
pub fn character_translations() -> HashMap<char, &'static str> {
let mut t: HashMap<char, &'static str> = HashMap::new();
t.insert('%', "%%{}");
t
}
pub fn hlstyle() -> &'static str {
""
}
pub fn hl(
contents: &str,
fg: Option<(i32, i64)>,
bg: Option<(i32, i64)>,
attrs: u32,
) -> String {
let mut text = String::new();
if let Some((_, hex)) = fg {
if hex >= 0 {
text.push_str(&format!("%{{F#ff{:06x}}}", hex));
}
}
if let Some((_, hex)) = bg {
if hex >= 0 {
text.push_str(&format!("%{{B#ff{:06x}}}", hex));
}
}
if attrs & ATTR_UNDERLINE != 0 {
text.push_str("%{+u}");
}
format!("{}{}%{{F-B--u}}", text, contents)
}
pub fn render(left_half: &str, right_half: &str) -> String {
format!("%{{l}}{}%{{r}}{}", left_half, right_half)
}
pub fn get_theme<'a>(
matcher_info: Option<&str>,
local_themes: &'a serde_json::Map<String, serde_json::Value>,
default_theme: &'a str,
) -> &'a str {
let mi = match matcher_info {
None | Some("") => return default_theme,
Some(s) => s,
};
if !local_themes.contains_key(mi) {
return default_theme;
}
default_theme
}
}
#[allow(non_camel_case_types)]
pub type renderer = LemonbarRenderer;
#[cfg(test)]
mod tests {
use super::*;
use crate::ported::colorscheme::ATTR_UNDERLINE;
#[test]
fn character_translations_escapes_percent() {
let t = LemonbarRenderer::character_translations();
assert_eq!(t.get(&'%'), Some(&"%%{}"));
}
#[test]
fn hl_plain_wraps_with_reset_marker() {
let out = LemonbarRenderer::hl("hi", None, None, 0);
assert_eq!(out, "hi%{F-B--u}");
}
#[test]
fn hl_with_fg_emits_f_marker() {
let out = LemonbarRenderer::hl("hi", Some((231, 0xffffff)), None, 0);
assert!(out.contains("%{F#ffffffff}"));
}
#[test]
fn hl_with_bg_emits_b_marker() {
let out = LemonbarRenderer::hl("hi", None, Some((21, 0x0000ff)), 0);
assert!(out.contains("%{B#ff0000ff}"));
}
#[test]
fn hl_with_underline_emits_u_marker() {
let out = LemonbarRenderer::hl("hi", None, None, ATTR_UNDERLINE);
assert!(out.contains("%{+u}"));
}
#[test]
fn hl_with_all_three_emits_all_markers_in_order() {
let out = LemonbarRenderer::hl(
"x",
Some((231, 0xffffff)),
Some((21, 0x0000ff)),
ATTR_UNDERLINE,
);
let f_pos = out.find("%{F#ff").unwrap();
let b_pos = out.find("%{B#ff").unwrap();
let u_pos = out.find("%{+u}").unwrap();
assert!(f_pos < b_pos);
assert!(b_pos < u_pos);
}
#[test]
fn render_wraps_left_and_right_with_position_markers() {
let out = LemonbarRenderer::render("LEFT", "RIGHT");
assert_eq!(out, "%{l}LEFT%{r}RIGHT");
}
#[test]
fn hlstyle_returns_empty() {
assert_eq!(LemonbarRenderer::hlstyle(), "");
}
}