Skip to content

Commit

Permalink
Add vim keymap dialogs - Ctrl+h to open (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
panarch authored Oct 29, 2024
1 parent 4cca45f commit 6af2285
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 39 deletions.
1 change: 1 addition & 0 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum KeyEvent {
CapI,
CapO,
CapS,
CtrlH,
CtrlR,
DollarSign,
Caret,
Expand Down
37 changes: 15 additions & 22 deletions core/src/state/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ impl NotebookState {
vec![
"[o] Open note".to_owned(),
"[h] Close parent".to_owned(),
"[j] Down".to_owned(),
"[k] Up".to_owned(),
"[j|k] Down | Up".to_owned(),
"[1-9] Set steps".to_owned(),
"[m] More actions".to_owned(),
"[Esc] Quit".to_owned(),
Expand All @@ -197,8 +196,7 @@ impl NotebookState {
vec![
"[l] Toggle".to_owned(),
"[h] Close parent".to_owned(),
"[j] Down".to_owned(),
"[k] Up".to_owned(),
"[j|k] Down | Up".to_owned(),
"[1-9] Set steps".to_owned(),
"[m] More actions".to_owned(),
"[Esc] Quit".to_owned(),
Expand All @@ -213,32 +211,25 @@ impl NotebookState {
]
}
EditingNormalMode(VimNormalState::Idle) => {
/* TODO:
[o] insert new line below
[O] insert new line above
[0] move to line start
[$] move to line end
/*
h j k l w e b [1-9] o O 0 $
a, A, I, G, g, s, S, x, ^, y, d, u, Ctrl+r
*/

vec![
"[i] Insert mode".to_owned(),
"[v] Visual mode".to_owned(),
"[n] Browse".to_owned(),
"[h|j|k|l] Move cursor".to_owned(),
"[w|e|b] Word forward|end|back".to_owned(),
"[1-9] Set steps".to_owned(),
"[t] Toggle line number".to_owned(),
"[Ctrl+h] Show Vim keymap".to_owned(),
"[Esc] Quit".to_owned(),
]
}
EditingNormalMode(VimNormalState::Numbering(n)) => {
// TODO: s, S, x, y, d

// h j k l [0-9] s S x y d w e b G
vec![
format!("[h|j|k|l] Move cursor {n} steps"),
format!("[w|e|b] Word forward|end|back {n} steps"),
format!("[G] Go to line {n}"),
"[0-9] Append steps".to_owned(),
"[Ctrl+h] Show Vim keymap".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
Expand Down Expand Up @@ -285,18 +276,20 @@ impl NotebookState {
]
}
EditingVisualMode(VimVisualState::Idle) => {
//todo
// more in the keymap
vec![
"[hjkl] Move".to_owned(),
"[h|j|k|l] Move cursor".to_owned(),
"[1-9] Append steps".to_owned(),
"[Ctrl+h] Show Vim keymap".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingVisualMode(VimVisualState::Numbering(n)) => {
//todo
// more in the keymap
vec![
format!("[h|j|k|l] Move cursor {n} steps"),
"[1-9] Append steps".to_owned(),
format!("[h|j|k|l] move cursor {n} steps"),
"[0-9] append steps".to_owned(),
"[Ctrl+h] Show Vim keymap".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/state/notebook/inner_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{db::Db, state::notebook::NotebookState, Event, NotebookTransition, R
pub use editing_normal_mode::VimNormalState;
pub use editing_visual_mode::VimVisualState;

#[derive(Clone)]
#[derive(Clone, Copy)]
pub enum InnerState {
NoteSelected,
NoteMoreActions,
Expand Down
8 changes: 7 additions & 1 deletion core/src/state/notebook/inner_state/editing_normal_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use {
crate::{
db::Db,
state::notebook::{directory, note, InnerState, NotebookState},
transition::{NormalModeTransition, NotebookTransition, VisualModeTransition},
transition::{
NormalModeTransition, NotebookTransition, VimKeymapKind, VisualModeTransition,
},
Error, Event, KeyEvent, NotebookEvent, NumKey, Result,
},
};
Expand Down Expand Up @@ -136,6 +138,7 @@ async fn consume_idle(

NumberingMode.into()
}
Key(KeyEvent::CtrlH) => Ok(NotebookTransition::ShowVimKeymap(VimKeymapKind::NormalIdle)),
event @ Key(_) => Ok(NotebookTransition::Inedible(event)),
_ => Err(Error::Wip("todo: Notebook::consume".to_owned())),
}
Expand Down Expand Up @@ -227,6 +230,9 @@ async fn consume_numbering(

IdleMode.into()
}
Key(KeyEvent::CtrlH) => Ok(NotebookTransition::ShowVimKeymap(
VimKeymapKind::NormalNumbering,
)),
event @ Key(_) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

Expand Down
17 changes: 5 additions & 12 deletions core/src/state/notebook/inner_state/editing_visual_mode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
db::Db,
state::notebook::{InnerState, NotebookState, VimNormalState},
transition::{NormalModeTransition, NotebookTransition, VisualModeTransition},
transition::{NormalModeTransition, NotebookTransition, VimKeymapKind, VisualModeTransition},
Error, Event, KeyEvent, NumKey, Result,
};

Expand Down Expand Up @@ -78,6 +78,7 @@ async fn consume_idle(

NumberingMode.into()
}
Key(KeyEvent::CtrlH) => Ok(NotebookTransition::ShowVimKeymap(VimKeymapKind::VisualIdle)),
event @ Key(_) => Ok(NotebookTransition::Inedible(event)),
_ => Err(Error::Wip("todo: Notebook::consume".to_owned())),
}
Expand Down Expand Up @@ -167,24 +168,16 @@ async fn consume_numbering(

MoveCursorToLine(n).into()
}
Key(KeyEvent::X | KeyEvent::D) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

DeleteSelection.into()
}

Key(KeyEvent::Y) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

YankSelection.into()
}
Key(KeyEvent::Esc) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

Ok(NotebookTransition::EditingNormalMode(
NormalModeTransition::IdleMode,
))
}
Key(KeyEvent::CtrlH) => Ok(NotebookTransition::ShowVimKeymap(
VimKeymapKind::VisualNumbering,
)),
event @ Key(_) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

Expand Down
9 changes: 9 additions & 0 deletions core/src/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ pub enum NotebookTransition {
SelectPrev(usize),
EditingNormalMode(NormalModeTransition),
EditingVisualMode(VisualModeTransition),
ShowVimKeymap(VimKeymapKind),
}

#[derive(Clone, Copy, Display)]
pub enum VimKeymapKind {
NormalIdle,
NormalNumbering,
VisualIdle,
VisualNumbering,
}

#[derive(Display)]
Expand Down
3 changes: 2 additions & 1 deletion tui/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ fn to_event(input: Input) -> Option<KeyEvent> {
let ctrl = key.modifiers == KeyModifiers::CONTROL;

let event = match code {
KeyCode::Char('h') if ctrl => KeyEvent::CtrlH,
KeyCode::Char('r') if ctrl => KeyEvent::CtrlR,
KeyCode::Char('a') => KeyEvent::A,
KeyCode::Char('b') => KeyEvent::B,
KeyCode::Char('d') => KeyEvent::D,
Expand All @@ -409,7 +411,6 @@ fn to_event(input: Input) -> Option<KeyEvent> {
KeyCode::Char('I') => KeyEvent::CapI,
KeyCode::Char('O') => KeyEvent::CapO,
KeyCode::Char('S') => KeyEvent::CapS,
KeyCode::Char('r') if ctrl => KeyEvent::CtrlR,
KeyCode::Char('1') => NumKey::One.into(),
KeyCode::Char('2') => NumKey::Two.into(),
KeyCode::Char('3') => NumKey::Three.into(),
Expand Down
8 changes: 7 additions & 1 deletion tui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod notebook;

use {
crate::{log, logger::*, Action},
glues_core::transition::VimKeymapKind,
ratatui::{
crossterm::event::{Event as Input, KeyCode, KeyEvent},
style::{Style, Stylize},
Expand Down Expand Up @@ -55,6 +56,7 @@ pub struct Context {

pub help: bool,
pub editor_keymap: bool,
pub vim_keymap: Option<VimKeymapKind>,
}

impl Default for Context {
Expand All @@ -71,6 +73,7 @@ impl Default for Context {

help: false,
editor_keymap: false,
vim_keymap: None,
}
}
}
Expand All @@ -86,7 +89,10 @@ impl Context {
}

pub async fn consume(&mut self, input: &Input) -> Action {
if self.editor_keymap {
if self.vim_keymap.is_some() {
self.vim_keymap = None;
return Action::None;
} else if self.editor_keymap {
self.editor_keymap = false;
return Action::None;
} else if self.help {
Expand Down
3 changes: 3 additions & 0 deletions tui/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ impl App {

pub(super) async fn handle_notebook_transition(&mut self, transition: NotebookTransition) {
match transition {
NotebookTransition::ShowVimKeymap(kind) => {
self.context.vim_keymap = Some(kind);
}
NotebookTransition::OpenDirectory { id, .. } => {
log!("Opening directory {id}");
let NotebookState { root, .. } = self.glues.state.get_inner().log_unwrap();
Expand Down
6 changes: 5 additions & 1 deletion tui/src/views/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod editor_keymap;
mod help;
mod note_actions;
mod prompt;
mod vim_keymap;

use {
crate::{
Expand All @@ -15,7 +16,10 @@ use {
};

pub fn draw(frame: &mut Frame, context: &mut Context) {
if context.editor_keymap {
if let Some(kind) = context.vim_keymap {
vim_keymap::draw(frame, kind);
return;
} else if context.editor_keymap {
editor_keymap::draw(frame);
return;
} else if context.help {
Expand Down
Loading

0 comments on commit 6af2285

Please sign in to comment.