use ratatui::style::{Color, Modifier, Style};
use ratatui::text::Span;
use super::{ACCENT, MUTED, TEXT};
fn section_color(section: &str) -> Color {
match section {
"normal" => Color::Rgb(0x7a, 0xa2, 0xf7), "visual" => Color::Rgb(0xbb, 0x9a, 0xf7), "insert" => Color::Rgb(0x9e, 0xce, 0x6a), "leader" => ACCENT, "git" => Color::Rgb(0x7d, 0xcf, 0xff), "ex+panes" => Color::Rgb(0xe0, 0xaf, 0x68), _ => ACCENT,
}
}
pub(crate) fn row_spans(text: &str, section: &str, width: u16) -> Vec<Span<'static>> {
if text.starts_with("strop help") {
return vec![Span::styled(
text.to_string(),
Style::default().fg(ACCENT).add_modifier(Modifier::BOLD),
)];
}
if text.starts_with('[') && text.ends_with(']') {
let used = text.chars().count();
let pad = (width as usize).saturating_sub(used + 1);
return vec![
Span::styled(
text.to_string(),
Style::default()
.fg(section_color(section))
.add_modifier(Modifier::BOLD),
),
Span::styled(
format!(" {}", "─".repeat(pad)),
Style::default().fg(Color::Rgb(0x3a, 0x3d, 0x4d)),
),
];
}
let planned = text.ends_with("(soon)");
let fg = if planned { MUTED } else { TEXT };
let key_fg = if planned {
MUTED
} else {
section_color(section)
};
match text
.strip_prefix(" ")
.and_then(|t| t.find(" ").map(|i| i + 2))
{
Some(split) if split < text.len() => {
let (keys, desc) = text.split_at(split);
vec![
Span::styled(
keys.to_string(),
Style::default().fg(key_fg).add_modifier(Modifier::BOLD),
),
Span::styled(desc.to_string(), Style::default().fg(fg)),
]
}
_ => vec![Span::styled(text.to_string(), Style::default().fg(fg))],
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sections_and_keys_get_section_hues() {
let header = row_spans("[leader]", "leader", 40);
assert_eq!(header[0].style.fg, Some(ACCENT));
assert!(header[1].content.starts_with(' '), "rule trails");
let row = row_spans(" space f file finder", "leader", 40);
assert_eq!(row[0].content, " space f");
assert_eq!(row[0].style.fg, Some(ACCENT));
assert_eq!(row[1].content, " file finder");
assert_eq!(row[1].style.fg, Some(TEXT));
let normal = row_spans(" h j k l move", "normal", 40);
assert_eq!(normal[0].style.fg, Some(Color::Rgb(0x7a, 0xa2, 0xf7)));
let soon = row_spans(" space j jumplist picker (soon)", "leader", 40);
assert!(soon.iter().all(|s| s.style.fg == Some(MUTED)));
}
}