lib-nested/nested/src/string_editor.rs

64 lines
1.8 KiB
Rust
Raw Normal View History

2020-12-14 19:36:21 +01:00
use {
std::sync::RwLock,
termion::event::{Key, Event},
2020-12-14 19:36:21 +01:00
crate::{
core::{ViewPort, OuterViewPort},
2021-01-18 17:00:53 +01:00
singleton::{SingletonView, SingletonBuffer},
vec::VecBuffer,
terminal::{TerminalView, TerminalEvent, TerminalEditor, TerminalEditorResult},
tree_nav::{TreeNav, TreeNavResult}
2020-12-14 19:36:21 +01:00
}
};
2021-01-06 21:35:46 +01:00
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct CharEditor {
data: SingletonBuffer<Option<char>>,
data_port: ViewPort<dyn SingletonView<Item = Option<char>>>
2020-12-14 19:36:21 +01:00
}
impl CharEditor {
2021-01-18 17:00:53 +01:00
pub fn new() -> Self {
let mut data_port = ViewPort::new();
CharEditor {
data: SingletonBuffer::new(None, data_port.inner()),
data_port
2020-12-14 19:36:21 +01:00
}
2021-01-18 17:00:53 +01:00
}
pub fn get_data_port(&self) -> OuterViewPort<dyn SingletonView<Item = Option<char>>> {
2021-01-18 17:00:53 +01:00
self.data_port.outer()
}
}
impl TreeNav for CharEditor {}
impl TerminalEditor for CharEditor {
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
crate::terminal::make_label(
&if let Some(c) = self.data.get() {
c.to_string()
} else {
"".to_string()
})
}
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
match event {
TerminalEvent::Input(Event::Key(Key::Char('\n'))) =>
TerminalEditorResult::Continue,
TerminalEvent::Input(Event::Key(Key::Char(c))) => {
self.data.set(Some(*c));
TerminalEditorResult::Exit
}
TerminalEvent::Input(Event::Key(Key::Backspace)) |
TerminalEvent::Input(Event::Key(Key::Delete)) => {
self.data.set(None);
TerminalEditorResult::Exit
}
_ => TerminalEditorResult::Continue
2020-12-14 19:36:21 +01:00
}
}
2021-01-18 17:00:53 +01:00
}
2020-12-14 19:36:21 +01:00
2021-01-18 17:00:53 +01:00