Skip to content

Commit 7f85768

Browse files
committed
costumize key config
- uniform key config - load key bindings from config - load key binding from key_config.ron - provide vim style sample config - update README - rm lazy_static - remove default key config - make keybinds with interieur mutability - remove mutablility for keybinds - fix vim style config
1 parent 91d7070 commit 7f85768

21 files changed

+677
-436
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ keywords = [
2121
[dependencies]
2222
scopetime = { path = "./scopetime", version = "0.1" }
2323
asyncgit = { path = "./asyncgit", version = "0.9" }
24-
crossterm = "0.17"
24+
crossterm = { version = "0.17", features = [ "serde" ] }
2525
clap = { version = "2.33", default-features = false }
2626
tui = { version = "0.9", default-features = false, features = ['crossterm'] }
2727
bytesize = { version = "1.0.1", default-features = false}

KEY_CONFIG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Key Config
2+
3+
Default using arrow key to navigate the gitui and Ctrl + C to leave
4+
Here is the [default config](assets/default_key_config.ron)
5+
6+
to change the key bindings of the program you have to modify `key_config.ron` file
7+
[Ron format](https://github.com/ron-rs/ron) located at config path. The path differs depending on the operating system:
8+
9+
* `$HOME/Library/Preferences/gitui/key_config.ron` (mac)
10+
* `$XDG_CONFIG_HOME/gitui/key_config.ron` (linux using XDG)
11+
* `$HOME/.config/gitui/key_config.ron` (linux)
12+
13+
There is also a vim style key config with `h`, `j`, `k`, `l` to navigate and `Ctrl + C` to leave
14+
Here is the [default config](assets/vim_style_key_config.ron)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,6 @@ However, you can customize everything to your liking: See [Themes](THEMES.md).
135135
- [tig](https://github.com/jonas/tig)
136136
- [GitUp](https://github.com/git-up/GitUp)
137137
- It would be nice to come up with a way to have the map view available in a terminal tool
138+
139+
# Key Bindings
140+
You can customize every keybing to your liking: See [Key Config](KEY_CONFIG.md).

assets/vim_style_key_config.ron

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// bit for modifiers
2+
// bits: 0 None
3+
// bits: 1 SHIFT
4+
// bits: 2 CONTROL
5+
(
6+
tab_1: ( code: Char('1'), modifiers: ( bits: 0,),),
7+
tab_2: ( code: Char('2'), modifiers: ( bits: 0,),),
8+
tab_3: ( code: Char('3'), modifiers: ( bits: 0,),),
9+
tab_4: ( code: Char('4'), modifiers: ( bits: 0,),),
10+
11+
tab_toggle: ( code: Tab, modifiers: ( bits: 0,),),
12+
tab_toggle_reverse: ( code: BackTab, modifiers: ( bits: 0,),),
13+
tab_toggle_reverse_windows: ( code: BackTab, modifiers: ( bits: 1,),),
14+
15+
focus_workdir: ( code: Char('w'), modifiers: ( bits: 0,),),
16+
focus_stage: ( code: Char('s'), modifiers: ( bits: 0,),),
17+
focus_right: ( code: Char('l'), modifiers: ( bits: 0,),),
18+
focus_left: ( code: Char('h'), modifiers: ( bits: 0,),),
19+
focus_above: ( code: Char('k'), modifiers: ( bits: 0,),),
20+
focus_below: ( code: Char('j'), modifiers: ( bits: 0,),),
21+
22+
exit: ( code: Char('c'), modifiers: ( bits: 2,),),
23+
exit_popup: ( code: Esc, modifiers: ( bits: 0,),),
24+
25+
close_msg: ( code: Enter, modifiers: ( bits: 0,),),
26+
open_commit: ( code: Char('c'), modifiers: ( bits: 0,),),
27+
open_commit_editor: ( code: Char('E'), modifiers: ( bits: 0,),),
28+
open_help: ( code: F(1), modifiers: ( bits: 0,),),
29+
30+
move_left: ( code: Char('h'), modifiers: ( bits: 0,),),
31+
move_right: ( code: Char('l'), modifiers: ( bits: 0,),),
32+
home: ( code: Home, modifiers: ( bits: 0,),),
33+
end: ( code: End, modifiers: ( bits: 0,),),
34+
move_up: ( code: Char('k'), modifiers: ( bits: 0,),),
35+
move_down: ( code: Char('j'), modifiers: ( bits: 0,),),
36+
page_up: ( code: Char('u'), modifiers: ( bits: 2,),),
37+
page_down: ( code: Char('d'), modifiers: ( bits: 2,),),
38+
39+
shift_up: ( code: Char('K'), modifiers: ( bits: 0,),),
40+
shift_down: ( code: Char('J'), modifiers: ( bits: 0,),),
41+
42+
enter: ( code: Enter, modifiers: ( bits: 0,),),
43+
44+
edit_file: ( code: Char('I'), modifiers: ( bits: 0,),),
45+
46+
status_stage_file: ( code: Enter, modifiers: ( bits: 0,),),
47+
status_stage_all: ( code: Char('a'), modifiers: ( bits: 0,),),
48+
status_reset_file: ( code: Char('U'), modifiers: ( bits: 0,),),
49+
50+
diff_reset_hunk: ( code: Enter, modifiers: ( bits: 0,),),
51+
status_ignore_file: ( code: Char('i'), modifiers: ( bits: 0,),),
52+
53+
stashing_save: ( code: Char('w'), modifiers: ( bits: 0,),),
54+
stashing_toggle_untracked: ( code: Char('u'), modifiers: ( bits: 0,),),
55+
stashing_toggle_index: ( code: Char('m'), modifiers: ( bits: 0,),),
56+
57+
stash_apply: ( code: Enter, modifiers: ( bits: 0,),),
58+
stash_open: ( code: Char('l'), modifiers: ( bits: 0,),),
59+
stash_drop: ( code: Char('D'), modifiers: ( bits: 0,),),
60+
61+
cmd_bar_toggle: ( code: Char('.'), modifiers: ( bits: 0,),),
62+
log_commit_details: ( code: Enter, modifiers: ( bits: 0,),),
63+
log_tag_commit: ( code: Char('t'), modifiers: ( bits: 0,),),
64+
commit_amend: ( code: Char('A'), modifiers: ( bits: 0,),),
65+
)

src/app.rs

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
ResetComponent, StashMsgComponent, TagCommitComponent,
99
},
1010
input::{Input, InputEvent, InputState},
11-
keys,
11+
keys::{KeyConfig, SharedKeyConfig},
1212
queue::{Action, InternalEvent, NeedsUpdate, Queue},
1313
strings::{self, commands, order},
1414
tabs::{Revlog, StashList, Stashing, Status},
@@ -49,6 +49,7 @@ pub struct App {
4949
stashlist_tab: StashList,
5050
queue: Queue,
5151
theme: SharedTheme,
52+
key_config: SharedKeyConfig,
5253
input: Input,
5354

5455
// "Flags"
@@ -66,13 +67,15 @@ impl App {
6667
let queue = Queue::default();
6768

6869
let theme = Rc::new(Theme::init());
70+
let key_config = Rc::new(KeyConfig::init());
6971

7072
Self {
7173
input,
7274
reset: ResetComponent::new(queue.clone(), theme.clone()),
7375
commit: CommitComponent::new(
7476
queue.clone(),
7577
theme.clone(),
78+
key_config.clone(),
7679
),
7780
stashmsg_popup: StashMsgComponent::new(
7881
queue.clone(),
@@ -82,6 +85,7 @@ impl App {
8285
&queue,
8386
sender,
8487
theme.clone(),
88+
key_config.clone(),
8589
),
8690
external_editor_popup: ExternalEditorComponent::new(
8791
theme.clone(),
@@ -92,19 +96,38 @@ impl App {
9296
),
9397
do_quit: false,
9498
cmdbar: RefCell::new(CommandBar::new(theme.clone())),
95-
help: HelpComponent::new(theme.clone()),
96-
msg: MsgComponent::new(theme.clone()),
99+
help: HelpComponent::new(
100+
theme.clone(),
101+
key_config.clone(),
102+
),
103+
msg: MsgComponent::new(theme.clone(), key_config.clone()),
97104
tab: 0,
98-
revlog: Revlog::new(&queue, sender, theme.clone()),
99-
status_tab: Status::new(&queue, sender, theme.clone()),
105+
revlog: Revlog::new(
106+
&queue,
107+
sender,
108+
theme.clone(),
109+
key_config.clone(),
110+
),
111+
status_tab: Status::new(
112+
&queue,
113+
sender,
114+
theme.clone(),
115+
key_config.clone(),
116+
),
100117
stashing_tab: Stashing::new(
101118
sender,
102119
&queue,
103120
theme.clone(),
121+
key_config.clone(),
122+
),
123+
stashlist_tab: StashList::new(
124+
&queue,
125+
theme.clone(),
126+
key_config.clone(),
104127
),
105-
stashlist_tab: StashList::new(&queue, theme.clone()),
106128
queue,
107129
theme,
130+
key_config,
108131
requires_redraw: Cell::new(false),
109132
file_to_open: None,
110133
}
@@ -160,30 +183,26 @@ impl App {
160183
if event_pump(ev, self.components_mut().as_mut_slice())? {
161184
flags.insert(NeedsUpdate::COMMANDS);
162185
} else if let Event::Key(k) = ev {
163-
let new_flags = match k {
164-
keys::TAB_TOGGLE => {
165-
self.toggle_tabs(false)?;
166-
NeedsUpdate::COMMANDS
167-
}
168-
keys::TAB_TOGGLE_REVERSE
169-
| keys::TAB_TOGGLE_REVERSE_WINDOWS => {
170-
self.toggle_tabs(true)?;
171-
NeedsUpdate::COMMANDS
172-
}
173-
keys::TAB_1
174-
| keys::TAB_2
175-
| keys::TAB_3
176-
| keys::TAB_4 => {
177-
self.switch_tab(k)?;
178-
NeedsUpdate::COMMANDS
179-
}
180-
181-
keys::CMD_BAR_TOGGLE => {
182-
self.cmdbar.borrow_mut().toggle_more();
183-
NeedsUpdate::empty()
184-
}
185-
186-
_ => NeedsUpdate::empty(),
186+
let new_flags = if k == self.key_config.tab_toggle {
187+
self.toggle_tabs(false)?;
188+
NeedsUpdate::COMMANDS
189+
} else if k == self.key_config.tab_toggle_reverse
190+
|| k == self.key_config.tab_toggle_reverse_windows
191+
{
192+
self.toggle_tabs(true)?;
193+
NeedsUpdate::COMMANDS
194+
} else if k == self.key_config.tab_1
195+
|| k == self.key_config.tab_2
196+
|| k == self.key_config.tab_3
197+
|| k == self.key_config.tab_4
198+
{
199+
self.switch_tab(k)?;
200+
NeedsUpdate::COMMANDS
201+
} else if k == self.key_config.cmd_bar_toggle {
202+
self.cmdbar.borrow_mut().toggle_more();
203+
NeedsUpdate::empty()
204+
} else {
205+
NeedsUpdate::empty()
187206
};
188207

189208
flags.insert(new_flags);
@@ -312,7 +331,7 @@ impl App {
312331

313332
fn check_quit_key(&mut self, ev: Event) -> bool {
314333
if let Event::Key(e) = ev {
315-
if let keys::EXIT = e {
334+
if e == self.key_config.exit {
316335
self.do_quit = true;
317336
return true;
318337
}
@@ -341,12 +360,14 @@ impl App {
341360
}
342361

343362
fn switch_tab(&mut self, k: KeyEvent) -> Result<()> {
344-
match k {
345-
keys::TAB_1 => self.set_tab(0)?,
346-
keys::TAB_2 => self.set_tab(1)?,
347-
keys::TAB_3 => self.set_tab(2)?,
348-
keys::TAB_4 => self.set_tab(3)?,
349-
_ => (),
363+
if k == self.key_config.tab_1 {
364+
self.set_tab(0)?
365+
} else if k == self.key_config.tab_2 {
366+
self.set_tab(1)?
367+
} else if k == self.key_config.tab_3 {
368+
self.set_tab(2)?
369+
} else if k == self.key_config.tab_4 {
370+
self.set_tab(3)?
350371
}
351372

352373
Ok(())

0 commit comments

Comments
 (0)