2024-09-28 20:05:19 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2024-09-06 22:36:12 +01:00
|
|
|
use ratatui::{
|
|
|
|
style::{Style, Stylize},
|
|
|
|
text::{Line, Span},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Shortcut {
|
2024-09-14 16:30:03 +01:00
|
|
|
pub key_sequences: Vec<Span<'static>>,
|
2024-09-06 22:36:12 +01:00
|
|
|
pub desc: &'static str,
|
|
|
|
}
|
|
|
|
|
2024-09-19 19:18:55 +01:00
|
|
|
fn add_spacing(list: Vec<Vec<Span>>) -> Line {
|
|
|
|
list.into_iter()
|
|
|
|
.flat_map(|mut s| {
|
|
|
|
s.push(Span::default().content(" "));
|
|
|
|
s
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2024-09-06 22:36:12 +01:00
|
|
|
pub fn span_vec_len(span_vec: &[Span]) -> usize {
|
|
|
|
span_vec.iter().rfold(0, |init, s| init + s.width())
|
|
|
|
}
|
|
|
|
|
2024-09-28 20:05:19 +01:00
|
|
|
pub fn create_shortcut_list(
|
|
|
|
shortcuts: impl IntoIterator<Item = Shortcut>,
|
|
|
|
render_width: u16,
|
|
|
|
) -> Box<[Line<'static>]> {
|
2024-10-31 18:25:16 +00:00
|
|
|
let shortcut_spans: Vec<Vec<Span<'static>>> =
|
|
|
|
shortcuts.into_iter().map(|h| h.to_spans()).collect();
|
2024-09-06 22:36:12 +01:00
|
|
|
|
2024-10-31 18:25:16 +00:00
|
|
|
let max_shortcut_width = shortcut_spans
|
|
|
|
.iter()
|
|
|
|
.map(|s| span_vec_len(s))
|
|
|
|
.max()
|
|
|
|
.unwrap_or(0);
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-10-31 18:25:16 +00:00
|
|
|
let columns = (render_width as usize / (max_shortcut_width + 4)).max(1);
|
|
|
|
let rows = (shortcut_spans.len() + columns - 1) / columns;
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-10-31 18:25:16 +00:00
|
|
|
let mut lines: Vec<Line<'static>> = Vec::new();
|
2024-09-06 22:36:12 +01:00
|
|
|
|
2024-10-31 18:25:16 +00:00
|
|
|
for row in 0..rows {
|
|
|
|
let row_spans: Vec<_> = (0..columns)
|
|
|
|
.filter_map(|col| {
|
|
|
|
let index = row * columns + col;
|
|
|
|
shortcut_spans.get(index).map(|span| {
|
|
|
|
let padding = max_shortcut_width - span_vec_len(span);
|
|
|
|
let mut span_clone = span.clone();
|
|
|
|
span_clone.push(Span::raw(" ".repeat(padding)));
|
|
|
|
span_clone
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
lines.push(add_spacing(row_spans));
|
2024-09-06 22:36:12 +01:00
|
|
|
}
|
2024-09-28 20:05:19 +01:00
|
|
|
|
|
|
|
lines.into_boxed_slice()
|
2024-09-06 22:36:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Shortcut {
|
2024-09-28 20:05:19 +01:00
|
|
|
pub fn new<const N: usize>(desc: &'static str, key_sequences: [&'static str; N]) -> Self {
|
2024-09-06 22:36:12 +01:00
|
|
|
Self {
|
2024-09-14 16:30:03 +01:00
|
|
|
key_sequences: key_sequences
|
2024-09-06 22:36:12 +01:00
|
|
|
.iter()
|
2024-09-28 20:05:19 +01:00
|
|
|
.map(|s| Span::styled(Cow::<'static, str>::Borrowed(s), Style::default().bold()))
|
2024-09-06 22:36:12 +01:00
|
|
|
.collect(),
|
|
|
|
desc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-28 20:05:19 +01:00
|
|
|
fn to_spans(&self) -> Vec<Span<'static>> {
|
2024-09-06 22:36:12 +01:00
|
|
|
let mut ret: Vec<_> = self
|
2024-09-14 16:30:03 +01:00
|
|
|
.key_sequences
|
2024-09-06 22:36:12 +01:00
|
|
|
.iter()
|
|
|
|
.flat_map(|seq| {
|
|
|
|
[
|
|
|
|
Span::default().content("["),
|
|
|
|
seq.clone(),
|
|
|
|
Span::default().content("] "),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
ret.push(Span::styled(self.desc, Style::default().italic()));
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|