Skip to content

Commit

Permalink
configurable keybindings #23 (#36)
Browse files Browse the repository at this point in the history
* start implementing config

* impl deserialize for keybinding

* refactor(config): restructure

* fix headers_hide

* use indexmap

* get rid of crate's event

* fix help

* refactor writing to lines in help

* default config

* fix uppercase letters

* fix space

* make help key in the bottom reflect reality

* rename SoftQuit to Close

* rename keymodifiers to crosstermkeymodifiers

* merge 2 match branches

* restructure keymap config
  • Loading branch information
micielski committed Jul 4, 2024
1 parent 9673bcc commit 30aa7a1
Show file tree
Hide file tree
Showing 39 changed files with 856 additions and 363 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ license = "GPL-3.0-or-later"

[workspace.dependencies]
rm-config = { version = "0.3", path = "rm-config" }
rm-shared = { version = "0.3", path = "rm-shared" }

magnetease = "0.1"
anyhow = "1"
Expand All @@ -24,16 +25,17 @@ fuzzy-matcher = "0.3.7"
clap = { version = "4.5.6", features = ["derive"] }
base64 = "0.22"
xdg = "2.5"
url = "2.5"
url = { version = "2.5", features = ["serde"] }
toml = "0.8"
thiserror = "1"

# Async
tokio = { version = "1", features = ["macros", "sync"] }
tokio-util = "0.7"
futures = "0.3"

# TUI
crossterm = { version = "0.27", features = ["event-stream"] }
crossterm = { version = "0.27", features = ["event-stream", "serde"] }
ratatui = { version = "0.26", features = ["serde"] }
tui-input = "0.8"
tui-tree-widget = "0.20"
Expand Down
5 changes: 3 additions & 2 deletions rm-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ homepage.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rm-shared.workspace = true
xdg.workspace = true
toml.workspace = true
serde.workspace = true
anyhow.workspace = true
url.workspace = true
ratatui.workspace = true


crossterm.workspace = true
thiserror.workspace = true
46 changes: 46 additions & 0 deletions rm-config/defaults/keymap.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[general]
keybindings = [
{ on = "?", action = "ShowHelp" },
{ on = "F1", action = "ShowHelp" },

{ on = "q", action = "Quit" },
{ on = "Esc", action = "Close" },
{ on = "Enter", action = "Confirm" },
{ on = " ", action = "Select" },
{ on = "Tab", action = "SwitchFocus" },
{ on = "/", action = "Search" },

{ on = "1", action = "SwitchToTorrents" },
{ on = "2", action = "SwitchToSearch" },

{ on = "Home", action = "GoToBeginning" },
{ on = "End", action = "GoToEnd" },
{ on = "PageUp", action = "ScrollPageUp" },
{ on = "PageDown", action = "ScrollPageDown" },

{ modifier = "Ctrl", on = "u", action = "ScrollPageUp" },
{ modifier = "Ctrl", on = "d", action = "ScrollPageDown" },

# Arrows
{ on = "Left", action = "Left" },
{ on = "Right", action = "Right" },
{ on = "Up", action = "Up"},
{ on = "Down", action = "Down" },

# Vi
{ on = "h", action = "Left" },
{ on = "l", action = "Right" },
{ on = "k", action = "Up" },
{ on = "j", action = "Down" },
]

[torrents_tab]
keybindings = [
{ on = "a", action = "AddMagnet" },
{ on = "p", action = "Pause" },
{ on = "f", action = "ShowFiles" },
{ on = "s", action = "ShowStats" },

{ on = "d", action = "DeleteWithoutFiles" },
{ on = "D", action = "DeleteWithFiles" },
]
73 changes: 73 additions & 0 deletions rm-config/src/keymap/actions/general.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use rm_shared::action::Action;
use serde::{Deserialize, Serialize};

use super::UserAction;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum GeneralAction {
ShowHelp,
Quit,
Close,
SwitchToTorrents,
SwitchToSearch,
Left,
Right,
Down,
Up,
Search,
SwitchFocus,
Confirm,
Select,
ScrollPageDown,
ScrollPageUp,
GoToBeginning,
GoToEnd,
}

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::SwitchToTorrents => "switch to torrents tab",
GeneralAction::SwitchToSearch => "switch to search tab",
GeneralAction::Left => "switch to tab left",
GeneralAction::Right => "switch to tab right",
GeneralAction::Down => "move down",
GeneralAction::Up => "move up",
GeneralAction::Search => "search",
GeneralAction::SwitchFocus => "switch focus",
GeneralAction::Confirm => "confirm",
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",
}
}
}

impl From<GeneralAction> for Action {
fn from(value: GeneralAction) -> Self {
match value {
GeneralAction::ShowHelp => Action::ShowHelp,
GeneralAction::Quit => Action::Quit,
GeneralAction::Close => Action::Close,
GeneralAction::SwitchToTorrents => Action::ChangeTab(1),
GeneralAction::SwitchToSearch => Action::ChangeTab(2),
GeneralAction::Left => Action::Left,
GeneralAction::Right => Action::Right,
GeneralAction::Down => Action::Down,
GeneralAction::Up => Action::Up,
GeneralAction::Search => Action::Search,
GeneralAction::SwitchFocus => Action::ChangeFocus,
GeneralAction::Confirm => Action::Confirm,
GeneralAction::Select => Action::Select,
GeneralAction::ScrollPageDown => Action::ScrollDownPage,
GeneralAction::ScrollPageUp => Action::ScrollUpPage,
GeneralAction::GoToBeginning => Action::Home,
GeneralAction::GoToEnd => Action::End,
}
}
}
8 changes: 8 additions & 0 deletions rm-config/src/keymap/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use rm_shared::action::Action;

pub mod general;
pub mod torrents_tab;

pub trait UserAction: Into<Action> {
fn desc(&self) -> &'static str;
}
40 changes: 40 additions & 0 deletions rm-config/src/keymap/actions/torrents_tab.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use rm_shared::action::Action;
use serde::{Deserialize, Serialize};

use super::UserAction;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TorrentsAction {
AddMagnet,
Pause,
DeleteWithFiles,
DeleteWithoutFiles,
ShowFiles,
ShowStats,
}

impl UserAction for TorrentsAction {
fn desc(&self) -> &'static str {
match self {
TorrentsAction::AddMagnet => "add a magnet",
TorrentsAction::Pause => "pause/unpause",
TorrentsAction::DeleteWithFiles => "delete with files",
TorrentsAction::DeleteWithoutFiles => "delete without files",
TorrentsAction::ShowFiles => "show files",
TorrentsAction::ShowStats => "show statistics",
}
}
}

impl From<TorrentsAction> for Action {
fn from(value: TorrentsAction) -> Self {
match value {
TorrentsAction::AddMagnet => Action::AddMagnet,
TorrentsAction::Pause => Action::Pause,
TorrentsAction::DeleteWithFiles => Action::DeleteWithFiles,
TorrentsAction::DeleteWithoutFiles => Action::DeleteWithoutFiles,
TorrentsAction::ShowFiles => Action::ShowFiles,
TorrentsAction::ShowStats => Action::ShowStats,
}
}
}
Loading

0 comments on commit 30aa7a1

Please sign in to comment.