Skip to content

Commit 33618e4

Browse files
committed
add: eventaction to determine termination or not
1 parent c7d9c28 commit 33618e4

File tree

8 files changed

+133
-41
lines changed

8 files changed

+133
-41
lines changed

src/core/checkbox/render.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::{
77
},
88
grapheme::{trim, Grapheme, Graphemes, StyledGraphemes},
99
pane::Pane,
10-
render::{AsAny, Renderable},
10+
render::{AsAny, EventAction, Renderable},
11+
Error, Result,
1112
};
1213

1314
use super::Checkbox;
@@ -87,8 +88,21 @@ impl Renderable for Renderer {
8788
/// | <kbd> ↑ </kbd> | Move the cursor backward
8889
/// | <kbd> ↓ </kbd> | Move the cursor forward
8990
/// | <kbd> Space </kbd> | Put checkmark for the current item
90-
fn handle_event(&mut self, event: &Event) {
91+
fn handle_event(&mut self, event: &Event) -> Result<EventAction> {
9192
match event {
93+
Event::Key(KeyEvent {
94+
code: KeyCode::Enter,
95+
modifiers: KeyModifiers::NONE,
96+
kind: KeyEventKind::Press,
97+
state: KeyEventState::NONE,
98+
}) => return Ok(EventAction::Quit),
99+
Event::Key(KeyEvent {
100+
code: KeyCode::Char('c'),
101+
modifiers: KeyModifiers::CONTROL,
102+
kind: KeyEventKind::Press,
103+
state: KeyEventState::NONE,
104+
}) => return Err(Error::Interrupted("ctrl+c".into())),
105+
92106
// Move cursor.
93107
Event::Key(KeyEvent {
94108
code: KeyCode::Up,
@@ -115,6 +129,7 @@ impl Renderable for Renderer {
115129

116130
_ => (),
117131
}
132+
Ok(EventAction::Continue)
118133
}
119134

120135
fn postrun(&mut self) {

src/core/json/render.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::{
77
},
88
grapheme::{trim, StyledGraphemes},
99
pane::Pane,
10-
render::{AsAny, Renderable},
10+
render::{AsAny, EventAction, Renderable},
11+
Error, Result,
1112
};
1213

1314
use super::{JsonSyntaxKind, JsonTree};
@@ -200,8 +201,21 @@ impl Renderable for Renderer {
200201
/// | <kbd> ↑ </kbd> | Move the cursor backward
201202
/// | <kbd> ↓ </kbd> | Move the cursor forward
202203
/// | <kbd> Space </kbd> | Switch fold/unfold at the current node
203-
fn handle_event(&mut self, event: &Event) {
204+
fn handle_event(&mut self, event: &Event) -> Result<EventAction> {
204205
match event {
206+
Event::Key(KeyEvent {
207+
code: KeyCode::Enter,
208+
modifiers: KeyModifiers::NONE,
209+
kind: KeyEventKind::Press,
210+
state: KeyEventState::NONE,
211+
}) => return Ok(EventAction::Quit),
212+
Event::Key(KeyEvent {
213+
code: KeyCode::Char('c'),
214+
modifiers: KeyModifiers::CONTROL,
215+
kind: KeyEventKind::Press,
216+
state: KeyEventState::NONE,
217+
}) => return Err(Error::Interrupted("ctrl+c".into())),
218+
205219
// Move cursor.
206220
Event::Key(KeyEvent {
207221
code: KeyCode::Up,
@@ -232,6 +246,7 @@ impl Renderable for Renderer {
232246

233247
_ => (),
234248
}
249+
Ok(EventAction::Continue)
235250
}
236251

237252
fn postrun(&mut self) {

src/core/listbox/render.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::{
77
},
88
grapheme::{trim, Graphemes, StyledGraphemes},
99
pane::Pane,
10-
render::{AsAny, Renderable},
10+
render::{AsAny, EventAction, Renderable},
11+
Error, Result,
1112
};
1213

1314
use super::Listbox;
@@ -71,8 +72,21 @@ impl Renderable for Renderer {
7172
/// | :-- | :--
7273
/// | <kbd> ↑ </kbd> | Move the cursor backward
7374
/// | <kbd> ↓ </kbd> | Move the cursor forward
74-
fn handle_event(&mut self, event: &Event) {
75+
fn handle_event(&mut self, event: &Event) -> Result<EventAction> {
7576
match event {
77+
Event::Key(KeyEvent {
78+
code: KeyCode::Enter,
79+
modifiers: KeyModifiers::NONE,
80+
kind: KeyEventKind::Press,
81+
state: KeyEventState::NONE,
82+
}) => return Ok(EventAction::Quit),
83+
Event::Key(KeyEvent {
84+
code: KeyCode::Char('c'),
85+
modifiers: KeyModifiers::CONTROL,
86+
kind: KeyEventKind::Press,
87+
state: KeyEventState::NONE,
88+
}) => return Err(Error::Interrupted("ctrl+c".into())),
89+
7690
// Move cursor.
7791
Event::Key(KeyEvent {
7892
code: KeyCode::Up,
@@ -93,6 +107,7 @@ impl Renderable for Renderer {
93107

94108
_ => (),
95109
}
110+
Ok(EventAction::Continue)
96111
}
97112

98113
fn postrun(&mut self) {

src/core/text/render.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::{
44
crossterm::{event::Event, style::ContentStyle},
55
grapheme::{matrixify, StyledGraphemes},
66
pane::Pane,
7-
render::{AsAny, Renderable},
7+
render::{AsAny, EventAction, Renderable},
8+
Result,
89
};
910

1011
#[derive(Clone)]
@@ -27,7 +28,9 @@ impl Renderable for Renderer {
2728
)
2829
}
2930

30-
fn handle_event(&mut self, _event: &Event) {}
31+
fn handle_event(&mut self, _event: &Event) -> Result<EventAction> {
32+
Ok(EventAction::Continue)
33+
}
3134

3235
fn postrun(&mut self) {}
3336
}

src/core/text_editor/render.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::{
77
},
88
grapheme::{matrixify, StyledGraphemes},
99
pane::Pane,
10-
render::{AsAny, Renderable, State},
10+
render::{AsAny, EventAction, Renderable, State},
11+
Error, Result,
1112
};
1213

1314
use super::{History, Mode, Suggest, TextEditor};
@@ -85,8 +86,21 @@ impl Renderable for Renderer {
8586
/// | <kbd> Backspace </kbd> | Erase a character at the current cursor position
8687
/// | <kbd> Ctrl + U </kbd> | Erase all characters on the current line
8788
/// | <kbd> TAB </kbd> | Perform tab completion by searching for suggestions
88-
fn handle_event(&mut self, event: &Event) {
89+
fn handle_event(&mut self, event: &Event) -> Result<EventAction> {
8990
match event {
91+
Event::Key(KeyEvent {
92+
code: KeyCode::Enter,
93+
modifiers: KeyModifiers::NONE,
94+
kind: KeyEventKind::Press,
95+
state: KeyEventState::NONE,
96+
}) => return Ok(EventAction::Quit),
97+
Event::Key(KeyEvent {
98+
code: KeyCode::Char('c'),
99+
modifiers: KeyModifiers::CONTROL,
100+
kind: KeyEventKind::Press,
101+
state: KeyEventState::NONE,
102+
}) => return Err(Error::Interrupted("ctrl+c".into())),
103+
90104
// Move cursor.
91105
Event::Key(KeyEvent {
92106
code: KeyCode::Left,
@@ -191,6 +205,7 @@ impl Renderable for Renderer {
191205

192206
_ => (),
193207
}
208+
Ok(EventAction::Continue)
194209
}
195210

196211
fn postrun(&mut self) {

src/core/tree/render.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::{
77
},
88
grapheme::{trim, Graphemes, StyledGraphemes},
99
pane::Pane,
10-
render::{AsAny, Renderable},
10+
render::{AsAny, EventAction, Renderable},
11+
Error, Result,
1112
};
1213

1314
use super::{Kind, Tree};
@@ -99,8 +100,21 @@ impl Renderable for Renderer {
99100
/// | <kbd> ↑ </kbd> | Move the cursor backward
100101
/// | <kbd> ↓ </kbd> | Move the cursor forward
101102
/// | <kbd> Space </kbd> | Switch fold/unfold at the current node
102-
fn handle_event(&mut self, event: &Event) {
103+
fn handle_event(&mut self, event: &Event) -> Result<EventAction> {
103104
match event {
105+
Event::Key(KeyEvent {
106+
code: KeyCode::Enter,
107+
modifiers: KeyModifiers::NONE,
108+
kind: KeyEventKind::Press,
109+
state: KeyEventState::NONE,
110+
}) => return Ok(EventAction::Quit),
111+
Event::Key(KeyEvent {
112+
code: KeyCode::Char('c'),
113+
modifiers: KeyModifiers::CONTROL,
114+
kind: KeyEventKind::Press,
115+
state: KeyEventState::NONE,
116+
}) => return Err(Error::Interrupted("ctrl+c".into())),
117+
104118
// Move cursor.
105119
Event::Key(KeyEvent {
106120
code: KeyCode::Up,
@@ -131,6 +145,7 @@ impl Renderable for Renderer {
131145

132146
_ => (),
133147
}
148+
Ok(EventAction::Continue)
134149
}
135150

136151
fn postrun(&mut self) {

src/lib.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ use std::sync::Once;
113113
use crate::{
114114
crossterm::{
115115
cursor,
116-
event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
116+
event::{self, Event},
117117
execute,
118118
terminal::{disable_raw_mode, enable_raw_mode},
119119
},
120120
engine::Engine,
121121
error::{Error, Result},
122-
render::Renderable,
122+
render::{EventAction, Renderable},
123123
terminal::Terminal,
124124
};
125125

@@ -217,11 +217,29 @@ impl<T> Prompt<T> {
217217
loop {
218218
let ev = event::read()?;
219219

220-
for editor in &mut self.renderables {
221-
editor.handle_event(&ev);
220+
// This flow iterates through each renderable component,
221+
// handling the current event.
222+
// If a component signals to quit (EventAction::Quit),
223+
// it sets a flag to indicate the prompt should quit.
224+
// If an error occurs while handling the event,
225+
// it returns the error immediately.
226+
let mut should_quit = false;
227+
for renderable in &mut self.renderables {
228+
match renderable.handle_event(&ev) {
229+
Ok(EventAction::Quit) => {
230+
should_quit = true;
231+
break;
232+
}
233+
Err(e) => return Err(e),
234+
_ => (),
235+
}
222236
}
223237

224-
let finalizable = (self.evaluator)(&ev, &self.renderables)?;
238+
let is_ready_for_output = (self.evaluator)(&ev, &self.renderables)?;
239+
240+
if should_quit && is_ready_for_output {
241+
break;
242+
}
225243

226244
let size = engine.size()?;
227245
terminal.draw(
@@ -231,26 +249,6 @@ impl<T> Prompt<T> {
231249
.map(|editor| editor.make_pane(size.0))
232250
.collect(),
233251
)?;
234-
235-
match &ev {
236-
Event::Key(KeyEvent {
237-
code: KeyCode::Enter,
238-
modifiers: KeyModifiers::NONE,
239-
kind: KeyEventKind::Press,
240-
state: KeyEventState::NONE,
241-
}) => {
242-
if finalizable {
243-
break;
244-
}
245-
}
246-
Event::Key(KeyEvent {
247-
code: KeyCode::Char('c'),
248-
modifiers: KeyModifiers::CONTROL,
249-
kind: KeyEventKind::Press,
250-
state: KeyEventState::NONE,
251-
}) => return Err(Error::Interrupted("ctrl+c".into())),
252-
_ => (),
253-
}
254252
}
255253

256254
let ret = (self.output)(&self.renderables);

src/render.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
use std::{any::Any, cell::RefCell};
22

3-
use crate::{crossterm::event::Event, pane::Pane};
3+
use crate::{crossterm::event::Event, pane::Pane, Result};
4+
5+
/// Represents the action to be taken after an event is processed.
6+
///
7+
/// This enum is used to determine how the `Prompt::run` method should proceed
8+
/// after handling an event for a `Renderable` component.
9+
///
10+
/// - `Continue`: Indicates that the prompt should continue running and process further events.
11+
/// - `Quit`: Signals that the prompt should stop running. If any of the `Renderable` components
12+
/// returns `Quit`, a flag is set to indicate that the prompt should terminate. This allows
13+
/// for a graceful exit when the user has completed their interaction with the prompt or when
14+
/// an exit condition is met.
15+
#[derive(Eq, PartialEq)]
16+
pub enum EventAction {
17+
Continue,
18+
Quit,
19+
}
420

521
/// A trait for objects that can be rendered in the terminal.
622
/// It requires the ability to create a pane, handle events,
@@ -9,7 +25,7 @@ pub trait Renderable: AsAny {
925
/// Creates a pane with the given width.
1026
fn make_pane(&self, width: u16) -> Pane;
1127
/// Handles terminal events.
12-
fn handle_event(&mut self, event: &Event);
28+
fn handle_event(&mut self, event: &Event) -> Result<EventAction>;
1329
/// Performs something (e.g. cleanup) after rendering is complete.
1430
fn postrun(&mut self);
1531
}
@@ -47,9 +63,9 @@ impl<R: Clone + Renderable + 'static> Renderable for State<R> {
4763

4864
/// Updates the `before` state and delegates event handling
4965
/// to the `after` state.
50-
fn handle_event(&mut self, event: &Event) {
66+
fn handle_event(&mut self, event: &Event) -> Result<EventAction> {
5167
self.before = self.after.borrow().clone();
52-
self.after.borrow_mut().handle_event(event);
68+
self.after.borrow_mut().handle_event(event)
5369
}
5470

5571
/// Performs cleanup on the `after` state

0 commit comments

Comments
 (0)