Skip to content

Commit 0d51376

Browse files
authored
Add select-first (gg) key support to NoteTree (#126)
1 parent abd1ed3 commit 0d51376

File tree

10 files changed

+91
-3
lines changed

10 files changed

+91
-3
lines changed

core/src/state/notebook.rs

+5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ impl NotebookState {
101101
NoteTree(NoteTreeState::Numbering(n)) => {
102102
format!("Steps: '{n}' selected")
103103
}
104+
NoteTree(NoteTreeState::GatewayMode) => "Gateway mode".to_owned(),
104105
NoteTree(NoteTreeState::MoveMode) => match &self.selected {
105106
SelectedItem::Note(Note { name, .. }) => {
106107
format!("Note move mode: '{name}'")
@@ -267,6 +268,7 @@ impl NotebookState {
267268
"[j] Select next".to_owned(),
268269
"[k] Select previous".to_owned(),
269270
"[G] Select last".to_owned(),
271+
"[g] Enter gateway mode".to_owned(),
270272
"[1-9] Add steps".to_owned(),
271273
"[>] Expand width".to_owned(),
272274
"[<] Shrink width".to_owned(),
@@ -313,6 +315,9 @@ impl NotebookState {
313315
"[Esc] Cancel".to_owned(),
314316
]
315317
}
318+
NoteTree(NoteTreeState::GatewayMode) => {
319+
vec!["[g] Select first".to_owned(), "[Esc] Cancel".to_owned()]
320+
}
316321
NoteTree(NoteTreeState::MoveMode) => {
317322
vec![
318323
"[j] Select next".to_owned(),

core/src/state/notebook/inner_state/note_tree.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{Event, NotebookTransition, Result, db::Db, state::notebook::Notebook
22

33
mod directory_more_actions;
44
mod directory_selected;
5+
mod gateway;
56
mod move_mode;
67
mod note_more_actions;
78
mod note_selected;
@@ -14,6 +15,7 @@ pub enum NoteTreeState {
1415
DirectorySelected,
1516
DirectoryMoreActions,
1617
Numbering(usize),
18+
GatewayMode,
1719
MoveMode,
1820
}
1921

@@ -31,6 +33,7 @@ pub async fn consume(
3133
NoteMoreActions => note_more_actions::consume(db, state, event).await,
3234
DirectoryMoreActions => directory_more_actions::consume(db, state, event).await,
3335
Numbering(n) => numbering::consume(state, n, event).await,
36+
GatewayMode => gateway::consume(state, event).await,
3437
MoveMode => move_mode::consume(db, state, event).await,
3538
}
3639
}

core/src/state/notebook/inner_state/note_tree/directory_selected.rs

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ pub async fn consume(
8181
Ok(NotebookTransition::None)
8282
}
8383
Key(KeyEvent::CapG) => Ok(NotebookTransition::NoteTree(NoteTreeTransition::SelectLast)),
84+
Key(KeyEvent::G) => {
85+
state.inner_state = InnerState::NoteTree(NoteTreeState::GatewayMode);
86+
87+
Ok(NotebookTransition::NoteTree(
88+
NoteTreeTransition::GatewayMode,
89+
))
90+
}
8491
Key(KeyEvent::AngleBracketOpen) => Ok(NotebookTransition::NoteTree(
8592
NoteTreeTransition::ShrinkWidth(1),
8693
)),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use crate::{
2+
Error, Event, KeyEvent, NotebookTransition, Result,
3+
state::notebook::{InnerState, NoteTreeState, NotebookState, SelectedItem},
4+
transition::NoteTreeTransition,
5+
};
6+
7+
pub async fn consume(state: &mut NotebookState, event: Event) -> Result<NotebookTransition> {
8+
use Event::*;
9+
10+
match event {
11+
Key(KeyEvent::G) => {
12+
state.inner_state = leave_gateway_mode(&state.selected)?;
13+
14+
Ok(NotebookTransition::NoteTree(
15+
NoteTreeTransition::SelectFirst,
16+
))
17+
}
18+
Key(KeyEvent::Esc) => {
19+
state.inner_state = leave_gateway_mode(&state.selected)?;
20+
21+
Ok(NotebookTransition::None)
22+
}
23+
event @ Key(_) => Ok(NotebookTransition::Inedible(event)),
24+
_ => Err(Error::Wip(
25+
"todo: NoteTree::GatewayMode::consume".to_owned(),
26+
)),
27+
}
28+
}
29+
30+
fn leave_gateway_mode(selected: &SelectedItem) -> Result<InnerState> {
31+
match selected {
32+
SelectedItem::Directory(_) => Ok(InnerState::NoteTree(NoteTreeState::DirectorySelected)),
33+
SelectedItem::Note(_) => Ok(InnerState::NoteTree(NoteTreeState::NoteSelected)),
34+
SelectedItem::None => Err(Error::Wip("todo: cannot leave gateway mode".to_owned())),
35+
}
36+
}

core/src/state/notebook/inner_state/note_tree/note_selected.rs

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ pub async fn consume(
7070
Ok(NotebookTransition::None)
7171
}
7272
Key(KeyEvent::CapG) => Ok(NotebookTransition::NoteTree(NoteTreeTransition::SelectLast)),
73+
Key(KeyEvent::G) => {
74+
state.inner_state = InnerState::NoteTree(NoteTreeState::GatewayMode);
75+
76+
Ok(NotebookTransition::NoteTree(
77+
NoteTreeTransition::GatewayMode,
78+
))
79+
}
7380
Key(KeyEvent::AngleBracketOpen) => Ok(NotebookTransition::NoteTree(
7481
NoteTreeTransition::ShrinkWidth(1),
7582
)),

core/src/transition.rs

+2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ pub enum NoteTreeTransition {
9494

9595
SelectNext(usize),
9696
SelectPrev(usize),
97+
SelectFirst,
9798
SelectLast,
9899
ExpandWidth(usize),
99100
ShrinkWidth(usize),
101+
GatewayMode,
100102
}
101103

102104
pub enum MoveModeTransition {

tui/src/context/notebook.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub const DIRECTORY_ACTIONS: [&str; 5] = [
4646
pub enum ContextState {
4747
NoteTreeBrowsing,
4848
NoteTreeNumbering,
49+
NoteTreeGateway,
4950
NoteActionsDialog,
5051
DirectoryActionsDialog,
5152
MoveMode,
@@ -228,6 +229,19 @@ impl NotebookContext {
228229
}
229230
}
230231

232+
pub fn select_first(&mut self) {
233+
let i = self
234+
.tree_items
235+
.iter()
236+
.enumerate()
237+
.find(|(_, item)| item.selectable)
238+
.map(|(i, _)| i);
239+
240+
if i.is_some() {
241+
self.tree_state.select(i);
242+
}
243+
}
244+
231245
pub fn select_last(&mut self) {
232246
let i = self
233247
.tree_items
@@ -326,7 +340,9 @@ impl NotebookContext {
326340

327341
match self.state {
328342
ContextState::NoteTreeBrowsing => self.consume_on_note_tree_browsing(code),
329-
ContextState::NoteTreeNumbering | ContextState::MoveMode => Action::PassThrough,
343+
ContextState::NoteTreeGateway
344+
| ContextState::NoteTreeNumbering
345+
| ContextState::MoveMode => Action::PassThrough,
330346
ContextState::EditorNormalMode { idle } => self.consume_on_editor_normal(input, idle),
331347
ContextState::EditorVisualMode => Action::PassThrough,
332348
ContextState::EditorInsertMode => self.consume_on_editor_insert(input),

tui/src/transitions/notebook.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl App {
3434
NoteTreeState::NoteSelected | NoteTreeState::DirectorySelected,
3535
) => ContextState::NoteTreeBrowsing,
3636
InnerState::NoteTree(NoteTreeState::Numbering(_)) => ContextState::NoteTreeNumbering,
37+
InnerState::NoteTree(NoteTreeState::GatewayMode) => ContextState::NoteTreeGateway,
3738
InnerState::NoteTree(NoteTreeState::NoteMoreActions) => ContextState::NoteActionsDialog,
3839
InnerState::NoteTree(NoteTreeState::DirectoryMoreActions) => {
3940
ContextState::DirectoryActionsDialog

tui/src/transitions/notebook/note_tree.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ impl App {
8080
let event = get_select_event(selected);
8181
self.glues.dispatch(event).await.log_unwrap();
8282
}
83+
NoteTreeTransition::SelectFirst => {
84+
self.context.notebook.select_first();
85+
86+
let selected = self.context.notebook.selected();
87+
let event = get_select_event(selected);
88+
self.glues.dispatch(event).await.log_unwrap();
89+
}
8390
NoteTreeTransition::SelectLast => {
8491
self.context.notebook.select_last();
8592

@@ -100,7 +107,8 @@ impl App {
100107

101108
self.context.notebook.tree_width = width;
102109
}
103-
NoteTreeTransition::ShowNoteActionsDialog(_)
110+
NoteTreeTransition::GatewayMode
111+
| NoteTreeTransition::ShowNoteActionsDialog(_)
104112
| NoteTreeTransition::ShowDirectoryActionsDialog(_) => {}
105113
}
106114

tui/src/views/body/notebook/note_tree.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ const NOTE_SYMBOL: &str = "󱇗 ";
2222
pub fn draw(frame: &mut Frame, area: Rect, context: &mut NotebookContext) {
2323
let note_tree_focused = matches!(
2424
context.state,
25-
ContextState::NoteTreeBrowsing | ContextState::NoteTreeNumbering | ContextState::MoveMode
25+
ContextState::NoteTreeBrowsing
26+
| ContextState::NoteTreeNumbering
27+
| ContextState::NoteTreeGateway
28+
| ContextState::MoveMode
2629
);
2730
let title = "[Browser]";
2831
let title = if note_tree_focused {

0 commit comments

Comments
 (0)