Skip to content

Commit

Permalink
fixup: select, text and so on
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Dec 3, 2023
1 parent 7f06c34 commit 49c8959
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 158 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,22 @@ graph
subgraph Dataflow
Event --> EventHandler
subgraph TextEditorViewer as Viewable
EventHandler --> |edit| TextBuffer
TextBuffer --> |matrixify| Pane
EventHandler --> |edit| Text
Text --> |matrixify| Pane
end
Pane -->|extract| Lines
Lines --> F([Draw])
end
```

When an event comes in, it is handled by the handler inside the `TextEditorViewer`
component. The handler then edits (e.g. insert character) `TextBuffer`.
This `TextBuffer` is used to construct a `Pane`, which is essentially a matrix of
component. The handler then edits (e.g. insert character) `Text`.
This `Text` is used to construct a `Pane`, which is essentially a matrix of
lines divided by a specific width. The panes are extracted a certain number of
lines in order to fit within the terminal screen when rendering.
Finally, these Lines are passed to a `draw` function which renders them on the screen.

### Relationship between TextBuffer, TextEditorViewer, and Readline
### Relationship between Text, TextEditorViewer, and Readline

A preset is composed of a combination of multiple components.
Let's take the Readline preset as an example to explain.
Expand All @@ -155,15 +155,15 @@ Let's take the Readline preset as an example to explain.
input, error message presentation, and validation.
Readline leverages the capabilities of TextEditorViewer and State\<TextEditorViewer\> for
text editing and state management.
- TextBuffer
- TextBuffer is a low-level component responsible for managing text content.
- Text
- Text is a low-level component responsible for managing text content.
It handles tasks related to storing, editing, and tracking the cursor
position of text data.
- TextEditorViewer, State\<TextEditorViewer\> (viewable object)
- TextEditorViewer is a component that operates and displays text data
through TextBuffer.
through Text.
It accepts user text input, manages editing, and displays the content
while reflecting changes back to TextBuffer.
while reflecting changes back to Text.
- State\<TextEditorViewer\> represents the state of TextEditorViewer at different stages,
including the initial state, the state before editing, and the state after
editing. It holds snapshots of the TextEditorViewer at these different stages.
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,15 @@ pub use crossterm;
mod engine;
pub mod error;
mod grapheme;
mod history;
pub mod item_box;
mod pane;
pub mod preset;
pub mod select_box;
pub mod style;
pub mod suggest;
mod terminal;
mod text_buffer;
mod theme;
pub mod text;
pub mod theme;
pub mod tree;
mod validate;
pub mod validate;
pub mod view;

use std::io;
Expand Down
8 changes: 4 additions & 4 deletions src/preset/confirm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
error::Result,
text_buffer::TextBuffer,
text::Text,
theme::confirm::Theme,
validate::Validator,
view::{
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Confirm {
let text = text_editor_state
.after
.borrow()
.textbuffer
.text
.content_without_cursor();

let error_message_state = viewables[1]
Expand All @@ -78,7 +78,7 @@ impl Confirm {
if !ret {
error_message_state.after.borrow_mut().text =
validator.error_message(&text);
text_editor_state.after.borrow_mut().textbuffer = TextBuffer::default();
text_editor_state.after.borrow_mut().text = Text::default();
}
ret
}
Expand All @@ -96,7 +96,7 @@ impl Confirm {
.unwrap()
.after
.borrow()
.textbuffer
.text
.content_without_cursor())
},
)
Expand Down
4 changes: 2 additions & 2 deletions src/preset/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Password {
.unwrap()
.after
.borrow()
.textbuffer
.text
.content_without_cursor();

let error_message_state = viewables[2]
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Password {
.unwrap()
.after
.borrow()
.textbuffer
.text
.content_without_cursor())
},
)
Expand Down
15 changes: 7 additions & 8 deletions src/preset/queryselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use std::fmt::Display;
use crate::{
crossterm::event::Event,
error::Result,
item_box::ItemBox,
suggest::Suggest,
select_box::SelectBox,
theme::queryselect::Theme,
view::{
Mode, SelectViewer, SelectViewerBuilder, State, TextEditorViewer, TextEditorViewerBuilder,
TextViewerBuilder, Viewable,
Mode, SelectViewer, SelectViewerBuilder, State, Suggest, TextEditorViewer,
TextEditorViewerBuilder, TextViewerBuilder, Viewable,
},
Prompt,
};
Expand Down Expand Up @@ -102,11 +101,11 @@ impl QuerySelect {
let query = text_editor_state
.after
.borrow()
.textbuffer
.text
.content_without_cursor();

let list = filter(&query, &select_state.init.itembox.list);
select_state.after.borrow_mut().itembox = ItemBox { list, position: 0 };
let list = filter(&query, &select_state.init.selectbox.list);
select_state.after.borrow_mut().selectbox = SelectBox { list, position: 0 };
}
Ok(true)
},
Expand All @@ -117,7 +116,7 @@ impl QuerySelect {
.unwrap()
.after
.borrow()
.itembox
.selectbox
.get())
},
)
Expand Down
9 changes: 4 additions & 5 deletions src/preset/readline.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{
crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
error::Result,
suggest::Suggest,
theme::readline::Theme,
validate::Validator,
view::{
Mode, State, TextEditorViewer, TextEditorViewerBuilder, TextViewer, TextViewerBuilder,
Viewable,
Mode, State, Suggest, TextEditorViewer, TextEditorViewerBuilder, TextViewer,
TextViewerBuilder, Viewable,
},
Prompt,
};
Expand Down Expand Up @@ -93,7 +92,7 @@ impl Readline {
.unwrap()
.after
.borrow()
.textbuffer
.text
.content_without_cursor();

let error_message_state = viewables[2]
Expand Down Expand Up @@ -132,7 +131,7 @@ impl Readline {
.unwrap()
.after
.borrow()
.textbuffer
.text
.content_without_cursor())
},
)
Expand Down
2 changes: 1 addition & 1 deletion src/preset/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Select {
.unwrap()
.after
.borrow()
.itembox
.selectbox
.get())
},
)
Expand Down
22 changes: 11 additions & 11 deletions src/item_box.rs → src/select_box.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{fmt, iter::FromIterator};

#[derive(Clone, Default)]
pub struct ItemBox {
pub struct SelectBox {
pub list: Vec<String>,
pub position: usize,
}

impl<T: fmt::Display> FromIterator<T> for ItemBox {
impl<T: fmt::Display> FromIterator<T> for SelectBox {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self {
list: iter.into_iter().map(|e| format!("{}", e)).collect(),
Expand All @@ -15,7 +15,7 @@ impl<T: fmt::Display> FromIterator<T> for ItemBox {
}
}

impl ItemBox {
impl SelectBox {
pub fn content(&self) -> Vec<String> {
self.list.clone()
}
Expand Down Expand Up @@ -59,10 +59,10 @@ mod test {

#[test]
fn test() {
let mut ib = ItemBox::from_iter(["a", "b", "c"]);
assert!(!ib.backward());
ib.position = 1;
assert!(ib.backward());
let mut b = SelectBox::from_iter(["a", "b", "c"]);
assert!(!b.backward());
b.position = 1;
assert!(b.backward());
}
}

Expand All @@ -71,10 +71,10 @@ mod test {

#[test]
fn test() {
let mut ib = ItemBox::from_iter(["a", "b", "c"]);
assert!(ib.forward());
ib.position = ib.list.len() - 1;
assert!(!ib.forward());
let mut b = SelectBox::from_iter(["a", "b", "c"]);
assert!(b.forward());
b.position = b.list.len() - 1;
assert!(!b.forward());
}
}
}
Loading

0 comments on commit 49c8959

Please sign in to comment.