Skip to main content

tvui/
markup.rs

1//! The `~X~` hotkey markup of menu titles, items and buttons.
2
3use ratatui::style::Style;
4use ratatui::text::Span;
5
6/// The letter a `~X~` mark in `label` names.
7#[must_use]
8pub fn hotkey(label: &str) -> Option<char> {
9    let (_, rest) = label.split_once('~')?;
10    rest.split_once('~')?.0.chars().next()
11}
12
13/// `label` without its marks.
14#[must_use]
15pub fn plain(label: &str) -> String {
16    label.replace('~', "")
17}
18
19/// The spans of `label`: the marked letter in `hot`, the rest in `base`.
20pub(crate) fn spans(label: &str, base: Style, hot: Style) -> Vec<Span<'static>> {
21    let mut spans = Vec::new();
22    for (index, part) in label.split('~').enumerate() {
23        if !part.is_empty() {
24            let style = if index % 2 == 1 { hot } else { base };
25            spans.push(Span::styled(part.to_string(), style));
26        }
27    }
28    spans
29}
30
31#[cfg(test)]
32mod tests;