Skip to content

Commit

Permalink
polish it
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Aug 26, 2024
1 parent 33f2783 commit b0dc4ee
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
8 changes: 4 additions & 4 deletions rm-config/src/keymap/actions/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
76 changes: 53 additions & 23 deletions rm-main/src/tui/global_popups/help.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, str::FromStr};
use std::collections::BTreeMap;

use ratatui::{
prelude::*,
Expand Down Expand Up @@ -26,6 +26,8 @@ macro_rules! add_line {
};
}

const KEYS_DELIMITER: &str = ", ";

pub struct HelpPopup {
ctx: app::Ctx,
scroll: Option<Scroll>,
Expand Down Expand Up @@ -54,7 +56,8 @@ impl HelpPopup {

fn get_keybindings<T: Into<Action> + UserAction + Ord>(
keybindings: &[Keybinding<T>],
max_len: &mut usize,
max_keycode_len: &mut usize,
max_line_len: &mut usize,
) -> Vec<(String, &'static str)> {
let mut keys: BTreeMap<&T, Vec<String>> = BTreeMap::new();

Expand All @@ -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)
Expand All @@ -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;
}
}

Expand All @@ -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));
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b0dc4ee

Please sign in to comment.