2021-08-31 02:10:10 +02:00
|
|
|
use {
|
|
|
|
crate::{
|
2021-11-19 12:19:52 +01:00
|
|
|
core::{OuterViewPort, ViewPort},
|
2022-06-17 15:48:13 +02:00
|
|
|
list::{PTYListEditor},
|
|
|
|
sequence::{SequenceView, SequenceViewExt, decorator::{PTYSeqDecorate, SeqDecorStyle}},
|
2021-11-19 12:19:52 +01:00
|
|
|
singleton::{SingletonBuffer, SingletonView},
|
|
|
|
terminal::{
|
|
|
|
TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle,
|
|
|
|
TerminalView,
|
|
|
|
},
|
|
|
|
tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult},
|
|
|
|
},
|
|
|
|
std::sync::Arc,
|
|
|
|
std::sync::RwLock,
|
|
|
|
termion::event::{Event, Key},
|
2022-06-17 15:48:13 +02:00
|
|
|
cgmath::Vector2
|
2021-08-31 02:10:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
|
|
|
|
|
|
|
pub struct DigitEditor {
|
|
|
|
radix: u32,
|
2022-06-17 15:48:13 +02:00
|
|
|
data: SingletonBuffer<Option<char>>
|
2021-08-31 02:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DigitEditor {
|
|
|
|
pub fn new(radix: u32) -> Self {
|
|
|
|
DigitEditor {
|
|
|
|
radix,
|
2022-06-17 15:48:13 +02:00
|
|
|
data: SingletonBuffer::new(None),
|
2021-08-31 02:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_data_port(&self) -> OuterViewPort<dyn SingletonView<Item = Option<u32>>> {
|
|
|
|
let radix = self.radix;
|
2022-06-17 15:48:13 +02:00
|
|
|
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;
|
2022-06-17 15:48:13 +02:00
|
|
|
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 {
|
2021-11-19 12:19:52 +01:00
|
|
|
TerminalEvent::Input(Event::Key(Key::Char(' ')))
|
|
|
|
| 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));
|
|
|
|
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);
|
|
|
|
TerminalEditorResult::Exit
|
|
|
|
}
|
2021-11-19 12:19:52 +01:00
|
|
|
_ => TerminalEditorResult::Continue,
|
2021-08-31 02:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 23:30:49 +02:00
|
|
|
impl TerminalTreeEditor for DigitEditor {}
|
|
|
|
|
2021-08-31 02:10:10 +02:00
|
|
|
pub struct PosIntEditor {
|
|
|
|
radix: u32,
|
2022-06-17 15:48:13 +02:00
|
|
|
digits_editor: PTYListEditor<DigitEditor>
|
2021-08-31 02:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PosIntEditor {
|
|
|
|
pub fn new(radix: u32) -> Self {
|
|
|
|
PosIntEditor {
|
|
|
|
radix,
|
2022-06-17 15:48:13 +02:00
|
|
|
digits_editor: PTYListEditor::new(
|
2021-11-19 12:19:52 +01:00
|
|
|
Box::new(move || Arc::new(RwLock::new(DigitEditor::new(radix)))),
|
2022-06-17 15:48:13 +02:00
|
|
|
SeqDecorStyle::Hex,
|
|
|
|
0
|
2021-11-19 12:19:52 +01:00
|
|
|
),
|
2021-08-31 02:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 03:03:51 +02:00
|
|
|
|
|
|
|
pub fn get_data_port(&self) -> OuterViewPort<dyn SequenceView<Item = u32>> {
|
|
|
|
let radix = self.radix;
|
2022-06-17 15:48:13 +02:00
|
|
|
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-08-31 03:03:51 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeNav for PosIntEditor {
|
2021-11-19 12:19:52 +01:00
|
|
|
fn get_cursor(&self) -> TreeCursor {
|
|
|
|
self.digits_editor.get_cursor()
|
|
|
|
}
|
|
|
|
fn goto(&mut self, cur: TreeCursor) -> TreeNavResult {
|
|
|
|
self.digits_editor.goto(cur)
|
|
|
|
}
|
2022-06-17 15:48:13 +02:00
|
|
|
fn goby(&mut self, cur: Vector2<isize>) -> TreeNavResult {
|
|
|
|
self.digits_editor.goby(cur)
|
2021-11-19 12:19:52 +01:00
|
|
|
}
|
2022-06-17 15:48:13 +02:00
|
|
|
|
2021-08-31 02:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TerminalEditor for PosIntEditor {
|
|
|
|
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
|
2022-06-17 15:48:13 +02:00
|
|
|
self.digits_editor.editor
|
2021-08-31 02:10:10 +02:00
|
|
|
.get_seg_seq_view()
|
2022-06-17 15:48:13 +02:00
|
|
|
.pty_decorate(SeqDecorStyle::Hex, 0)
|
2021-08-31 02:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
|
|
|
|
match event {
|
2021-11-19 12:19:52 +01:00
|
|
|
TerminalEvent::Input(Event::Key(Key::Char(' ')))
|
|
|
|
| TerminalEvent::Input(Event::Key(Key::Char('\n'))) => {
|
2021-08-31 03:03:51 +02:00
|
|
|
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 TerminalTreeEditor for PosIntEditor {}
|
|
|
|
|