From ea994a332937b0acb63d53bf52e7c8ec6349160a Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Mon, 26 Aug 2024 14:12:19 +0200 Subject: [PATCH 1/6] ui: improve help popup --- rm-main/src/tui/global_popups/help.rs | 79 ++++++++++++++++++--------- 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/rm-main/src/tui/global_popups/help.rs b/rm-main/src/tui/global_popups/help.rs index 5afb6e9..1e6edc6 100644 --- a/rm-main/src/tui/global_popups/help.rs +++ b/rm-main/src/tui/global_popups/help.rs @@ -52,12 +52,11 @@ impl HelpPopup { Self { ctx, scroll: None } } - fn write_keybindings + UserAction + Ord>( + fn get_keybindings + UserAction + Ord>( keybindings: &[Keybinding], - lines: &mut Vec, - ) { - let mut keys = BTreeMap::new(); - let mut max_len = 0; + max_len: &mut usize, + ) -> Vec<(String, &'static str)> { + let mut keys: BTreeMap<&T, Vec> = BTreeMap::new(); for keybinding in keybindings { if !keybinding.show_in_help { @@ -65,8 +64,8 @@ impl HelpPopup { } let keycode = keybinding.keycode_string(); - if keycode.len() > max_len { - max_len = keycode.chars().count(); + if keycode.len() > *max_len { + *max_len = keycode.chars().count(); } keys.entry(&keybinding.action) @@ -86,21 +85,19 @@ impl HelpPopup { keycodes_total_len += keycode.chars().count(); } - if keycodes_total_len + delimiter_len > max_len { - max_len = keycodes_total_len + delimiter_len; + if keycodes_total_len + delimiter_len > *max_len { + *max_len = keycodes_total_len + delimiter_len; } } + let mut res = vec![]; for (action, keycodes) in keys { - let mut keycode_string = keycodes.join(" / "); - let mut how_much_to_pad = max_len - keycode_string.chars().count(); - while how_much_to_pad > 0 { - keycode_string.insert(0, ' '); - how_much_to_pad -= 1; - } - - add_line!(lines, keycode_string, action.desc()); + let keycode_string = keycodes.join(" / "); + let desc = action.desc(); + res.push((keycode_string, desc)); } + + res } fn scroll_down(&mut self) -> ComponentAction { @@ -162,13 +159,45 @@ impl Component for HelpPopup { let block = popup_block_with_close_highlight(" Help "); - let mut lines = vec![Line::from(vec![Span::styled( - "Global Keybindings", - Style::default().bold().underlined(), - )]) - .centered()]; + let mut max_len = 0; + let mut global_keys = + Self::get_keybindings(&CONFIG.keybindings.general.keybindings, &mut max_len); + let mut torrents_keys = + Self::get_keybindings(&CONFIG.keybindings.torrents_tab.keybindings, &mut max_len); + let mut search_keys = + Self::get_keybindings(&CONFIG.keybindings.search_tab.keybindings, &mut max_len); + debug_assert!(max_len > 0); + + let pad_keys = |keys: &mut Vec<(String, &'static str)>| { + for key in keys { + let mut how_much_to_pad = max_len - key.0.chars().count(); + while how_much_to_pad > 0 { + key.0.insert(0, ' '); + how_much_to_pad -= 1; + } + } + }; + pad_keys(&mut global_keys); + pad_keys(&mut torrents_keys); + pad_keys(&mut search_keys); + + let mut lines = vec![]; + + let insert_keys = |lines: &mut Vec, keys: Vec<(String, &'static str)>| { + for (keycode, desc) in keys { + add_line!(lines, keycode, desc); + } + }; + + lines.push( + Line::from(vec![Span::styled( + "Global Keybindings", + Style::default().bold().underlined(), + )]) + .centered(), + ); - Self::write_keybindings(&CONFIG.keybindings.general.keybindings, &mut lines); + insert_keys(&mut lines, global_keys); lines.push( Line::from(vec![Span::styled( @@ -178,7 +207,7 @@ impl Component for HelpPopup { .centered(), ); - Self::write_keybindings(&CONFIG.keybindings.torrents_tab.keybindings, &mut lines); + insert_keys(&mut lines, torrents_keys); lines.push( Line::from(vec![Span::styled( @@ -188,7 +217,7 @@ impl Component for HelpPopup { .centered(), ); - Self::write_keybindings(&CONFIG.keybindings.search_tab.keybindings, &mut lines); + insert_keys(&mut lines, search_keys); let help_text = Text::from(lines); From 33f2783b0b51f60231f2dc71de33cf973a00fa12 Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Mon, 26 Aug 2024 18:21:10 +0200 Subject: [PATCH 2/6] merge lines in help --- rm-config/src/keymap/actions/general.rs | 54 +++++++++++++++++++ rm-config/src/keymap/actions/mod.rs | 6 +++ rm-config/src/keymap/mod.rs | 71 ++++++++++++++++++++++++- rm-main/src/tui/global_popups/help.rs | 40 ++++++++++++-- 4 files changed, 166 insertions(+), 5 deletions(-) diff --git a/rm-config/src/keymap/actions/general.rs b/rm-config/src/keymap/actions/general.rs index c99cd84..9f8b380 100644 --- a/rm-config/src/keymap/actions/general.rs +++ b/rm-config/src/keymap/actions/general.rs @@ -27,7 +27,34 @@ pub enum GeneralAction { MoveToColumnRight, } +pub enum GeneralActionMergable { + MoveUpDown, + MoveLeftRight, + ScrollPageUpDown, + MoveColumnLeftRight, + SwitchToTorrentsSearch, +} + impl UserAction for GeneralAction { + fn is_mergable_with(&self, other: &GeneralAction) -> bool { + let other = *other; + match self { + GeneralAction::SwitchToTorrents => other == Self::SwitchToSearch, + GeneralAction::SwitchToSearch => other == Self::SwitchToTorrents, + GeneralAction::Left => other == Self::Right, + GeneralAction::Right => other == Self::Left, + GeneralAction::Down => other == Self::Up, + GeneralAction::Up => other == Self::Down, + GeneralAction::ScrollPageDown => other == Self::ScrollPageUp, + GeneralAction::ScrollPageUp => other == Self::ScrollPageDown, + GeneralAction::GoToBeginning => other == Self::GoToEnd, + GeneralAction::GoToEnd => other == Self::GoToBeginning, + GeneralAction::MoveToColumnLeft => other == Self::MoveToColumnRight, + GeneralAction::MoveToColumnRight => other == Self::MoveToColumnLeft, + _ => false, + } + } + fn desc(&self) -> &'static str { match self { GeneralAction::ShowHelp => "toggle help", @@ -52,6 +79,33 @@ impl UserAction for GeneralAction { GeneralAction::MoveToColumnLeft => "move to left column", } } + + fn merged_desc(&self, other: &GeneralAction) -> Option<&'static str> { + match (&self, other) { + (Self::Left, Self::Right) => Some("switch to tab left / right"), + (Self::Right, Self::Left) => Some("switch to tab right / left"), + (Self::Down, Self::Up) => Some("move down / up"), + (Self::Up, Self::Down) => Some("move up / down"), + (Self::SwitchToTorrents, Self::SwitchToSearch) => { + Some("switch to torrents / search tab") + } + (Self::SwitchToSearch, Self::SwitchToTorrents) => { + Some("switch to search / torrents tab") + } + (Self::MoveToColumnLeft, Self::MoveToColumnRight) => { + Some("move to column left / right") + } + (Self::MoveToColumnRight, Self::MoveToColumnLeft) => { + Some("move to column right / left") + } + (Self::ScrollPageDown, Self::ScrollPageUp) => Some("scroll page down / up"), + (Self::ScrollPageUp, Self::ScrollPageDown) => Some("scroll page up / down"), + (Self::GoToBeginning, Self::GoToEnd) => Some("go to beginning / end"), + (Self::GoToEnd, Self::GoToBeginning) => Some("go to end / beginning"), + + _ => None, + } + } } impl From for Action { diff --git a/rm-config/src/keymap/actions/mod.rs b/rm-config/src/keymap/actions/mod.rs index 3436448..044f687 100644 --- a/rm-config/src/keymap/actions/mod.rs +++ b/rm-config/src/keymap/actions/mod.rs @@ -6,4 +6,10 @@ pub mod torrents_tab; pub trait UserAction: Into { fn desc(&self) -> &'static str; + fn merged_desc(&self, other: &Self) -> Option<&'static str> { + None + } + fn is_mergable_with(&self, other: &Self) -> bool { + false + } } diff --git a/rm-config/src/keymap/mod.rs b/rm-config/src/keymap/mod.rs index f7c3882..6e276a0 100644 --- a/rm-config/src/keymap/mod.rs +++ b/rm-config/src/keymap/mod.rs @@ -1,10 +1,14 @@ pub mod actions; use std::{ - collections::HashMap, io::ErrorKind, marker::PhantomData, path::PathBuf, sync::OnceLock, + collections::{BTreeMap, HashMap}, + io::ErrorKind, + marker::PhantomData, + path::PathBuf, + sync::OnceLock, }; -use actions::search_tab::SearchAction; +use actions::{search_tab::SearchAction, UserAction}; use anyhow::{Context, Result}; use crossterm::event::{KeyCode, KeyModifiers as CrosstermKeyModifiers}; use serde::{ @@ -36,6 +40,8 @@ pub struct KeymapConfig { #[derive(Serialize, Deserialize, Clone)] pub struct KeybindsHolder> { pub keybindings: Vec>, + #[serde(skip)] + pub help_repr: Vec<(String, &'static str)>, } #[derive(Serialize, Clone)] @@ -314,6 +320,7 @@ impl KeymapConfig { keys.push(keybinding.keycode_string()); } } + for keybinding in &self.torrents_tab.keybindings { if action == keybinding.action.into() { keys.push(keybinding.keycode_string()); @@ -350,6 +357,66 @@ impl KeymapConfig { } } + fn populate_help_repr(&mut self) { + fn get_keybindings + UserAction + Ord>( + keybindings: &[Keybinding], + max_len: &mut usize, + ) -> Vec<(String, &'static str)> { + let mut keys: BTreeMap<&T, Vec> = BTreeMap::new(); + + for keybinding in keybindings { + if !keybinding.show_in_help { + continue; + } + + let keycode = keybinding.keycode_string(); + if keycode.len() > *max_len { + *max_len = keycode.chars().count(); + } + + keys.entry(&keybinding.action) + .or_insert_with(Vec::new) + .push(keybinding.keycode_string()); + } + + for keycodes in keys.values() { + let mut keycodes_total_len = 0; + let delimiter_len = if keycodes.len() >= 2 { + (keycodes.len() - 1) * 3 + } else { + 0 + }; + + for keycode in keycodes { + keycodes_total_len += keycode.chars().count(); + } + + if keycodes_total_len + delimiter_len > *max_len { + *max_len = keycodes_total_len + delimiter_len; + } + } + + let mut res = vec![]; + for (action, keycodes) in keys { + let keycode_string = keycodes.join(" / "); + let desc = action.desc(); + res.push((keycode_string, desc)); + } + + res + } + + let mut max_len = 0; + // let global_keys_deduped = self + // .general + // .keybindings + // .clone() + // .dedup_by(|a, b| a.action.is_mergable_with(b.action)); + let mut global_keys = get_keybindings(&self.general.keybindings, &mut max_len); + let mut torrents_tab = get_keybindings(&self.torrents_tab.keybindings, &mut max_len); + let mut search_tab = get_keybindings(&self.search_tab.keybindings, &mut max_len); + } + pub fn path() -> &'static PathBuf { static PATH: OnceLock = OnceLock::new(); PATH.get_or_init(|| utils::get_config_path(Self::FILENAME)) diff --git a/rm-main/src/tui/global_popups/help.rs b/rm-main/src/tui/global_popups/help.rs index 1e6edc6..a335589 100644 --- a/rm-main/src/tui/global_popups/help.rs +++ b/rm-main/src/tui/global_popups/help.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeMap; +use std::{collections::BTreeMap, str::FromStr}; use ratatui::{ prelude::*, @@ -90,9 +90,43 @@ impl HelpPopup { } } - let mut res = vec![]; + let mut new_keys = vec![]; + for (action, keycodes) in keys { - let keycode_string = keycodes.join(" / "); + new_keys.push((action, keycodes)); + } + + let mut res = vec![]; + let mut skip_next_loop = false; + for (idx, (action, keycodes)) in new_keys.iter().enumerate() { + if skip_next_loop { + skip_next_loop = false; + continue; + } + + if idx < new_keys.len().saturating_sub(1) { + if action.is_mergable_with(new_keys[idx + 1].0) { + skip_next_loop = true; + let keys = format!( + "{} / {}", + keycodes.join(", "), + new_keys[idx + 1].1.join(", ") + ); + + if keys.chars().count() > *max_len { + *max_len = keys.chars().count(); + } + + let desc = action.merged_desc(new_keys[idx + 1].0).unwrap(); + res.push((keys, desc)); + continue; + } + } + + let keycode_string = keycodes.join(", "); + if keycode_string.chars().count() > *max_len { + *max_len = keycode_string.chars().count(); + } let desc = action.desc(); res.push((keycode_string, desc)); } From b0dc4ee1d1715b5f1c747c920cd80fbe95c491f0 Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Mon, 26 Aug 2024 20:31:32 +0200 Subject: [PATCH 3/6] polish it --- rm-config/src/keymap/actions/general.rs | 8 +-- rm-main/src/tui/global_popups/help.rs | 76 +++++++++++++++++-------- 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/rm-config/src/keymap/actions/general.rs b/rm-config/src/keymap/actions/general.rs index 9f8b380..5307047 100644 --- a/rm-config/src/keymap/actions/general.rs +++ b/rm-config/src/keymap/actions/general.rs @@ -58,8 +58,8 @@ impl UserAction for GeneralAction { fn desc(&self) -> &'static str { match self { GeneralAction::ShowHelp => "toggle help", - GeneralAction::Quit => "quit Rustmission / a popup", - GeneralAction::Close => "close a popup / task", + GeneralAction::Quit => "quit Rustmission, a popup", + GeneralAction::Close => "close a popup, a task", GeneralAction::SwitchToTorrents => "switch to torrents tab", GeneralAction::SwitchToSearch => "switch to search tab", GeneralAction::Left => "switch to tab left", @@ -72,8 +72,8 @@ impl UserAction for GeneralAction { GeneralAction::Select => "select", GeneralAction::ScrollPageDown => "scroll page down", GeneralAction::ScrollPageUp => "scroll page up", - GeneralAction::GoToBeginning => "scroll to the beginning", - GeneralAction::GoToEnd => "scroll to the end", + GeneralAction::GoToBeginning => "scroll to beginning", + GeneralAction::GoToEnd => "scroll to end", GeneralAction::XdgOpen => "open with xdg-open", GeneralAction::MoveToColumnRight => "move to right column", GeneralAction::MoveToColumnLeft => "move to left column", diff --git a/rm-main/src/tui/global_popups/help.rs b/rm-main/src/tui/global_popups/help.rs index a335589..e5bac35 100644 --- a/rm-main/src/tui/global_popups/help.rs +++ b/rm-main/src/tui/global_popups/help.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, str::FromStr}; +use std::collections::BTreeMap; use ratatui::{ prelude::*, @@ -26,6 +26,8 @@ macro_rules! add_line { }; } +const KEYS_DELIMITER: &str = ", "; + pub struct HelpPopup { ctx: app::Ctx, scroll: Option, @@ -54,7 +56,8 @@ impl HelpPopup { fn get_keybindings + UserAction + Ord>( keybindings: &[Keybinding], - max_len: &mut usize, + max_keycode_len: &mut usize, + max_line_len: &mut usize, ) -> Vec<(String, &'static str)> { let mut keys: BTreeMap<&T, Vec> = BTreeMap::new(); @@ -64,8 +67,8 @@ impl HelpPopup { } let keycode = keybinding.keycode_string(); - if keycode.len() > *max_len { - *max_len = keycode.chars().count(); + if keycode.len() > *max_keycode_len { + *max_keycode_len = keycode.chars().count(); } keys.entry(&keybinding.action) @@ -85,8 +88,8 @@ impl HelpPopup { keycodes_total_len += keycode.chars().count(); } - if keycodes_total_len + delimiter_len > *max_len { - *max_len = keycodes_total_len + delimiter_len; + if keycodes_total_len + delimiter_len > *max_keycode_len { + *max_keycode_len = keycodes_total_len + delimiter_len; } } @@ -104,30 +107,41 @@ impl HelpPopup { continue; } - if idx < new_keys.len().saturating_sub(1) { - if action.is_mergable_with(new_keys[idx + 1].0) { + if let Some(next_key) = new_keys.get(idx + 1) { + if action.is_mergable_with(next_key.0) { skip_next_loop = true; let keys = format!( "{} / {}", - keycodes.join(", "), - new_keys[idx + 1].1.join(", ") + keycodes.join(KEYS_DELIMITER), + next_key.1.join(KEYS_DELIMITER) ); - if keys.chars().count() > *max_len { - *max_len = keys.chars().count(); + if keys.chars().count() > *max_keycode_len { + *max_keycode_len = keys.chars().count(); + } + + let desc = action.merged_desc(next_key.0).unwrap(); + + let line_len = keys.chars().count() + desc.chars().count() + 3; + if line_len > *max_line_len { + *max_line_len = line_len; } - let desc = action.merged_desc(new_keys[idx + 1].0).unwrap(); res.push((keys, desc)); + continue; } } - let keycode_string = keycodes.join(", "); - if keycode_string.chars().count() > *max_len { - *max_len = keycode_string.chars().count(); + let keycode_string = keycodes.join(KEYS_DELIMITER); + if keycode_string.chars().count() > *max_keycode_len { + *max_keycode_len = keycode_string.chars().count(); } let desc = action.desc(); + let line_len = keycode_string.chars().count() + desc.chars().count() + 3; + if line_len > *max_line_len { + *max_line_len = line_len; + } res.push((keycode_string, desc)); } @@ -194,17 +208,33 @@ impl Component for HelpPopup { let block = popup_block_with_close_highlight(" Help "); let mut max_len = 0; - let mut global_keys = - Self::get_keybindings(&CONFIG.keybindings.general.keybindings, &mut max_len); - let mut torrents_keys = - Self::get_keybindings(&CONFIG.keybindings.torrents_tab.keybindings, &mut max_len); - let mut search_keys = - Self::get_keybindings(&CONFIG.keybindings.search_tab.keybindings, &mut max_len); + let mut max_line_len = 0; + let mut global_keys = Self::get_keybindings( + &CONFIG.keybindings.general.keybindings, + &mut max_len, + &mut max_line_len, + ); + let mut torrents_keys = Self::get_keybindings( + &CONFIG.keybindings.torrents_tab.keybindings, + &mut max_len, + &mut max_line_len, + ); + let mut search_keys = Self::get_keybindings( + &CONFIG.keybindings.search_tab.keybindings, + &mut max_len, + &mut max_line_len, + ); debug_assert!(max_len > 0); + let to_pad_additionally = (text_rect + .width + .saturating_sub(max_line_len.try_into().unwrap()) + / 2) + .saturating_sub(6); + max_len += usize::from(to_pad_additionally); let pad_keys = |keys: &mut Vec<(String, &'static str)>| { for key in keys { - let mut how_much_to_pad = max_len - key.0.chars().count(); + let mut how_much_to_pad = max_len.saturating_sub(key.0.chars().count()); while how_much_to_pad > 0 { key.0.insert(0, ' '); how_much_to_pad -= 1; From 413b0c6515d042b95f233a43f6cec383337f7a7d Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Mon, 26 Aug 2024 20:36:38 +0200 Subject: [PATCH 4/6] polish it even more --- rm-main/src/tui/global_popups/help.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rm-main/src/tui/global_popups/help.rs b/rm-main/src/tui/global_popups/help.rs index e5bac35..db7b1bc 100644 --- a/rm-main/src/tui/global_popups/help.rs +++ b/rm-main/src/tui/global_popups/help.rs @@ -207,34 +207,34 @@ impl Component for HelpPopup { let block = popup_block_with_close_highlight(" Help "); - let mut max_len = 0; + let mut to_pad = 0; let mut max_line_len = 0; let mut global_keys = Self::get_keybindings( &CONFIG.keybindings.general.keybindings, - &mut max_len, + &mut to_pad, &mut max_line_len, ); let mut torrents_keys = Self::get_keybindings( &CONFIG.keybindings.torrents_tab.keybindings, - &mut max_len, + &mut to_pad, &mut max_line_len, ); let mut search_keys = Self::get_keybindings( &CONFIG.keybindings.search_tab.keybindings, - &mut max_len, + &mut to_pad, &mut max_line_len, ); - debug_assert!(max_len > 0); + debug_assert!(to_pad > 0); let to_pad_additionally = (text_rect .width .saturating_sub(max_line_len.try_into().unwrap()) / 2) .saturating_sub(6); - max_len += usize::from(to_pad_additionally); + to_pad += usize::from(to_pad_additionally); let pad_keys = |keys: &mut Vec<(String, &'static str)>| { for key in keys { - let mut how_much_to_pad = max_len.saturating_sub(key.0.chars().count()); + let mut how_much_to_pad = to_pad.saturating_sub(key.0.chars().count()); while how_much_to_pad > 0 { key.0.insert(0, ' '); how_much_to_pad -= 1; @@ -251,6 +251,7 @@ impl Component for HelpPopup { for (keycode, desc) in keys { add_line!(lines, keycode, desc); } + lines.push(Line::default()); }; lines.push( From 7902daef7aab5275c39dd1cfa87db6c7aa9224d6 Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Mon, 26 Aug 2024 20:45:05 +0200 Subject: [PATCH 5/6] get rid of unused code --- rm-config/src/keymap/actions/mod.rs | 2 + rm-config/src/keymap/mod.rs | 68 +-------------------------- rm-main/src/tui/global_popups/help.rs | 5 ++ 3 files changed, 9 insertions(+), 66 deletions(-) diff --git a/rm-config/src/keymap/actions/mod.rs b/rm-config/src/keymap/actions/mod.rs index 044f687..f4e2160 100644 --- a/rm-config/src/keymap/actions/mod.rs +++ b/rm-config/src/keymap/actions/mod.rs @@ -7,9 +7,11 @@ pub mod torrents_tab; pub trait UserAction: Into { fn desc(&self) -> &'static str; fn merged_desc(&self, other: &Self) -> Option<&'static str> { + let _ = other; None } fn is_mergable_with(&self, other: &Self) -> bool { + let _ = other; false } } diff --git a/rm-config/src/keymap/mod.rs b/rm-config/src/keymap/mod.rs index 6e276a0..4114548 100644 --- a/rm-config/src/keymap/mod.rs +++ b/rm-config/src/keymap/mod.rs @@ -1,14 +1,10 @@ pub mod actions; use std::{ - collections::{BTreeMap, HashMap}, - io::ErrorKind, - marker::PhantomData, - path::PathBuf, - sync::OnceLock, + collections::HashMap, io::ErrorKind, marker::PhantomData, path::PathBuf, sync::OnceLock, }; -use actions::{search_tab::SearchAction, UserAction}; +use actions::search_tab::SearchAction; use anyhow::{Context, Result}; use crossterm::event::{KeyCode, KeyModifiers as CrosstermKeyModifiers}; use serde::{ @@ -357,66 +353,6 @@ impl KeymapConfig { } } - fn populate_help_repr(&mut self) { - fn get_keybindings + UserAction + Ord>( - keybindings: &[Keybinding], - max_len: &mut usize, - ) -> Vec<(String, &'static str)> { - let mut keys: BTreeMap<&T, Vec> = BTreeMap::new(); - - for keybinding in keybindings { - if !keybinding.show_in_help { - continue; - } - - let keycode = keybinding.keycode_string(); - if keycode.len() > *max_len { - *max_len = keycode.chars().count(); - } - - keys.entry(&keybinding.action) - .or_insert_with(Vec::new) - .push(keybinding.keycode_string()); - } - - for keycodes in keys.values() { - let mut keycodes_total_len = 0; - let delimiter_len = if keycodes.len() >= 2 { - (keycodes.len() - 1) * 3 - } else { - 0 - }; - - for keycode in keycodes { - keycodes_total_len += keycode.chars().count(); - } - - if keycodes_total_len + delimiter_len > *max_len { - *max_len = keycodes_total_len + delimiter_len; - } - } - - let mut res = vec![]; - for (action, keycodes) in keys { - let keycode_string = keycodes.join(" / "); - let desc = action.desc(); - res.push((keycode_string, desc)); - } - - res - } - - let mut max_len = 0; - // let global_keys_deduped = self - // .general - // .keybindings - // .clone() - // .dedup_by(|a, b| a.action.is_mergable_with(b.action)); - let mut global_keys = get_keybindings(&self.general.keybindings, &mut max_len); - let mut torrents_tab = get_keybindings(&self.torrents_tab.keybindings, &mut max_len); - let mut search_tab = get_keybindings(&self.search_tab.keybindings, &mut max_len); - } - pub fn path() -> &'static PathBuf { static PATH: OnceLock = OnceLock::new(); PATH.get_or_init(|| utils::get_config_path(Self::FILENAME)) diff --git a/rm-main/src/tui/global_popups/help.rs b/rm-main/src/tui/global_popups/help.rs index db7b1bc..ec6ccad 100644 --- a/rm-main/src/tui/global_popups/help.rs +++ b/rm-main/src/tui/global_popups/help.rs @@ -224,12 +224,16 @@ impl Component for HelpPopup { &mut to_pad, &mut max_line_len, ); + debug_assert!(to_pad > 0); + debug_assert!(max_line_len > 0); + let to_pad_additionally = (text_rect .width .saturating_sub(max_line_len.try_into().unwrap()) / 2) .saturating_sub(6); + to_pad += usize::from(to_pad_additionally); let pad_keys = |keys: &mut Vec<(String, &'static str)>| { @@ -241,6 +245,7 @@ impl Component for HelpPopup { } } }; + pad_keys(&mut global_keys); pad_keys(&mut torrents_keys); pad_keys(&mut search_keys); From 30a32960d116baa96e4bc92882845d5855445b5d Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Mon, 26 Aug 2024 20:45:50 +0200 Subject: [PATCH 6/6] document unwrap --- rm-main/src/tui/global_popups/help.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rm-main/src/tui/global_popups/help.rs b/rm-main/src/tui/global_popups/help.rs index ec6ccad..2a97f46 100644 --- a/rm-main/src/tui/global_popups/help.rs +++ b/rm-main/src/tui/global_popups/help.rs @@ -120,7 +120,9 @@ impl HelpPopup { *max_keycode_len = keys.chars().count(); } - let desc = action.merged_desc(next_key.0).unwrap(); + let desc = action + .merged_desc(next_key.0) + .expect("keys checked for mergability before"); let line_len = keys.chars().count() + desc.chars().count() + 3; if line_len > *max_line_len {