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

212 lines
6.1 KiB
Rust
Raw Normal View History

2021-08-31 02:10:10 +02:00
use {
2023-02-13 18:39:45 +01:00
r3vi::{
view::{
OuterViewPort,
singleton::*,
},
buffer::{
singleton::*,
vec::*,
index_hashmap::*
}
},
2021-08-31 02:10:10 +02:00
crate::{
2023-02-13 18:39:45 +01:00
type_system::{Context, TypeTerm, ReprTree},
editors::list::{ListEditor, PTYListController, PTYListStyle},
2021-11-19 12:19:52 +01:00
terminal::{
2023-02-13 18:39:45 +01:00
TerminalAtom, TerminalEvent, TerminalStyle, make_label
2021-11-19 12:19:52 +01:00
},
2023-02-13 18:39:45 +01:00
diagnostics::{Message},
2023-02-24 19:26:46 +01:00
tree::{NestedNode, TreeNavResult},
commander::ObjCommander
2021-11-19 12:19:52 +01:00
},
std::sync::Arc,
std::sync::RwLock,
std::iter::FromIterator,
2021-11-19 12:19:52 +01:00
termion::event::{Event, Key},
2023-02-13 18:39:45 +01:00
cgmath::{Point2}
2021-08-31 02:10:10 +02:00
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct DigitEditor {
2022-12-19 11:26:07 +01:00
ctx: Arc<RwLock<Context>>,
2021-08-31 02:10:10 +02:00
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
}
2023-02-24 19:26:46 +01:00
impl ObjCommander for DigitEditor {
fn send_cmd_obj(&mut self, cmd_obj: Arc<RwLock<ReprTree>>) -> TreeNavResult {
let cmd_obj = cmd_obj.read().unwrap();
let cmd_type = cmd_obj.get_type().clone();
2021-08-31 02:10:10 +02:00
if cmd_type == (&self.ctx, "( Char )").into() {
2023-02-24 19:26:46 +01:00
if let Some(cmd_view) = cmd_obj.get_view::<dyn SingletonView<Item = char>>() {
let c = cmd_view.get();
2022-10-23 19:29:50 +02:00
self.msg.clear();
2023-05-19 11:26:05 +02:00
if self.ctx.read().unwrap().meta_chars.contains(&c) {
return TreeNavResult::Exit;
} else if c.to_digit(self.radix).is_none() {
/* in case the character c is not in the range of digit-chars,
add a message to the diagnostics view
*/
let mut message = IndexBuffer::from_iter(vec![
2022-10-23 19:29:50 +02:00
(Point2::new(1, 0), make_label("invalid digit '")),
2023-02-24 19:26:46 +01:00
(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(message.get_port().flatten()));
} else {
self.data.set(Some(c));
2022-10-23 19:29:50 +02:00
}
2021-08-31 02:10:10 +02:00
}
}
2023-02-24 19:26:46 +01:00
TreeNavResult::Continue
2021-08-31 02:10:10 +02:00
}
}
2022-12-19 11:26:07 +01:00
impl DigitEditor {
pub fn new(ctx: Arc<RwLock<Context>>, radix: u32) -> Self {
DigitEditor {
ctx,
radix,
data: SingletonBuffer::new(None),
msg: VecBuffer::new(),
}
}
pub fn into_node(self, depth: usize) -> NestedNode {
2023-02-13 18:39:45 +01:00
let data = self.get_data();
2022-12-19 11:26:07 +01:00
let editor = Arc::new(RwLock::new(self));
let ed = editor.write().unwrap();
2022-12-19 11:26:07 +01:00
let r = ed.radix;
NestedNode::new(depth)
2022-12-19 11:26:07 +01:00
.set_ctx(ed.ctx.clone())
.set_cmd(editor.clone())
2023-02-13 18:39:45 +01:00
.set_data(data)
2022-12-19 11:26:07 +01:00
.set_view(
ed.data
.get_port()
.map(move |c| {
TerminalAtom::new(
c.unwrap_or('?'),
if c.unwrap_or('?').to_digit(r).is_some() {
TerminalStyle::fg_color((100, 140, 100))
} else {
//TerminalStyle::bg_color((90, 10, 10))
TerminalStyle::fg_color((200, 40, 40))
},
)
})
.to_grid()
)
.set_diag(
ed.msg.get_port().to_sequence()
)
}
2022-05-08 23:30:49 +02:00
2022-12-19 11:26:07 +01: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))
2022-10-23 19:29:50 +02:00
}
2023-02-13 18:39:45 +01:00
pub fn get_type(&self) -> TypeTerm {
2023-08-12 19:03:14 +02:00
TypeTerm::TypeID(self.ctx.read().unwrap().get_typeid("Digit").unwrap())
2023-02-13 18:39:45 +01:00
}
pub fn get_data(&self) -> Arc<RwLock<ReprTree>> {
let data_view = self.get_data_port();
ReprTree::ascend(
&ReprTree::new_leaf(
self.ctx.read().unwrap().type_term_from_str("( u32 )").unwrap(),
data_view.into()
),
self.get_type()
)
}
2022-10-23 19:29:50 +02:00
}
2021-08-31 02:10:10 +02:00
pub struct PosIntEditor {
radix: u32,
2022-12-19 11:26:07 +01:00
digits: NestedNode
2021-08-31 02:10:10 +02:00
}
impl PosIntEditor {
2022-12-19 11:26:07 +01:00
pub fn new(ctx: Arc<RwLock<Context>>, radix: u32) -> Self {
let mut node = Context::make_node(&ctx, (&ctx, format!("( List ( Digit {} ) )", radix).as_str()).into(), 0).unwrap();
PTYListController::for_node( &mut node, Some(' '), None );
PTYListStyle::for_node( &mut node,
(
match radix {
2 => "0d".into(),
16 => "0x".into(),
_ => "".into()
},
"".into(),
"".into()
)
);
2023-02-13 18:39:45 +01:00
// Set Type
let data = node.data.clone().unwrap();
node = node.set_data(ReprTree::ascend(
&data,
2023-08-12 19:03:14 +02:00
TypeTerm::App(vec![
TypeTerm::TypeID(ctx.read().unwrap().get_typeid("PosInt").unwrap()),
TypeTerm::Num(radix as i64).into(),
TypeTerm::TypeID(ctx.read().unwrap().get_typeid("BigEndian").unwrap())
2023-02-13 18:39:45 +01:00
]
2023-08-12 19:03:14 +02:00
)));
2023-02-13 18:39:45 +01:00
2021-08-31 02:10:10 +02:00
PosIntEditor {
radix,
2023-02-13 18:39:45 +01:00
digits: node
2021-08-31 02:10:10 +02:00
}
}
2022-12-19 11:26:07 +01:00
pub fn into_node(self) -> NestedNode {
self.digits
}
/*
pub fn get_data_port(&self) -> OuterViewPort<dyn SequenceView<Item = u32>> {
let radix = self.radix;
2022-12-19 11:26:07 +01:00
self.digits
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-12-19 11:26:07 +01:00
*/
2021-08-31 02:10:10 +02:00
}
2022-05-08 23:30:49 +02:00