Skip to content

Commit

Permalink
Add entering insert mode key bindings: a, A and I (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
panarch authored Oct 28, 2024
1 parent 1bda12f commit fe34b22
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 16 deletions.
4 changes: 4 additions & 0 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ pub enum NotebookEvent {

#[derive(Clone, Copy, Debug, Display)]
pub enum KeyEvent {
A,
B,
E,
H,
I,
J,
K,
L,
M,
O,
W,
CapA,
CapI,
CapO,
DollarSign,
Num(NumKey),
Expand Down
6 changes: 0 additions & 6 deletions core/src/state/notebook/consume/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ pub async fn open(
Ok(NotebookTransition::OpenNote { note, content })
}

pub async fn edit(state: &mut NotebookState) -> Result<NotebookTransition> {
state.inner_state = InnerState::EditingInsertMode;

Ok(NotebookTransition::EditMode)
}

pub async fn view(state: &mut NotebookState) -> Result<NotebookTransition> {
let note = state.get_editing()?.clone();

Expand Down
29 changes: 28 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 @@ -35,7 +35,6 @@ async fn consume_idle(
Notebook(SelectNote(note)) => note::select(state, note),
Notebook(SelectDirectory(directory)) => directory::select(state, directory),
Notebook(UpdateNoteContent(content)) => note::update_content(db, state, content).await,
Notebook(EditNote) => note::edit(state).await,
Notebook(BrowseNoteTree) => note::browse(state).await,
Key(KeyEvent::J) => Ok(NotebookTransition::EditingNormalMode(
NormalModeTransition::MoveCursorDown(1),
Expand Down Expand Up @@ -64,6 +63,34 @@ async fn consume_idle(
Key(KeyEvent::DollarSign) => Ok(NotebookTransition::EditingNormalMode(
NormalModeTransition::MoveCursorLineEnd,
)),
Key(KeyEvent::I) => {
state.inner_state = InnerState::EditingInsertMode;

Ok(NotebookTransition::EditingNormalMode(
NormalModeTransition::InsertAtCursor,
))
}
Key(KeyEvent::CapI) => {
state.inner_state = InnerState::EditingInsertMode;

Ok(NotebookTransition::EditingNormalMode(
NormalModeTransition::InsertAtLineStart,
))
}
Key(KeyEvent::A) => {
state.inner_state = InnerState::EditingInsertMode;

Ok(NotebookTransition::EditingNormalMode(
NormalModeTransition::InsertAfterCursor,
))
}
Key(KeyEvent::CapA) => {
state.inner_state = InnerState::EditingInsertMode;

Ok(NotebookTransition::EditingNormalMode(
NormalModeTransition::InsertAtLineEnd,
))
}
Key(KeyEvent::O) => {
state.inner_state = InnerState::EditingInsertMode;

Expand Down
5 changes: 4 additions & 1 deletion core/src/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub enum NotebookTransition {
note: Note,
content: String,
},
EditMode,
ViewMode(Note),
SelectNote(Note),
SelectDirectory(Directory),
Expand Down Expand Up @@ -92,6 +91,10 @@ pub enum NormalModeTransition {
MoveCursorWordBack(usize),
MoveCursorLineStart,
MoveCursorLineEnd,
InsertAtCursor,
InsertAtLineStart,
InsertAfterCursor,
InsertAtLineEnd,
InsertNewLineBelow,
InsertNewLineAbove,
}
Expand Down
6 changes: 5 additions & 1 deletion tui/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,20 @@ fn to_event(input: Input) -> Option<KeyEvent> {
};

let event = match code {
KeyCode::Char('a') => KeyEvent::A,
KeyCode::Char('b') => KeyEvent::B,
KeyCode::Char('e') => KeyEvent::E,
KeyCode::Char('h') => KeyEvent::H,
KeyCode::Char('i') => KeyEvent::I,
KeyCode::Char('j') => KeyEvent::J,
KeyCode::Char('k') => KeyEvent::K,
KeyCode::Char('l') => KeyEvent::L,
KeyCode::Char('m') => KeyEvent::M,
KeyCode::Char('O') => KeyEvent::CapO,
KeyCode::Char('o') => KeyEvent::O,
KeyCode::Char('w') => KeyEvent::W,
KeyCode::Char('A') => KeyEvent::CapA,
KeyCode::Char('I') => KeyEvent::CapI,
KeyCode::Char('O') => KeyEvent::CapO,
KeyCode::Char('1') => NumKey::One.into(),
KeyCode::Char('2') => NumKey::Two.into(),
KeyCode::Char('3') => NumKey::Three.into(),
Expand Down
8 changes: 2 additions & 6 deletions tui/src/context/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ impl NotebookContext {

Action::Dispatch(NotebookEvent::BrowseNoteTree.into())
}
KeyCode::Char('i') => {
self.state = ContextState::EditorInsertMode;
Action::Dispatch(NotebookEvent::EditNote.into())
}
KeyCode::Char('t') => {
self.show_line_number = !self.show_line_number;

Expand All @@ -274,8 +270,8 @@ impl NotebookContext {
}
.into(),
KeyCode::Char(
'h' | 'j' | 'k' | 'l' | 'w' | 'e' | 'b' | 'o' | 'O' | '$' | '0' | '1' | '2' | '3'
| '4' | '5' | '6' | '7' | '8' | '9',
'a' | 'h' | 'i' | 'j' | 'k' | 'l' | 'w' | 'e' | 'b' | 'o' | 'A' | 'I' | 'O' | '$'
| '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9',
)
| KeyCode::Esc => Action::PassThrough,
_ => Action::None,
Expand Down
19 changes: 18 additions & 1 deletion tui/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl App {

self.glues.dispatch(event).await.log_unwrap();
}
NotebookTransition::EditMode => {}
NotebookTransition::RemoveNote {
selected_directory, ..
}
Expand Down Expand Up @@ -241,6 +240,24 @@ impl App {
self.context.notebook.editor.move_cursor(CursorMove::Up);
self.context.notebook.state = context::notebook::ContextState::EditorInsertMode;
}
NotebookTransition::EditingNormalMode(NormalModeTransition::InsertAtCursor) => {
self.context.notebook.state = context::notebook::ContextState::EditorInsertMode;
}
NotebookTransition::EditingNormalMode(NormalModeTransition::InsertAtLineStart) => {
self.context.notebook.editor.move_cursor(CursorMove::Head);
self.context.notebook.state = context::notebook::ContextState::EditorInsertMode;
}
NotebookTransition::EditingNormalMode(NormalModeTransition::InsertAfterCursor) => {
self.context
.notebook
.editor
.move_cursor(CursorMove::Forward);
self.context.notebook.state = context::notebook::ContextState::EditorInsertMode;
}
NotebookTransition::EditingNormalMode(NormalModeTransition::InsertAtLineEnd) => {
self.context.notebook.editor.move_cursor(CursorMove::End);
self.context.notebook.state = context::notebook::ContextState::EditorInsertMode;
}
NotebookTransition::Alert(message) => {
log!("[Alert] {message}");
self.context.alert = Some(message);
Expand Down

0 comments on commit fe34b22

Please sign in to comment.