Skip to content

Commit

Permalink
merge lines in help
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Aug 26, 2024
1 parent ea994a3 commit 33f2783
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 5 deletions.
54 changes: 54 additions & 0 deletions rm-config/src/keymap/actions/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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<GeneralAction> for Action {
Expand Down
6 changes: 6 additions & 0 deletions rm-config/src/keymap/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ pub mod torrents_tab;

pub trait UserAction: Into<Action> {
fn desc(&self) -> &'static str;
fn merged_desc(&self, other: &Self) -> Option<&'static str> {
None
}
fn is_mergable_with(&self, other: &Self) -> bool {
false
}
}
71 changes: 69 additions & 2 deletions rm-config/src/keymap/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -36,6 +40,8 @@ pub struct KeymapConfig {
#[derive(Serialize, Deserialize, Clone)]
pub struct KeybindsHolder<T: Into<Action>> {
pub keybindings: Vec<Keybinding<T>>,
#[serde(skip)]
pub help_repr: Vec<(String, &'static str)>,
}

#[derive(Serialize, Clone)]
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -350,6 +357,66 @@ impl KeymapConfig {
}
}

fn populate_help_repr(&mut self) {
fn get_keybindings<T: Into<Action> + UserAction + Ord>(
keybindings: &[Keybinding<T>],
max_len: &mut usize,
) -> Vec<(String, &'static str)> {
let mut keys: BTreeMap<&T, Vec<String>> = 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<PathBuf> = OnceLock::new();
PATH.get_or_init(|| utils::get_config_path(Self::FILENAME))
Expand Down
40 changes: 37 additions & 3 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;
use std::{collections::BTreeMap, str::FromStr};

use ratatui::{
prelude::*,
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 33f2783

Please sign in to comment.