From 17ed589f4e6c8e86e68767cfba42f32e6d0e9106 Mon Sep 17 00:00:00 2001 From: nyx Date: Tue, 1 Oct 2024 18:39:35 -0400 Subject: [PATCH 1/7] organize shortcut list into a grid-like-pattern --- tui/src/hint.rs | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/tui/src/hint.rs b/tui/src/hint.rs index 8e16e749..f634ac73 100644 --- a/tui/src/hint.rs +++ b/tui/src/hint.rs @@ -28,40 +28,31 @@ pub fn create_shortcut_list( render_width: u16, ) -> Box<[Line<'static>]> { let hints = shortcuts.into_iter().collect::>(); + let shortcut_spans: Vec>> = hints.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(()) - } - } - }) - .count(); + let mut lines: Vec> = Vec::new(); - let rest = shortcut_spans.split_off(split_idx); - lines.push(add_spacing(shortcut_spans)); - - if rest.is_empty() { - break; - } else { - shortcut_spans = rest; + for row in 0..rows { + let mut row_spans = Vec::new(); + for col in 0..columns { + let index = row * columns + col; + if index < shortcut_spans.len() { + let mut span = shortcut_spans[index].clone(); + let padding = max_shortcut_width - span_vec_len(&span); + span.push(Span::raw(" ".repeat(padding))); + row_spans.push(span); + } } + lines.push(add_spacing(row_spans)); } lines.into_boxed_slice() From 572aefe7cfd2c07aa13e1ae391fca2589a2d28cb Mon Sep 17 00:00:00 2001 From: nyx Date: Tue, 1 Oct 2024 20:23:57 -0400 Subject: [PATCH 2/7] increase minimum size --- tui/src/state.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index ce724868..4f47ad15 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -22,8 +22,8 @@ use ratatui::{ Frame, }; -const MIN_WIDTH: u16 = 77; -const MIN_HEIGHT: u16 = 19; +const MIN_WIDTH: u16 = 100; +const MIN_HEIGHT: u16 = 30; const TITLE: &str = concat!("Linux Toolbox - ", env!("BUILD_DATE")); const ACTIONS_GUIDE: &str = "List of important tasks performed by commands' names: @@ -748,4 +748,4 @@ fn get_random_tip() -> &'static str { let mut rng = rand::thread_rng(); let random_index = rng.gen_range(0..tips.len()); tips[random_index] -} +} \ No newline at end of file From 952fa159bab4a43b6ed5ff1ffa46a1ee52942dfe Mon Sep 17 00:00:00 2001 From: nyx Date: Tue, 1 Oct 2024 20:27:12 -0400 Subject: [PATCH 3/7] add newline back to satisfy lint --- tui/src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index 4f47ad15..90f7dc0a 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -748,4 +748,4 @@ fn get_random_tip() -> &'static str { let mut rng = rand::thread_rng(); let random_index = rng.gen_range(0..tips.len()); tips[random_index] -} \ No newline at end of file +} From 23f5d453076ad5b988d93d0714e8fa0ce6645364 Mon Sep 17 00:00:00 2001 From: nyx Date: Tue, 1 Oct 2024 21:00:12 -0400 Subject: [PATCH 4/7] decrease to 25 --- tui/src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index 90f7dc0a..46fa3d04 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -23,7 +23,7 @@ use ratatui::{ }; const MIN_WIDTH: u16 = 100; -const MIN_HEIGHT: u16 = 30; +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: From fb527f62810cc97153ab7d46d7fc9055bccc66be Mon Sep 17 00:00:00 2001 From: Nyx <144965845+nnyyxxxx@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:29:08 -0400 Subject: [PATCH 5/7] optimization Co-authored-by: Adam Perkowski --- tui/src/hint.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tui/src/hint.rs b/tui/src/hint.rs index f634ac73..a51b204a 100644 --- a/tui/src/hint.rs +++ b/tui/src/hint.rs @@ -27,8 +27,10 @@ pub fn create_shortcut_list( shortcuts: impl IntoIterator, render_width: u16, ) -> Box<[Line<'static>]> { - let hints = shortcuts.into_iter().collect::>(); - let shortcut_spans: Vec>> = hints.iter().map(|h| h.to_spans()).collect(); + let shortcut_spans: Vec>> = shortcuts + .into_iter() + .map(|h| h.to_spans()) + .collect(); let max_shortcut_width = shortcut_spans .iter() From 06f3f0f26977366c728ffffdb39fe177dd883f2a Mon Sep 17 00:00:00 2001 From: nyx Date: Tue, 1 Oct 2024 21:31:18 -0400 Subject: [PATCH 6/7] satisfy lint --- tui/src/hint.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tui/src/hint.rs b/tui/src/hint.rs index f634ac73..331d3359 100644 --- a/tui/src/hint.rs +++ b/tui/src/hint.rs @@ -27,8 +27,8 @@ pub fn create_shortcut_list( shortcuts: impl IntoIterator, render_width: u16, ) -> Box<[Line<'static>]> { - let hints = shortcuts.into_iter().collect::>(); - let shortcut_spans: Vec>> = hints.iter().map(|h| h.to_spans()).collect(); + let shortcut_spans: Vec>> = + shortcuts.into_iter().map(|h| h.to_spans()).collect(); let max_shortcut_width = shortcut_spans .iter() From e67e36b23af38436ecc9b6200c3f8c9e9ce0636b Mon Sep 17 00:00:00 2001 From: nyx Date: Tue, 1 Oct 2024 22:46:00 -0400 Subject: [PATCH 7/7] implement some changes proposed by adam Co-authored-by: Adam Perkowski --- tui/src/hint.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tui/src/hint.rs b/tui/src/hint.rs index 331d3359..b5f096ca 100644 --- a/tui/src/hint.rs +++ b/tui/src/hint.rs @@ -42,16 +42,17 @@ pub fn create_shortcut_list( let mut lines: Vec> = Vec::new(); for row in 0..rows { - let mut row_spans = Vec::new(); - for col in 0..columns { - let index = row * columns + col; - if index < shortcut_spans.len() { - let mut span = shortcut_spans[index].clone(); - let padding = max_shortcut_width - span_vec_len(&span); - span.push(Span::raw(" ".repeat(padding))); - row_spans.push(span); - } - } + 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 + }) + }) + .collect(); lines.push(add_spacing(row_spans)); }