Skip to content

Commit 9682b6b

Browse files
committed
rename from State to Snapshot
1 parent 33618e4 commit 9682b6b

File tree

15 files changed

+123
-137
lines changed

15 files changed

+123
-137
lines changed

src/core/checkbox/render.rs

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

1413
use super::Checkbox;

src/core/json/render.rs

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

1413
use super::{JsonSyntaxKind, JsonTree};

src/core/listbox/render.rs

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

1413
use super::Listbox;

src/core/text/render.rs

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

1110
#[derive(Clone)]

src/core/text_editor/render.rs

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

1414
use super::{History, Mode, Suggest, TextEditor};
@@ -222,7 +222,7 @@ impl AsAny for Renderer {
222222
}
223223
}
224224

225-
impl State<Renderer> {
225+
impl Snapshot<Renderer> {
226226
pub fn text_changed(&self) -> bool {
227227
self.before.texteditor.text() != self.after.borrow().texteditor.text()
228228
}

src/core/tree/render.rs

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

1413
use super::{Kind, Tree};

src/lib.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,12 @@ pub mod error;
102102
mod grapheme;
103103
mod pane;
104104
pub mod preset;
105-
pub mod render;
105+
mod snapshot;
106106
pub mod style;
107107
mod terminal;
108108
pub mod validate;
109109

110-
use std::io;
111-
use std::sync::Once;
110+
use std::{any::Any, io, sync::Once};
112111

113112
use crate::{
114113
crossterm::{
@@ -119,10 +118,44 @@ use crate::{
119118
},
120119
engine::Engine,
121120
error::{Error, Result},
122-
render::{EventAction, Renderable},
121+
pane::Pane,
123122
terminal::Terminal,
124123
};
125124

125+
/// Represents the action to be taken after an event is processed.
126+
///
127+
/// This enum is used to determine how the `Prompt::run` method should proceed
128+
/// after handling an event for a `Renderable` component.
129+
///
130+
/// - `Continue`: Indicates that the prompt should continue running and process further events.
131+
/// - `Quit`: Signals that the prompt should stop running. If any of the `Renderable` components
132+
/// returns `Quit`, a flag is set to indicate that the prompt should terminate. This allows
133+
/// for a graceful exit when the user has completed their interaction with the prompt or when
134+
/// an exit condition is met.
135+
#[derive(Eq, PartialEq)]
136+
pub enum EventAction {
137+
Continue,
138+
Quit,
139+
}
140+
141+
/// A trait for objects that can be rendered in the terminal.
142+
/// It requires the ability to create a pane, handle events,
143+
/// and perform cleanup.
144+
pub trait Renderable: AsAny {
145+
/// Creates a pane with the given width.
146+
fn make_pane(&self, width: u16) -> Pane;
147+
/// Handles terminal events.
148+
fn handle_event(&mut self, event: &Event) -> Result<EventAction>;
149+
/// Performs something (e.g. cleanup) after rendering is complete.
150+
fn postrun(&mut self);
151+
}
152+
153+
/// A trait for casting objects to `Any`, allowing for dynamic typing.
154+
pub trait AsAny {
155+
/// Returns `Any`.
156+
fn as_any(&self) -> &dyn Any;
157+
}
158+
126159
type Evaluate = dyn Fn(&Event, &Vec<Box<dyn Renderable>>) -> Result<bool>;
127160
type Output<T> = dyn Fn(&Vec<Box<dyn Renderable>>) -> Result<T>;
128161

src/preset/checkbox.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::{
44
checkbox,
55
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
66
error::Result,
7-
render::{Renderable, State},
7+
snapshot::Snapshot,
88
style::StyleBuilder,
9-
text, Prompt,
9+
text, Prompt, Renderable,
1010
};
1111

1212
/// Represents a checkbox component for creating
@@ -93,14 +93,14 @@ impl Checkbox {
9393
pub fn prompt(self) -> Result<Prompt<Vec<String>>> {
9494
Prompt::try_new(
9595
vec![
96-
Box::new(State::<text::Renderer>::new(self.title_renderer)),
97-
Box::new(State::<checkbox::Renderer>::new(self.checkbox_renderer)),
96+
Box::new(Snapshot::<text::Renderer>::new(self.title_renderer)),
97+
Box::new(Snapshot::<checkbox::Renderer>::new(self.checkbox_renderer)),
9898
],
9999
|_, _| Ok(true),
100100
|renderables: &Vec<Box<dyn Renderable + 'static>>| -> Result<Vec<String>> {
101101
Ok(renderables[1]
102102
.as_any()
103-
.downcast_ref::<State<checkbox::Renderer>>()
103+
.downcast_ref::<Snapshot<checkbox::Renderer>>()
104104
.unwrap()
105105
.after
106106
.borrow()

src/preset/json.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::{
22
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
33
error::Result,
44
json::{self, JsonNode, JsonPathSegment},
5-
render::{Renderable, State},
5+
snapshot::Snapshot,
66
style::StyleBuilder,
7-
text, Prompt,
7+
text, Prompt, Renderable,
88
};
99

1010
/// Represents a JSON preset for rendering JSON data and titles with customizable styles.
@@ -74,14 +74,14 @@ impl Json {
7474
pub fn prompt(self) -> Result<Prompt<Vec<JsonPathSegment>>> {
7575
Prompt::try_new(
7676
vec![
77-
Box::new(State::<text::Renderer>::new(self.title_renderer)),
78-
Box::new(State::<json::Renderer>::new(self.json_renderer)),
77+
Box::new(Snapshot::<text::Renderer>::new(self.title_renderer)),
78+
Box::new(Snapshot::<json::Renderer>::new(self.json_renderer)),
7979
],
8080
|_, _| Ok(true),
8181
|renderables: &Vec<Box<dyn Renderable + 'static>>| -> Result<Vec<JsonPathSegment>> {
8282
Ok(renderables[1]
8383
.as_any()
84-
.downcast_ref::<State<json::Renderer>>()
84+
.downcast_ref::<Snapshot<json::Renderer>>()
8585
.unwrap()
8686
.after
8787
.borrow()

src/preset/listbox.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::{
44
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
55
error::Result,
66
listbox,
7-
render::{Renderable, State},
7+
snapshot::Snapshot,
88
style::StyleBuilder,
9-
text, Prompt,
9+
text, Prompt, Renderable,
1010
};
1111

1212
/// A component for creating and managing a selectable list of options.
@@ -85,14 +85,14 @@ impl Listbox {
8585
pub fn prompt(self) -> Result<Prompt<String>> {
8686
Prompt::try_new(
8787
vec![
88-
Box::new(State::<text::Renderer>::new(self.title_renderer)),
89-
Box::new(State::<listbox::Renderer>::new(self.listbox_renderer)),
88+
Box::new(Snapshot::<text::Renderer>::new(self.title_renderer)),
89+
Box::new(Snapshot::<listbox::Renderer>::new(self.listbox_renderer)),
9090
],
9191
|_, _| Ok(true),
9292
|renderables: &Vec<Box<dyn Renderable + 'static>>| -> Result<String> {
9393
Ok(renderables[1]
9494
.as_any()
95-
.downcast_ref::<State<listbox::Renderer>>()
95+
.downcast_ref::<Snapshot<listbox::Renderer>>()
9696
.unwrap()
9797
.after
9898
.borrow()

0 commit comments

Comments
 (0)