use serde_json::{Map, Value};
pub struct I3barRenderer;
impl I3barRenderer {
pub fn hlstyle() -> &'static str {
""
}
pub fn hl(contents: &str, fg: Option<(i32, i64)>, bg: Option<(i32, i64)>) -> String {
let mut segment = Map::new();
segment.insert("full_text".to_string(), Value::String(contents.to_string()));
segment.insert("separator".to_string(), Value::Bool(false));
segment.insert("separator_block_width".to_string(), Value::from(0));
if let Some((_, hex)) = fg {
if hex >= 0 {
segment.insert("color".to_string(), Value::String(format!("#{:06x}", hex)));
}
}
if let Some((_, hex)) = bg {
if hex >= 0 {
segment.insert(
"background".to_string(),
Value::String(format!("#{:06x}", hex)),
);
}
}
format!("{},", Value::Object(segment))
}
}
#[allow(non_camel_case_types)]
pub type renderer = I3barRenderer;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hlstyle_returns_empty() {
assert_eq!(I3barRenderer::hlstyle(), "");
}
#[test]
fn hl_basic_contents_only() {
let out = I3barRenderer::hl("hello", None, None);
assert!(out.ends_with(','));
let json_part = &out[..out.len() - 1];
let v: Value = serde_json::from_str(json_part).unwrap();
assert_eq!(v["full_text"], "hello");
assert_eq!(v["separator"], false);
assert_eq!(v["separator_block_width"], 0);
assert!(v.get("color").is_none());
assert!(v.get("background").is_none());
}
#[test]
fn hl_with_fg_emits_color_field() {
let out = I3barRenderer::hl("x", Some((231, 0xffffff)), None);
let json_part = &out[..out.len() - 1];
let v: Value = serde_json::from_str(json_part).unwrap();
assert_eq!(v["color"], "#ffffff");
}
#[test]
fn hl_with_bg_emits_background_field() {
let out = I3barRenderer::hl("x", None, Some((21, 0x0000ff)));
let json_part = &out[..out.len() - 1];
let v: Value = serde_json::from_str(json_part).unwrap();
assert_eq!(v["background"], "#0000ff");
}
}