mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-11-21 21:09:42 +00:00
Merge e67e36b23a
into e3688e9b3d
This commit is contained in:
commit
1a8543f706
|
@ -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()
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user