From 3ec8164202b982a109571c019872a4f1b3eadf62 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 2 Sep 2024 00:05:36 +0200 Subject: [PATCH] posint example: switch between synced editors based on list of editor types --- examples/tty-04-posint/src/main.rs | 169 ++++++++++++----------------- 1 file changed, 68 insertions(+), 101 deletions(-) diff --git a/examples/tty-04-posint/src/main.rs b/examples/tty-04-posint/src/main.rs index 906791e..4c91ee5 100644 --- a/examples/tty-04-posint/src/main.rs +++ b/examples/tty-04-posint/src/main.rs @@ -63,76 +63,66 @@ async fn main() { /* initially copy values from Vec to EditTree... */ - ctx.read().unwrap().apply_morphism( + ctx.read().unwrap().build_repr_tree( &rt_int, - &nested::repr_tree::morphism::MorphismType { - src_type: Context::parse(&ctx, "ℕ ~ ~ > ~ ~ Char> ~ "), - dst_type: Context::parse(&ctx, "ℕ ~ ~ > ~ ~ Char> ~ EditTree") - }); + Context::parse(&ctx, "ℕ ~ ~ ~Char> ~ "), + vec![ + Context::parse(&ctx, "ℕ ~ ~ > ~ EditTree"), + Context::parse(&ctx, "ℕ ~ ~ > ~ EditTree"), + ]); - /* set Hex-editor to be master - */ - rt_int.write().unwrap().detach( &ctx ); - ctx.read().unwrap().apply_morphism( - &rt_int, - &laddertypes::MorphismType { - src_type: Context::parse(&ctx, "ℕ ~ ~ > ~ ~ Char> ~ EditTree"), - dst_type: Context::parse(&ctx, "ℕ ~ ~ > ~ ~ Char> ~ EditTree") + fn set_master( + ctx: &Arc>, + rt: &Arc>, + mut leaves: Vec< laddertypes::TypeTerm >, + master_idx: usize + ) { + eprintln!("set master to {}", master_idx); + if master_idx < leaves.len() { + let master = leaves.remove( master_idx ); + rt.write().unwrap().detach( &ctx ); + ctx.read().unwrap().build_repr_tree( + rt, + master, + leaves + ); } - ); + } - let edittree_hex_be_list = - rt_int - .descend(Context::parse(&ctx, " - - ~ ~Char> - ")).unwrap() - .edittree( &ctx ); + let editor_types = vec![ + Context::parse(&ctx, + "ℕ ~ ~ > ~ EditTree"), + Context::parse(&ctx, + "ℕ ~ ~ > ~ EditTree"), + Context::parse(&ctx, + "ℕ ~ ~ EditTree"), + Context::parse(&ctx, + "ℕ ~ ~ EditTree"), + Context::parse(&ctx, + "ℕ ~ ~ EditTree"), + ]; - let edittree_dec_be_list = - rt_int - .descend(Context::parse(&ctx, " - - ~ ~Char> - ")).unwrap() - .edittree( &ctx ); + set_master(&ctx, &rt_int, editor_types.clone(), 0); - let hex_digits_view = rt_int.descend(Context::parse(&ctx, " - - ~ > - ~ - ~ ℤ_2^64 - ~ machine.UInt64 > - ")).expect("descend") - .view_list::() - .map(|v| TerminalAtom::from(char::from_digit(*v as u32, 16))) - .to_sequence() - .to_grid_horizontal(); - - let dec_digits_view = rt_int.descend(Context::parse(&ctx, " - - ~ > - ~ - ~ ℤ_2^64 - ~ machine.UInt64 > - ")).expect("descend") - .view_list::() - .map(|v| TerminalAtom::from(char::from_digit(*v as u32, 10))) - .to_sequence() - .to_grid_horizontal(); - - /* list of both editors + /* list of editors */ let mut list_editor = nested::editors::list::ListEditor::new(ctx.clone(), Context::parse(&ctx, "")); - list_editor.data.push( edittree_hex_be_list.get() ); - list_editor.data.push( edittree_dec_be_list.get() ); + + // add all desired editors to the list + for leaf in editor_types.iter() { + let et = + rt_int + .descend(leaf.clone()).unwrap() + .edittree(&ctx).get(); + et.write().unwrap().goto(TreeCursor::none()); + list_editor.data.push(et); + } + let mut edittree = list_editor.into_node(SingletonBuffer::new(0).get_port()); /* cursors are a bit screwed initially so fix them up * TODO: how to fix this generally? */ - edittree_hex_be_list.get().write().unwrap().goto(TreeCursor::none()); - edittree_dec_be_list.get().write().unwrap().goto(TreeCursor::none()); edittree.goto(TreeCursor{ leaf_mode: nested::editors::list::ListCursorMode::Insert, tree_addr: vec![0,0] @@ -146,37 +136,23 @@ async fn main() { */ let ctx = ctx.clone(); let rt_int = rt_int.clone(); - let last_idx = RwLock::new(1); + let last_idx = RwLock::new(0); + let editor_types = editor_types.clone(); move |ev| { - let cur = edittree.read().unwrap().get_cursor(); if cur.tree_addr.len() > 0 { - match cur.tree_addr[0] { - 0 => { - let mut li = last_idx.write().unwrap(); - if *li != 0 { - rt_int.write().unwrap().detach( &ctx ); - ctx.read().unwrap().apply_morphism(&rt_int, &laddertypes::MorphismType { - src_type: Context::parse(&ctx, "ℕ ~ ~ ~ Char> ~ EditTree"), - dst_type: Context::parse(&ctx, "ℕ ~ ~ ~ Char> ~ EditTree") - }); + let mut li = last_idx.write().unwrap(); + let ci = cur.tree_addr[0]; - *li = 0; - } - } - 1 => { - let mut li = last_idx.write().unwrap(); - if *li != 1 { - rt_int.write().unwrap().detach( &ctx ); - ctx.read().unwrap().apply_morphism(&rt_int, &laddertypes::MorphismType { - src_type: Context::parse(&ctx, "ℕ ~ ~ ~ Char> ~ EditTree"), - dst_type: Context::parse(&ctx, "ℕ ~ ~ ~ Char> ~ EditTree") - }); - - *li = 1; - } - } - _=>{} + if *li != ci { + eprintln!("----------------------------------"); + set_master( + &ctx, + &rt_int, + editor_types.clone(), + ci as usize + ); + *li = ci; } } @@ -189,7 +165,7 @@ async fn main() { * so it will be displayed on TTY-output. */ let compositor = TerminalCompositor::new(app.port.inner()); - + /* Now add some views to our compositor */ { @@ -199,30 +175,21 @@ async fn main() { { let rt_edittree = rt.descend(Context::parse(&ctx, "EditTree")).expect("descend"); let halo_type = rt_edittree.read().unwrap().get_halo_type().clone(); - let edittree = rt_edittree.read().unwrap().get_view::>>>().unwrap().get().read().unwrap().clone(); + let edittree = rt_edittree.edittree( &ctx ); comp.push( nested_tty::make_label( &ctx.read().unwrap().type_term_to_str(&halo_type) ) .map_item(|_pt, atom| atom.add_style_front(TerminalStyle::fg_color((90,90,90)))) .offset(Vector2::new(1,y))); - comp.push( edittree.display_view() + comp.push( edittree.get().read().unwrap().display_view() .offset(Vector2::new(1,y+1))); } - show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, " ~ ~Char>")).expect(""), 1); - show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, " ~ ~Char>")).expect(""), 4); - - /* project the seq of u64 representations to a view - */ - comp.push(nested_tty::make_label("dec: ").offset(Vector2::new(3,7))); - comp.push(dec_digits_view.offset(Vector2::new(8,7)).map_item(|_,a| { - a.add_style_back(TerminalStyle::fg_color((30,90,200))) - })); - - comp.push(nested_tty::make_label("hex: ").offset(Vector2::new(3,8))); - comp.push(hex_digits_view.offset(Vector2::new(8,8)).map_item(|_,a| { - a.add_style_back(TerminalStyle::fg_color((200, 200, 30))) - })); + let mut y = 1; + for t in editor_types.iter() { + show_edit_tree(&ctx, &mut comp, &rt_int.descend(t.clone()).expect(""), y); + y += 3; + } } /* write the changes in the view of `term_port` to the terminal