From d7d0a46c7beb8962fb1398d757317b75979ab903 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 10 Mar 2024 16:17:24 +0100 Subject: [PATCH] move digit into separate module --- examples/tty-02-node/src/main.rs | 7 +- lib-nested-core/src/editors/integer/ctx.rs | 37 +--- lib-nested-core/src/editors/integer/editor.rs | 160 +----------------- lib-nested-core/src/editors/integer/mod.rs | 2 +- lib-nested-core/src/editors/mod.rs | 1 + lib-nested-tty/src/editors/mod.rs | 2 +- 6 files changed, 16 insertions(+), 193 deletions(-) diff --git a/examples/tty-02-node/src/main.rs b/examples/tty-02-node/src/main.rs index 8bfdaca..b44d2bf 100644 --- a/examples/tty-02-node/src/main.rs +++ b/examples/tty-02-node/src/main.rs @@ -29,7 +29,8 @@ async fn main() { let ctx = Arc::new(RwLock::new(Context::new())); nested::editors::char::init_ctx( ctx.clone() ); - nested::editors::integer::editor::init_ctx( ctx.clone() ); + nested::editors::digit::init_ctx( ctx.clone() ); + nested::editors::integer::init_ctx( ctx.clone() ); nested::editors::list::init_ctx( ctx.clone() ); let char_type = Context::parse(&ctx, "Char"); @@ -85,9 +86,9 @@ async fn main() { * / | \ * / | \ * / | \ - * u32 [ EditTree ] Char + * u32 EditTree Char * - Editor \ - * - Display [ EditTree ] + * - Display EditTree * / | \ - Editor * / | \ - Display * TTY PixelBuf SDF / | \ diff --git a/lib-nested-core/src/editors/integer/ctx.rs b/lib-nested-core/src/editors/integer/ctx.rs index 86875d0..b8bfac2 100644 --- a/lib-nested-core/src/editors/integer/ctx.rs +++ b/lib-nested-core/src/editors/integer/ctx.rs @@ -15,39 +15,8 @@ use { std::sync::{Arc, RwLock} }; -pub fn init_ctx(ctx: &mut Context) { +pub fn init_ctx(ctx: Arc>) { /* - ctx.add_typename("MachineInt".into()); - ctx.add_typename("u32".into()); - ctx.add_typename("u64".into()); - ctx.add_typename("LittleEndian".into()); - ctx.add_typename("BigEndian".into()); - - ctx.add_node_ctor( - "Digit", Arc::new( - |ctx: Arc>, ty: TypeTerm, depth: OuterViewPort>| { - match ty { - TypeTerm::App(args) => { - if args.len() > 1 { - match args[1] { - TypeTerm::Num(radix) => { - let node = DigitEditor::new(ctx.clone(), radix as u32).into_node(depth); - Some( - node - ) - }, - _ => None - } - } else { - None - } - } - _ => None - } - } - ) - ); - ctx.add_list_typename("PosInt".into()); let pattern = MorphismTypePattern { src_tyid: ctx.get_typeid("List"), @@ -127,7 +96,8 @@ pub fn init_ctx(ctx: &mut Context) { } ) ); -*/ + */ + /* ctx.add_typename("Date".into()); ctx.add_typename("ISO-8601".into()); ctx.add_typename("TimeSince".into()); @@ -137,5 +107,6 @@ pub fn init_ctx(ctx: &mut Context) { ctx.add_typename("Duration".into()); ctx.add_typename("Seconds".into()); ctx.add_typename("ℕ".into()); + */ } diff --git a/lib-nested-core/src/editors/integer/editor.rs b/lib-nested-core/src/editors/integer/editor.rs index 934888a..4fdfe23 100644 --- a/lib-nested-core/src/editors/integer/editor.rs +++ b/lib-nested-core/src/editors/integer/editor.rs @@ -12,7 +12,11 @@ use { }, laddertypes::{TypeTerm}, crate::{ - editors::{list::{ListCmd}, ObjCommander}, + editors::{ + digit::DigitEditor, + list::{ListCmd}, + ObjCommander + }, repr_tree::{Context, ReprTree}, edit_tree::{EditTree, TreeNav, TreeNavResult, TreeCursor, diagnostics::{Message}}, }, @@ -22,160 +26,6 @@ use { cgmath::{Point2} }; - -pub fn init_ctx( ctx: Arc> ) { - - // todo: proper scoping of Radix variable - ctx.write().unwrap().add_varname("Radix"); - let morphtype = - crate::repr_tree::MorphismType { - src_type: Context::parse(&ctx, ""), - dst_type: Context::parse(&ctx, "~EditTree") - }; - - ctx.write().unwrap() - .morphisms - .add_morphism( - morphtype, - { - let ctx = ctx.clone(); - move |rt, σ| { - let radix = - match σ.get( &laddertypes::TypeID::Var(ctx.read().unwrap().get_var_typeid("Radix").unwrap()) ) { - Some(TypeTerm::Num(n)) => *n as u32, - _ => 0 - }; - - /* Create EditTree object - */ - let mut edittree_digit = DigitEditor::new( - ctx.clone(), - radix - ).into_node( - r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() - ); - - /* Insert EditTree into ReprTree - */ - let mut rt = rt.write().unwrap(); - rt.insert_leaf( - vec![ Context::parse(&ctx, "EditTree") ].into_iter(), - SingletonBuffer::new( Arc::new(RwLock::new(edittree_digit)) ).get_port().into() - ); - } - } - ); -} - -//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - -pub struct DigitEditor { - ctx: Arc>, - radix: u32, - data: SingletonBuffer>, - msg: VecBuffer, -} - -impl ObjCommander for DigitEditor { - fn send_cmd_obj(&mut self, cmd_obj: Arc>) -> TreeNavResult { - let cmd_obj = cmd_obj.read().unwrap(); - let cmd_type = cmd_obj.get_type().clone(); - - if cmd_type == Context::parse(&self.ctx, "Char") { - if let Some(cmd_view) = cmd_obj.get_view::>() { - let c = cmd_view.get(); - - self.msg.clear(); - - 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 message = IndexBuffer::from_iter(vec![ - (Point2::new(1, 0), make_label("invalid digit '")), - (Point2::new(2, 0), make_label(&format!("{}", c)) - .map_item(|_p,a| a.add_style_back(TerminalStyle::fg_color((140,140,250))))), - (Point2::new(3, 0), make_label("'")) - ]); - - self.msg.push(crate::diagnostics::make_error(message.get_port().flatten())); -*/ - - self.data.set(Some(c)); - } else { - self.data.set(Some(c)); - } - } - } - - TreeNavResult::Continue - } -} - -impl DigitEditor { - pub fn new(ctx: Arc>, radix: u32) -> Self { - DigitEditor { - ctx, - radix, - data: SingletonBuffer::new(None), - msg: VecBuffer::new(), - } - } - - pub fn into_node(self, depth: OuterViewPort>) -> EditTree { - let data = self.get_data(); - let editor = Arc::new(RwLock::new(self)); - let ed = editor.write().unwrap(); - let r = ed.radix; - - EditTree::new(ed.ctx.clone(), depth) - .set_editor(editor.clone()) - .set_cmd(editor.clone()) - .set_diag( - ed.msg.get_port().to_sequence() - ) - } - - pub fn attach_to(&mut self, source: OuterViewPort>) { - /* - source.add_observer( - Arc::new(NotifyFnObserver::new(|_msg| { - self.data.set( source.get() ) - })) - ); - */ - } - - pub fn get_data_port(&self) -> OuterViewPort>> { - let radix = self.radix; - self.data.get_port().map(move |c| - if let Some(d) = c.unwrap_or('?').to_digit(radix) { - Ok(d) - } else { - Err(c.unwrap_or('?')) - } - ) - } - - pub fn get_type(&self) -> TypeTerm { - TypeTerm::TypeID(self.ctx.read().unwrap().get_typeid("Digit").unwrap()) - } - - pub fn get_data(&self) -> Arc> { - ReprTree::ascend( - &ReprTree::new_leaf( - self.ctx.read().unwrap().type_term_from_str("").unwrap(), - self.get_data_port().into() - ), - self.get_type() - ) - } -} - pub struct PosIntEditor { radix: u32, digits: EditTree, diff --git a/lib-nested-core/src/editors/integer/mod.rs b/lib-nested-core/src/editors/integer/mod.rs index bdbf565..70b0759 100644 --- a/lib-nested-core/src/editors/integer/mod.rs +++ b/lib-nested-core/src/editors/integer/mod.rs @@ -5,7 +5,7 @@ pub mod ctx; pub use { add::Add, - editor::{DigitEditor, PosIntEditor}, + editor::PosIntEditor, radix::RadixProjection, ctx::init_ctx }; diff --git a/lib-nested-core/src/editors/mod.rs b/lib-nested-core/src/editors/mod.rs index 3a720c3..4de5274 100644 --- a/lib-nested-core/src/editors/mod.rs +++ b/lib-nested-core/src/editors/mod.rs @@ -4,6 +4,7 @@ pub mod list; //pub mod sum; pub mod char; +pub mod digit; pub mod integer; //pub mod typeterm; diff --git a/lib-nested-tty/src/editors/mod.rs b/lib-nested-tty/src/editors/mod.rs index 3133edd..8194268 100644 --- a/lib-nested-tty/src/editors/mod.rs +++ b/lib-nested-tty/src/editors/mod.rs @@ -44,7 +44,7 @@ pub fn edittree_make_digit_view( .write().unwrap() .insert_branch(ReprTree::new_leaf( Context::parse(&node.ctx, "TerminalView"), - node.get_edit::< nested::editors::integer::DigitEditor >() + node.get_edit::< nested::editors::digit::DigitEditor >() .unwrap() .read() .unwrap()