This commit is contained in:
nyx 2024-10-25 16:01:15 +02:00 committed by GitHub
commit 1a8543f706
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 33 deletions

View File

@ -27,41 +27,33 @@ pub fn create_shortcut_list(
shortcuts: impl IntoIterator<Item = Shortcut>, shortcuts: impl IntoIterator<Item = Shortcut>,
render_width: u16, render_width: u16,
) -> Box<[Line<'static>]> { ) -> Box<[Line<'static>]> {
let hints = shortcuts.into_iter().collect::<Box<[Shortcut]>>(); let shortcut_spans: Vec<Vec<Span<'static>>> =
shortcuts.into_iter().map(|h| h.to_spans()).collect();
let mut shortcut_spans: Vec<Vec<Span<'static>>> = hints.iter().map(|h| h.to_spans()).collect(); let max_shortcut_width = shortcut_spans
let mut lines: Vec<Line<'static>> = vec![];
loop {
let split_idx = shortcut_spans
.iter() .iter()
.scan(0usize, |total_len, s| { .map(|s| span_vec_len(s))
// take at least one so that we guarantee that we drain the list .max()
// otherwise, this might lock up if there's a shortcut that exceeds the window width .unwrap_or(0);
if *total_len == 0 {
*total_len += span_vec_len(s) + 4; let columns = (render_width as usize / (max_shortcut_width + 4)).max(1);
Some(()) let rows = (shortcut_spans.len() + columns - 1) / columns;
} else {
*total_len += span_vec_len(s); let mut lines: Vec<Line<'static>> = Vec::new();
if *total_len > render_width as usize {
None for row in 0..rows {
} else { let row_spans: Vec<_> = (0..columns)
*total_len += 4; .filter_map(|col| {
Some(()) 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(); })
.collect();
let rest = shortcut_spans.split_off(split_idx); lines.push(add_spacing(row_spans));
lines.push(add_spacing(shortcut_spans));
if rest.is_empty() {
break;
} else {
shortcut_spans = rest;
}
} }
lines.into_boxed_slice() lines.into_boxed_slice()

View File

@ -22,8 +22,8 @@ use ratatui::{
use std::rc::Rc; use std::rc::Rc;
use temp_dir::TempDir; use temp_dir::TempDir;
const MIN_WIDTH: u16 = 77; const MIN_WIDTH: u16 = 100;
const MIN_HEIGHT: u16 = 19; const MIN_HEIGHT: u16 = 25;
const TITLE: &str = concat!("Linux Toolbox - ", env!("BUILD_DATE")); const TITLE: &str = concat!("Linux Toolbox - ", env!("BUILD_DATE"));
const ACTIONS_GUIDE: &str = "List of important tasks performed by commands' names: const ACTIONS_GUIDE: &str = "List of important tasks performed by commands' names: