diff --git a/tui/src/hint.rs b/tui/src/hint.rs index 8e16e749..b5f096ca 100644 --- a/tui/src/hint.rs +++ b/tui/src/hint.rs @@ -27,41 +27,33 @@ pub fn create_shortcut_list( shortcuts: impl IntoIterator, render_width: u16, ) -> Box<[Line<'static>]> { - let hints = shortcuts.into_iter().collect::>(); + let shortcut_spans: Vec>> = + shortcuts.into_iter().map(|h| h.to_spans()).collect(); - let mut shortcut_spans: Vec>> = hints.iter().map(|h| h.to_spans()).collect(); + let max_shortcut_width = shortcut_spans + .iter() + .map(|s| span_vec_len(s)) + .max() + .unwrap_or(0); - let mut lines: Vec> = vec![]; + let columns = (render_width as usize / (max_shortcut_width + 4)).max(1); + let rows = (shortcut_spans.len() + columns - 1) / columns; - loop { - let split_idx = shortcut_spans - .iter() - .scan(0usize, |total_len, s| { - // take at least one so that we guarantee that we drain the list - // otherwise, this might lock up if there's a shortcut that exceeds the window width - if *total_len == 0 { - *total_len += span_vec_len(s) + 4; - Some(()) - } else { - *total_len += span_vec_len(s); - if *total_len > render_width as usize { - None - } else { - *total_len += 4; - Some(()) - } - } + let mut lines: Vec> = Vec::new(); + + 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 + }) }) - .count(); - - let rest = shortcut_spans.split_off(split_idx); - lines.push(add_spacing(shortcut_spans)); - - if rest.is_empty() { - break; - } else { - shortcut_spans = rest; - } + .collect(); + lines.push(add_spacing(row_spans)); } lines.into_boxed_slice() diff --git a/tui/src/state.rs b/tui/src/state.rs index 9ed61771..e6339d79 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -22,8 +22,8 @@ use ratatui::{ use std::rc::Rc; use temp_dir::TempDir; -const MIN_WIDTH: u16 = 77; -const MIN_HEIGHT: u16 = 19; +const MIN_WIDTH: u16 = 100; +const MIN_HEIGHT: u16 = 25; const TITLE: &str = concat!("Linux Toolbox - ", env!("BUILD_DATE")); const ACTIONS_GUIDE: &str = "List of important tasks performed by commands' names: