Skip to content

Commit

Permalink
Organize the code related to the keybind functionality (#33)
Browse files Browse the repository at this point in the history
* Fix Config to private

* Remove unnecessary pub

* Remove unnecessary if

* Remove unnecessary mut

* Add test

* Stabilize the help display order

* Add test

* Fix key event string

* Organize the order of UserEvent
  • Loading branch information
lusingander authored Aug 10, 2024
1 parent db1db65 commit fd9240b
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 110 deletions.
25 changes: 13 additions & 12 deletions assets/default-keybind.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
force_quit = ["ctrl-c"]
quit = ["q"]
help_toggle = ["?"]
# close widget or cancel progress but not quit app
close_or_cancel = ["esc"]

navigate_up = ["k", "up"]
navigate_down = ["j", "down"]
navigate_right = ["l", "right"]
navigate_left = ["h", "left"]

# close widget or cancel progress but not quit app
close_or_cancel = ["esc"]
help_toggle = ["?"]
go_to_top = ["g"]
go_to_bottom = ["shift-G"]
go_to_next = ["n"]
go_to_previous = ["shift-N"]
go_to_bottom = ["shift-g"]
scroll_down = ["ctrl-e"]
scroll_up = ["ctrl-y"]
page_up = ["ctrl-b"]
page_down = ["ctrl-f"]
half_page_up = ["ctrl-u"]
half_page_down = ["ctrl-d"]
select_top = ["shift-H"]
select_middle = ["shift-M"]
select_bottom = ["shift-L"]
select_top = ["shift-h"]
select_middle = ["shift-m"]
select_bottom = ["shift-l"]

go_to_next = ["n"]
go_to_previous = ["shift-n"]
confirm = ["enter"]
ref_list_toggle = ["tab"]
search = ["/"]

# copy part of information, ex: copy the short commit hash not all
short_copy = ["c"]
full_copy = ["shift-C"]

ref_list_toggle = ["tab"]
full_copy = ["shift-c"]
18 changes: 9 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ratatui::{

use crate::{
color::ColorSet,
config::Config,
config::UiConfig,
event::{AppEvent, Receiver, Sender, UserEvent},
external::copy_to_clipboard,
git::Repository,
Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct App<'a> {
status_line: StatusLine,

keybind: &'a KeyBind,
config: &'a Config,
ui_config: &'a UiConfig,
image_protocol: ImageProtocol,
tx: Sender,
}
Expand All @@ -59,7 +59,7 @@ impl<'a> App<'a> {
graph: &'a Graph,
graph_image: &'a GraphImage,
keybind: &'a KeyBind,
config: &'a Config,
ui_config: &'a UiConfig,
color_set: &'a ColorSet,
image_protocol: ImageProtocol,
tx: Sender,
Expand Down Expand Up @@ -91,14 +91,14 @@ impl<'a> App<'a> {
head,
ref_name_to_commit_index_map,
);
let view = View::of_list(commit_list_state, config, tx.clone());
let view = View::of_list(commit_list_state, ui_config, tx.clone());

Self {
repository,
status_line: StatusLine::None,
view,
keybind,
config,
ui_config,
image_protocol,
tx,
}
Expand Down Expand Up @@ -257,7 +257,7 @@ impl App<'_> {
commit,
changes,
refs,
self.config,
self.ui_config,
self.image_protocol,
self.tx.clone(),
);
Expand All @@ -267,7 +267,7 @@ impl App<'_> {
fn close_detail(&mut self) {
if let View::Detail(ref mut view) = self.view {
let commit_list_state = view.take_list_state();
self.view = View::of_list(commit_list_state, self.config, self.tx.clone());
self.view = View::of_list(commit_list_state, self.ui_config, self.tx.clone());
}
}

Expand All @@ -281,14 +281,14 @@ impl App<'_> {
if let View::List(ref mut view) = self.view {
let commit_list_state = view.take_list_state();
let refs = self.repository.all_refs().into_iter().cloned().collect();
self.view = View::of_refs(commit_list_state, refs, self.config, self.tx.clone());
self.view = View::of_refs(commit_list_state, refs, self.ui_config, self.tx.clone());
}
}

fn close_refs(&mut self) {
if let View::Refs(ref mut view) = self.view {
let commit_list_state = view.take_list_state();
self.view = View::of_list(commit_list_state, self.config, self.tx.clone());
self.view = View::of_list(commit_list_state, self.ui_config, self.tx.clone());
}
}

Expand Down
33 changes: 16 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ const DEFAULT_LIST_NAME_WIDTH: u16 = 20;
const DEFAULT_DETAIL_DATE_FORMAT: &str = "%Y-%m-%d %H:%M:%S %z";
const DEFAULT_DETAIL_DATE_LOCAL: bool = true;

pub fn load() -> (UiConfig, Option<KeyBind>) {
let path = xdg::BaseDirectories::with_prefix(APP_DIR_NAME)
.unwrap()
.get_config_file(CONFIG_FILE_NAME);
let config = if path.exists() {
let content = std::fs::read_to_string(path).unwrap();
toml::from_str(&content).unwrap()
} else {
Config::default()
};
(config.ui, config.keybind)
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
pub struct Config {
struct Config {
#[serde(default)]
pub ui: UiConfig,
ui: UiConfig,
/// The user customed keybinds, please ref `assets/default-keybind.toml`
pub keybind: Option<KeyBind>,
keybind: Option<KeyBind>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
Expand Down Expand Up @@ -100,20 +113,6 @@ fn ui_detail_date_local_default() -> bool {
DEFAULT_DETAIL_DATE_LOCAL
}

impl Config {
pub fn load() -> Config {
let path = xdg::BaseDirectories::with_prefix(APP_DIR_NAME)
.unwrap()
.get_config_file(CONFIG_FILE_NAME);
if path.exists() {
let content = std::fs::read_to_string(path).unwrap();
toml::from_str(&content).unwrap()
} else {
Config::default()
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
28 changes: 14 additions & 14 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ pub fn init() -> (Sender, Receiver) {
#[serde(rename_all = "snake_case")]
pub enum UserEvent {
// NOTE User Event should have document, else the enum item will be hidden in the help page
/// Force Quit app without passing input into widges or views
ForceQuit,
/// Quit app
Quit,
/// Toggle Help page
HelpToggle,
/// Close widget or cancel current progress
CloseOrCancel,
/// Navigate up
NavigateUp,
/// Navigate down
Expand All @@ -95,22 +103,10 @@ pub enum UserEvent {
NavigateRight,
/// Navigate left
NavigateLeft,
/// Force Quit serie without passing input into widges or views
ForceQuit,
/// Quit serie
Quit,
/// Close widget or cancel current progress
CloseOrCancel,
/// Toggle Help page
HelpToggle,
/// Go to top
GoToTop,
/// Go to bottom
GoToBottom,
/// Go to next item
GoToNext,
/// Go to previous item
GoToPrevious,
/// Scroll one line up
ScrollUp,
/// Scroll one line down
Expand All @@ -129,15 +125,19 @@ pub enum UserEvent {
SelectMiddle,
/// Select bottom part
SelectBottom,
/// Go to next item
GoToNext,
/// Go to previous item
GoToPrevious,
/// Confirm
Confirm,
/// Toggle for Reference List
RefListToggle,
/// Search
Search,
/// Copy part of content
ShortCopy,
/// Copy
FullCopy,
/// Toggle for Reference List
RefListToggle,
Unknown,
}
Loading

0 comments on commit fd9240b

Please sign in to comment.