lib-nested/nested/src/integer/editor.rs

213 lines
6.5 KiB
Rust
Raw Normal View History

2021-08-31 02:10:10 +02:00
use {
crate::{
2022-12-18 01:39:01 +01:00
core::{OuterViewPort},
list::{PTYListEditor},
sequence::{SequenceView, SequenceViewExt, decorator::{PTYSeqDecorate, SeqDecorStyle}},
2021-11-19 12:19:52 +01:00
singleton::{SingletonBuffer, SingletonView},
2022-10-23 19:29:50 +02:00
vec::{VecBuffer},
index::{buffer::IndexBuffer},
2021-11-19 12:19:52 +01:00
terminal::{
TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle,
2022-10-23 19:29:50 +02:00
TerminalView, make_label
2021-11-19 12:19:52 +01:00
},
tree::{TreeCursor, TreeNav, TreeNavResult},
diagnostics::{Diagnostics, Message},
Nested
2021-11-19 12:19:52 +01:00
},
std::sync::Arc,
std::sync::RwLock,
termion::event::{Event, Key},
2022-10-23 19:29:50 +02:00
cgmath::{Vector2, Point2}
2021-08-31 02:10:10 +02:00
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct DigitEditor {
radix: u32,
2022-10-23 19:29:50 +02:00
data: SingletonBuffer<Option<char>>,
msg: VecBuffer<Message>,
2021-08-31 02:10:10 +02:00
}
impl DigitEditor {
pub fn new(radix: u32) -> Self {
DigitEditor {
radix,
data: SingletonBuffer::new(None),
2022-10-23 19:29:50 +02:00
msg: VecBuffer::new(),
2021-08-31 02:10:10 +02:00
}
}
pub fn get_data_port(&self) -> OuterViewPort<dyn SingletonView<Item = Option<u32>>> {
let radix = self.radix;
self.data.get_port().map(move |c| c?.to_digit(radix))
2021-08-31 02:10:10 +02:00
}
}
impl TreeNav for DigitEditor {}
impl TerminalEditor for DigitEditor {
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
let radix = self.radix;
self.data
.get_port()
2021-11-19 12:19:52 +01:00
.map(move |c| {
TerminalAtom::new(
c.unwrap_or('?'),
if c.unwrap_or('?').to_digit(radix).is_some() {
TerminalStyle::fg_color((100, 140, 100))
} else {
//TerminalStyle::bg_color((90, 10, 10))
TerminalStyle::fg_color((200, 40, 40))
},
)
})
.to_grid()
2021-08-31 02:10:10 +02:00
}
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
match event {
TerminalEvent::Input(Event::Key(Key::Ctrl('x')))
| TerminalEvent::Input(Event::Key(Key::Char(' ')))
2021-11-19 12:19:52 +01:00
| TerminalEvent::Input(Event::Key(Key::Char('\n'))) => TerminalEditorResult::Exit,
2021-08-31 02:10:10 +02:00
TerminalEvent::Input(Event::Key(Key::Char(c))) => {
self.data.set(Some(*c));
2022-10-23 19:29:50 +02:00
self.msg.clear();
if c.to_digit(self.radix).is_none() {
let mut mb = IndexBuffer::new();
mb.insert_iter(vec![
(Point2::new(1, 0), make_label("invalid digit '")),
(Point2::new(2, 0), make_label(&format!("{}", *c))
2022-12-18 01:39:01 +01:00
.map_item(|_p,a| a.add_style_back(TerminalStyle::fg_color((140,140,250))))),
2022-10-23 19:29:50 +02:00
(Point2::new(3, 0), make_label("'"))
]);
self.msg.push(crate::diagnostics::make_error(mb.get_port().flatten()));
}
2021-08-31 02:10:10 +02:00
TerminalEditorResult::Exit
}
2021-11-19 12:19:52 +01:00
TerminalEvent::Input(Event::Key(Key::Backspace))
| TerminalEvent::Input(Event::Key(Key::Delete)) => {
2021-08-31 02:10:10 +02:00
self.data.set(None);
2022-10-23 19:29:50 +02:00
self.msg.clear();
self.msg.push(crate::diagnostics::make_warn(make_label("empty digit")));
2021-08-31 02:10:10 +02:00
TerminalEditorResult::Exit
}
2021-11-19 12:19:52 +01:00
_ => TerminalEditorResult::Continue,
2021-08-31 02:10:10 +02:00
}
}
}
impl Nested for DigitEditor {}
2022-05-08 23:30:49 +02:00
2022-10-23 19:29:50 +02:00
impl Diagnostics for DigitEditor {
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>> {
self.msg.get_port().to_sequence()
}
}
2021-08-31 02:10:10 +02:00
pub struct PosIntEditor {
radix: u32,
digits_editor: PTYListEditor<DigitEditor>
2021-08-31 02:10:10 +02:00
}
impl PosIntEditor {
pub fn new(radix: u32) -> Self {
PosIntEditor {
radix,
digits_editor: PTYListEditor::new(
2022-08-12 18:57:40 +02:00
Box::new(move || Arc::new(RwLock::new(DigitEditor::new(radix)))) as Box<dyn Fn() -> Arc<RwLock<DigitEditor>> + Send + Sync>,
SeqDecorStyle::Hex,
None,
0
2021-11-19 12:19:52 +01:00
),
2021-08-31 02:10:10 +02:00
}
}
pub fn get_data_port(&self) -> OuterViewPort<dyn SequenceView<Item = u32>> {
let radix = self.radix;
self.digits_editor.editor
2021-11-19 12:19:52 +01:00
.get_data_port()
.filter_map(move |digit_editor| {
digit_editor.read().unwrap().data.get()?.to_digit(radix)
})
}
2021-09-06 00:08:36 +02:00
pub fn get_value(&self) -> u32 {
let mut value = 0;
let mut weight = 1;
2021-11-19 12:19:52 +01:00
for digit_value in self
.get_data_port()
.get_view()
.unwrap()
.iter()
.collect::<Vec<_>>()
.into_iter()
.rev()
{
2021-09-06 00:08:36 +02:00
value += digit_value * weight;
weight *= self.radix;
}
value
}
2021-08-31 02:10:10 +02:00
}
2022-10-23 19:29:50 +02:00
impl Diagnostics for PosIntEditor {
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>> {
self.digits_editor.get_msg_port()
}
}
2021-08-31 02:10:10 +02:00
impl TreeNav for PosIntEditor {
2021-11-19 12:19:52 +01:00
fn get_cursor(&self) -> TreeCursor {
self.digits_editor.get_cursor()
}
2022-06-19 23:13:21 +02:00
fn get_cursor_warp(&self) -> TreeCursor {
self.digits_editor.get_cursor_warp()
}
2021-11-19 12:19:52 +01:00
fn goto(&mut self, cur: TreeCursor) -> TreeNavResult {
self.digits_editor.goto(cur)
}
fn goby(&mut self, cur: Vector2<isize>) -> TreeNavResult {
self.digits_editor.goby(cur)
2021-11-19 12:19:52 +01:00
}
2021-08-31 02:10:10 +02:00
}
impl TerminalEditor for PosIntEditor {
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
2022-10-23 19:29:50 +02:00
match self.radix {
10 => {
self.digits_editor.editor
.get_seg_seq_view()
.pty_decorate(SeqDecorStyle::Plain, 0)
},
16 => {
self.digits_editor.editor
.get_seg_seq_view()
.pty_decorate(SeqDecorStyle::Hex, 0)
}
_ => {
self.digits_editor.editor
.get_seg_seq_view()
.pty_decorate(SeqDecorStyle::Plain, 0)
}
}
2021-08-31 02:10:10 +02:00
}
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
match event {
TerminalEvent::Input(Event::Key(Key::Ctrl('x')))
| TerminalEvent::Input(Event::Key(Key::Char(' ')))
2021-11-19 12:19:52 +01:00
| TerminalEvent::Input(Event::Key(Key::Char('\n'))) => {
self.digits_editor.up();
2021-08-31 02:10:10 +02:00
TerminalEditorResult::Exit
}
2021-11-19 12:19:52 +01:00
event => self.digits_editor.handle_terminal_event(event),
2021-08-31 02:10:10 +02:00
}
}
}
2022-05-08 23:30:49 +02:00
impl Nested for PosIntEditor {}
2022-05-08 23:30:49 +02:00