Skip to content

Commit

Permalink
restructure keymap config
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Jun 30, 2024
1 parent 047fc80 commit a9f1655
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 111 deletions.
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,
}
}
}
115 changes: 5 additions & 110 deletions rm-config/src/keymap.rs → rm-config/src/keymap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod actions;

use std::{
collections::HashMap, io::ErrorKind, marker::PhantomData, path::PathBuf, sync::OnceLock,
};
Expand All @@ -12,6 +14,8 @@ use serde::{
use crate::utils;
use rm_shared::action::Action;

use self::actions::{general::GeneralAction, torrents_tab::TorrentsAction};

#[derive(Serialize, Deserialize, Clone)]
pub struct KeymapConfig {
pub general: KeybindsHolder<GeneralAction>,
Expand All @@ -25,115 +29,6 @@ pub struct KeybindsHolder<T: Into<Action>> {
pub keybindings: Vec<Keybinding<T>>,
}

#[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,
}

pub trait UserAction: Into<Action> {
fn desc(&self) -> &'static str;
}

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,
}
}
}

#[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,
}
}
}

#[derive(Serialize, Clone)]
pub struct Keybinding<T: Into<Action>> {
pub on: KeyCode,
Expand Down Expand Up @@ -365,7 +260,7 @@ impl Default for KeyModifier {

impl KeymapConfig {
pub const FILENAME: &'static str = "keymap.toml";
const DEFAULT_CONFIG: &'static str = include_str!("../defaults/keymap.toml");
const DEFAULT_CONFIG: &'static str = include_str!("../../defaults/keymap.toml");

pub fn init() -> Result<Self> {
match utils::fetch_config::<Self>(Self::FILENAME) {
Expand Down
2 changes: 1 addition & 1 deletion rm-main/src/ui/global_popups/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
app,
ui::{centered_rect, components::Component},
};
use rm_config::keymap::{Keybinding, UserAction};
use rm_config::keymap::{actions::UserAction, Keybinding};
use rm_shared::action::Action;

macro_rules! add_line {
Expand Down

0 comments on commit a9f1655

Please sign in to comment.