organize shortcut list into a grid-like-pattern (#721)

* organize shortcut list into a grid-like-pattern

* increase minimum size

* add newline back to satisfy lint

* decrease to 25

* optimization

Co-authored-by: Adam Perkowski <adas1per@protonmail.com>

* satisfy lint

* implement some changes proposed by adam

Co-authored-by: Adam Perkowski <adas1per@protonmail.com>

---------

Co-authored-by: nyx <nnyyxxxx@users.noreply.github.com>
Co-authored-by: Adam Perkowski <adas1per@protonmail.com>
This commit is contained in:
nyx 2024-10-31 14:25:16 -04:00 committed by GitHub
parent b2fb33229b
commit a5fd25f6af
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
.iter()
.map(|s| span_vec_len(s))
.max()
.unwrap_or(0);
let mut lines: Vec<Line<'static>> = vec![]; let columns = (render_width as usize / (max_shortcut_width + 4)).max(1);
let rows = (shortcut_spans.len() + columns - 1) / columns;
loop { let mut lines: Vec<Line<'static>> = Vec::new();
let split_idx = shortcut_spans
.iter() for row in 0..rows {
.scan(0usize, |total_len, s| { let row_spans: Vec<_> = (0..columns)
// take at least one so that we guarantee that we drain the list .filter_map(|col| {
// otherwise, this might lock up if there's a shortcut that exceeds the window width let index = row * columns + col;
if *total_len == 0 { shortcut_spans.get(index).map(|span| {
*total_len += span_vec_len(s) + 4; let padding = max_shortcut_width - span_vec_len(span);
Some(()) let mut span_clone = span.clone();
} else { span_clone.push(Span::raw(" ".repeat(padding)));
*total_len += span_vec_len(s); span_clone
if *total_len > render_width as usize { })
None
} else {
*total_len += 4;
Some(())
}
}
}) })
.count(); .collect();
lines.push(add_spacing(row_spans));
let rest = shortcut_spans.split_off(split_idx);
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!("CARGO_PKG_VERSION")); const TITLE: &str = concat!("Linux Toolbox - ", env!("CARGO_PKG_VERSION"));
const ACTIONS_GUIDE: &str = "List of important tasks performed by commands' names: const ACTIONS_GUIDE: &str = "List of important tasks performed by commands' names: