From 863fe95848b67d86b6fb23d3fa8a3544043aeaef Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 16 Jan 2024 15:20:51 +0100 Subject: [PATCH] example wip --- examples/tty-02-node/src/main.rs | 142 ++++++++---------- lib-nested-core/src/editors/char/mod.rs | 8 +- lib-nested-core/src/editors/integer/editor.rs | 45 ++++++ lib-nested-core/src/editors/list/ctx.rs | 39 ++++- lib-nested-tty/src/editors/mod.rs | 39 ++--- lib-nested-tty/src/lib.rs | 4 +- 6 files changed, 168 insertions(+), 109 deletions(-) diff --git a/examples/tty-02-node/src/main.rs b/examples/tty-02-node/src/main.rs index c3bc61b..7731127 100644 --- a/examples/tty-02-node/src/main.rs +++ b/examples/tty-02-node/src/main.rs @@ -29,6 +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::list::init_ctx( ctx.clone() ); /* structure of Repr-Tree * @@ -47,12 +49,11 @@ async fn main() { * / | \ * TTY PixelBuf SDF */ - let rt_digit = ReprTree::new_arc( Context::parse(&ctx, "") ); -// let port_char = r3vi::view::ViewPort::>::new(); - let port_u32 = r3vi::view::ViewPort::>::new(); - let port_edit = r3vi::view::ViewPort::>::new(); - let port_char_edit = r3vi::view::ViewPort::>::new(); + let rt_digit = ReprTree::new_arc( Context::parse(&ctx, "") ); + /* add initial representation + * ~ Char + */ rt_digit.write().unwrap() .insert_leaf( vec![ Context::parse(&ctx, "Char") ].into_iter(), @@ -63,93 +64,73 @@ async fn main() { .descend(Context::parse(&ctx, "Char")).unwrap().read().unwrap() .get_port::>().unwrap().0; - rt_digit.write().unwrap() - .insert_leaf( - vec![ Context::parse(&ctx, "EditTree") ].into_iter(), - port_edit.outer().into() + ctx.read().unwrap() + .morphisms + .morph( + rt_digit.clone(), + &Context::parse(&ctx, "~EditTree") ); -/* - let rt_string = ReprTree::new_arc( Context::parse(&ctx, "") ); + let port_edit = rt_digit.read().unwrap() + .descend(Context::parse(&ctx, "EditTree")).unwrap() + .read().unwrap() + .get_port::> >>().unwrap(); - let vec_string = Arc::new(RwLock::new(Vec::new())); + /* setup TTY-Display for DigitEditor + */ + { + let et = port_edit.get_view().unwrap().get().clone(); + let mut et = et.write().unwrap(); + *et = nested_tty::editors::edittree_make_digit_view(et.clone()); + } + + //--- + let rt_string = ReprTree::new_arc( Context::parse(&ctx, ">") ); + ctx.read().unwrap() + .morphisms + .morph( + rt_string.clone(), + &Context::parse(&ctx, ">~EditTree") + ); + + let editport_string = rt_string.read().unwrap() + .descend(Context::parse(&ctx, "EditTree")).unwrap() + .read().unwrap() + .get_port::>> >().unwrap(); + + /* setup TTY-Display for ListEditor + */ + { + let et = editport_string.get_view().unwrap().get().clone(); + let mut et = et.write().unwrap(); + *et = nested_tty::editors::edittree_make_list_edit(et.clone()); + } +/* + let vec_string = Arc::new(RwLock::new(Vec::::new())); rt_string.write().unwrap() .insert_leaf( vec![ Context::parse(&ctx, "") ].into_iter(), - r3vi::view::ViewPort::with_view(vec_string).into_outer() + r3vi::view::ViewPort::with_view(vec_string).into_outer().into() + ); + + + rt_string.write().unwrap() + .insert_leaf( + vec![ Context::parse(&ctx, "EditTree") ].into_iter(), + r3vi::view::ViewPort::with_view() ); */ - /* setup projections between representations - */ - eprintln!("rt_digit = {:?}", rt_digit); - eprintln!("morph [ Char ==> Char~EditTree ]"); - - let rt_char = rt_digit.read().unwrap() - .descend(Context::parse(&ctx, "Char")) - .unwrap().clone(); - - eprintln!("rt_char = {:?}", rt_char); - - ctx.read().unwrap() - .morphisms - .morph( - rt_char.clone(), - &Context::parse(&ctx, "Char~EditTree") - ); - - eprintln!("rt_digit = {:?}", rt_char); - - let edittree_char = - ReprTree::descend_ladder( - &rt_digit, - vec![ - Context::parse(&ctx, "Char"), - Context::parse(&ctx, "EditTree") - ].into_iter() - ).unwrap() - .read().unwrap() - .get_view::>().unwrap() - .get(); - - let mut edit_char = edittree_char.get_edit::< nested::editors::char::CharEditor >().unwrap(); - port_char.attach_to( edit_char.read().unwrap().get_port() ); - - let buf_edit_char = r3vi::buffer::singleton::SingletonBuffer::new( edittree_char.clone() ); - port_char_edit.attach_to( buf_edit_char.get_port() ); - - // created by ==> ~EditTree - let mut node_edit_digit = - nested::editors::integer::DigitEditor::new( - ctx.clone(), - 16 - ).into_node( - r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() - ); - - node_edit_digit = nested_tty::editors::edittree_make_digit_view( node_edit_digit ); - let mut edit_digit = node_edit_digit.get_edit::< nested::editors::integer::DigitEditor >().unwrap(); -/* - // created by ==> ~U32 - port_u32.attach_to( port_char.outer().map(|c| c.to_digit(16).unwrap_or(0)) ); - // port_u32.attach_to( edit_digit.read().unwrap().get_data_port().map(|d| d.unwrap_or(0)) ); - - let port_proj_u32_to_char = port_u32.outer().map(|val| char::from_digit(val, 16).unwrap_or('?') ); - */ - - let buf_edit_digit = r3vi::buffer::singleton::SingletonBuffer::new( node_edit_digit ); - port_edit.attach_to( buf_edit_digit.get_port() ); - /* setup terminal */ let app = TTYApplication::new({ /* event handler */ let ctx = ctx.clone(); - let node1 = buf_edit_digit.clone(); + let et1 = editport_string.clone(); move |ev| { - node1.get().send_cmd_obj(ev.to_repr_tree(&ctx)); + et1.get_view().unwrap().get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx)); } }); @@ -165,19 +146,22 @@ async fn main() { }) .offset(Vector2::new(5, 0)), ); - compositor.write().unwrap().push( buf_edit_digit.get().display_view().offset(Vector2::new(0,2)) ); + compositor.write().unwrap().push( port_edit.get_view().unwrap().get().read().unwrap().display_view().offset(Vector2::new(0,2)) ); let label = ctx.read().unwrap().type_term_to_str(&rt_digit.read().unwrap().get_type()); compositor .write() .unwrap() .push(nested_tty::make_label(&label).offset(Vector2::new(0, 1))); -/* + + compositor.write().unwrap().push( editport_string.get_view().unwrap().get().read().unwrap().display_view().offset(Vector2::new(0,4)) ); + + let label = ctx.read().unwrap().type_term_to_str(&rt_string.read().unwrap().get_type()); compositor .write() .unwrap() - .push(node1.display_view().offset(Vector2::new(15, 2))); -*/ + .push(nested_tty::make_label(&label).offset(Vector2::new(0, 3))); + /* write the changes in the view of `term_port` to the terminal */ app.show().await.expect("output error!"); diff --git a/lib-nested-core/src/editors/char/mod.rs b/lib-nested-core/src/editors/char/mod.rs index 7be55a3..3b3c1ff 100644 --- a/lib-nested-core/src/editors/char/mod.rs +++ b/lib-nested-core/src/editors/char/mod.rs @@ -37,17 +37,13 @@ pub fn init_ctx( ctx: Arc> ) { ctx.clone(), r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() ); -/* - /* setup tty-view for EditTree - */ - edittree_char = nested_tty::editors::edittree_make_char_view( edittree_char ); -*/ + /* Insert EditTree into ReprTree */ let mut rt = rt.write().unwrap(); rt.insert_leaf( vec![ Context::parse(&ctx, "EditTree") ].into_iter(), - SingletonBuffer::new(edittree_char).get_port().into() + SingletonBuffer::new( Arc::new(RwLock::new( edittree_char )) ).get_port().into() ); } } diff --git a/lib-nested-core/src/editors/integer/editor.rs b/lib-nested-core/src/editors/integer/editor.rs index ae4bf9b..0a911c9 100644 --- a/lib-nested-core/src/editors/integer/editor.rs +++ b/lib-nested-core/src/editors/integer/editor.rs @@ -22,6 +22,51 @@ 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 { diff --git a/lib-nested-core/src/editors/list/ctx.rs b/lib-nested-core/src/editors/list/ctx.rs index 8cb8c37..90ef8c5 100644 --- a/lib-nested-core/src/editors/list/ctx.rs +++ b/lib-nested-core/src/editors/list/ctx.rs @@ -1,5 +1,5 @@ use { - r3vi::{view::{OuterViewPort, singleton::*}}, + r3vi::{view::{OuterViewPort, singleton::*}, buffer::singleton::*}, laddertypes::{TypeTerm}, crate::{ repr_tree::{Context}, @@ -10,10 +10,43 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> -pub fn init_ctx(ctx: &mut Context) { +pub fn init_ctx(ctx: Arc>) { + + ctx.write().unwrap().add_varname("Item"); + let mt = crate::repr_tree::MorphismType { + src_type: Context::parse(&ctx, ""), + dst_type: Context::parse(&ctx, "~EditTree") + }; + ctx.write().unwrap().morphisms.add_morphism( + mt, + { + let ctx = ctx.clone(); + move |rt, σ| { + let item_id = laddertypes::TypeID::Var( ctx.read().unwrap().get_var_typeid("Item").unwrap() ); + if let Some( item_type ) = σ.get( &item_id ) { + eprintln!("create list of {:?}", item_type); + let mut edittree_list = ListEditor::new( + ctx.clone(), + item_type.clone() + ).into_node( + r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() + ); + + let mut rt = rt.write().unwrap(); + rt.insert_leaf( + vec![ Context::parse(&ctx, "EditTree") ].into_iter(), + SingletonBuffer::new( Arc::new(RwLock::new( edittree_list )) ).get_port().into() + ); + } else { + eprintln!("no item type"); + } + } + } + ); +/* + ctx.add_typename("ListCmd".into()); ctx.add_list_typename("List".into()); -/* ctx.add_node_ctor( "List", Arc::new( |ctx: Arc>, ty: TypeTerm, depth: OuterViewPort>| { diff --git a/lib-nested-tty/src/editors/mod.rs b/lib-nested-tty/src/editors/mod.rs index 5d65af6..4e2e30a 100644 --- a/lib-nested-tty/src/editors/mod.rs +++ b/lib-nested-tty/src/editors/mod.rs @@ -61,21 +61,21 @@ pub fn edittree_make_digit_view( node } - /* -pub fn node_make_seq_view( - mut node: NestedNode -) -> NestedNode { +pub fn edittree_make_seq_view( + mut node: EditTree +) -> EditTree { node.disp.view .write().unwrap() .insert_branch(ReprTree::new_leaf( Context::parse(&node.ctx, "TerminalView"), - node.data - .read() + node.get_edit::< nested::editors::list::ListEditor >() .unwrap() - .get_port::>() - .expect("unable to get Seq-view") - .map(move |char_node| node_make_tty_view(char_node.clone()).display_view() ) + .read().unwrap() + .get_data_port() + .map(move |item_edittree| + edittree_make_tty_view( item_edittree ).display_view() + ) .wrap(make_label("("), make_label(")")) .to_grid_horizontal() .flatten() @@ -83,24 +83,25 @@ pub fn node_make_seq_view( )); node } +*/ -pub fn node_make_list_edit( - mut node: NestedNode -) -> NestedNode { +pub fn edittree_make_list_edit( + mut node: EditTree +) -> EditTree { list::PTYListStyle::for_node( &mut node, ("(", "", ")") ); list::PTYListController::for_node( &mut node, None, None ); node } - -pub fn node_make_tty_view( - node: NestedNode -) -> NestedNode { - if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "Char") { +/* +pub fn edittree_make_tty_view( + et: EditTree +) -> EditTree { + if et.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "Char") { node_make_char_view( node ) - } else if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "") { + } else if et.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "") { node_make_seq_view( node ) - } else if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "") { + } else if et.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "") { node_make_list_edit( node ) } else { eprintln!("couldnt add view"); diff --git a/lib-nested-tty/src/lib.rs b/lib-nested-tty/src/lib.rs index 07f49f6..f5631af 100644 --- a/lib-nested-tty/src/lib.rs +++ b/lib-nested-tty/src/lib.rs @@ -56,10 +56,10 @@ impl DisplaySegment for nested::edit_tree::EditTree { } else { - make_label("?") + make_label("# could not get ViewPort #") } } else { - make_label("?") + make_label("# No TTY View available #") .map_item(|_p,a| a.add_style_back(TerminalStyle::fg_color((220, 30, 30)))) } }