char editor: improve readablility, add comment & use IndexBuffer::from_iter

This commit is contained in:
Michael Sippel 2023-08-14 01:49:14 +02:00
parent 1388dcafe2
commit fa8b80678e
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -22,6 +22,7 @@ use {
}, },
std::sync::Arc, std::sync::Arc,
std::sync::RwLock, std::sync::RwLock,
std::iter::FromIterator,
termion::event::{Event, Key}, termion::event::{Event, Key},
cgmath::{Point2} cgmath::{Point2}
}; };
@ -40,28 +41,31 @@ impl ObjCommander for DigitEditor {
let cmd_obj = cmd_obj.read().unwrap(); let cmd_obj = cmd_obj.read().unwrap();
let cmd_type = cmd_obj.get_type().clone(); let cmd_type = cmd_obj.get_type().clone();
let char_type = (&self.ctx, "( Char )").into(); if cmd_type == (&self.ctx, "( Char )").into() {
//let _term_event_type = (&ctx, "( TerminalEvent )").into();
if cmd_type == char_type {
if let Some(cmd_view) = cmd_obj.get_view::<dyn SingletonView<Item = char>>() { if let Some(cmd_view) = cmd_obj.get_view::<dyn SingletonView<Item = char>>() {
let c = cmd_view.get(); let c = cmd_view.get();
self.data.set(Some(c));
self.msg.clear(); self.msg.clear();
if self.ctx.read().unwrap().meta_chars.contains(&c) { if self.ctx.read().unwrap().meta_chars.contains(&c) {
return TreeNavResult::Exit; return TreeNavResult::Exit;
}
else if c.to_digit(self.radix).is_none() { } else if c.to_digit(self.radix).is_none() {
let mut mb = IndexBuffer::new(); /* in case the character c is not in the range of digit-chars,
mb.insert_iter(vec![ add a message to the diagnostics view
*/
let mut message = IndexBuffer::from_iter(vec![
(Point2::new(1, 0), make_label("invalid digit '")), (Point2::new(1, 0), make_label("invalid digit '")),
(Point2::new(2, 0), make_label(&format!("{}", c)) (Point2::new(2, 0), make_label(&format!("{}", c))
.map_item(|_p,a| a.add_style_back(TerminalStyle::fg_color((140,140,250))))), .map_item(|_p,a| a.add_style_back(TerminalStyle::fg_color((140,140,250))))),
(Point2::new(3, 0), make_label("'")) (Point2::new(3, 0), make_label("'"))
]); ]);
self.msg.push(crate::diagnostics::make_error(mb.get_port().flatten()));
self.msg.push(crate::diagnostics::make_error(message.get_port().flatten()));
} else {
self.data.set(Some(c));
} }
} }
} }