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>,
render_width: u16,
) -> 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 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<Line<'static>> = 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()

View File

@ -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!("CARGO_PKG_VERSION"));
const ACTIONS_GUIDE: &str = "List of important tasks performed by commands' names: