1
//! The `~X~` hotkey markup of menu titles, items and buttons.
2

            
3
use ratatui::style::Style;
4
use ratatui::text::Span;
5

            
6
/// The letter a `~X~` mark in `label` names.
7
#[must_use]
8
717
pub fn hotkey(label: &str) -> Option<char> {
9
717
    let (_, rest) = label.split_once('~')?;
10
686
    rest.split_once('~')?.0.chars().next()
11
717
}
12

            
13
/// `label` without its marks.
14
#[must_use]
15
55640
pub fn plain(label: &str) -> String {
16
55640
    label.replace('~', "")
17
55640
}
18

            
19
/// The spans of `label`: the marked letter in `hot`, the rest in `base`.
20
18104
pub(crate) fn spans(label: &str, base: Style, hot: Style) -> Vec<Span<'static>> {
21
18104
    let mut spans = Vec::new();
22
18104
    for (index, part) in label.split('~').enumerate() {
23
53388
        if !part.is_empty() {
24
36161
            let style = if index % 2 == 1 { hot } else { base };
25
36161
            spans.push(Span::styled(part.to_string(), style));
26
17227
        }
27
    }
28
18104
    spans
29
18104
}
30

            
31
#[cfg(test)]
32
mod tests;