posint example: switch between synced editors based on list of editor types

This commit is contained in:
Michael Sippel 2024-09-02 00:05:36 +02:00
parent 9f53b65074
commit 9b5dfc9cca
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -63,76 +63,66 @@ async fn main() {
/* initially copy values from Vec to EditTree... /* initially copy values from Vec to EditTree...
*/ */
ctx.read().unwrap().apply_morphism( ctx.read().unwrap().build_repr_tree(
&rt_int, &rt_int,
&nested::repr_tree::morphism::MorphismType { Context::parse(&ctx, " ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16>~Char> ~ <Vec Char>"),
src_type: Context::parse(&ctx, " ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <Vec Char>"), vec![
dst_type: Context::parse(&ctx, " ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ EditTree") Context::parse(&ctx, " ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16>> ~ EditTree"),
}); Context::parse(&ctx, " ~ <PosInt 16 LittleEndian> ~ <Seq~List <Digit 16>> ~ EditTree"),
]);
/* set Hex-editor to be master fn set_master(
*/ ctx: &Arc<RwLock<Context>>,
rt_int.write().unwrap().detach( &ctx ); rt: &Arc<RwLock<ReprTree>>,
ctx.read().unwrap().apply_morphism( mut leaves: Vec< laddertypes::TypeTerm >,
&rt_int, master_idx: usize
&laddertypes::MorphismType { ) {
src_type: Context::parse(&ctx, " ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ EditTree"), eprintln!("set master to {}", master_idx);
dst_type: Context::parse(&ctx, " ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char> ~ EditTree") 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 = let editor_types = vec![
rt_int Context::parse(&ctx,
.descend(Context::parse(&ctx, " " ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16>> ~ EditTree"),
<PosInt 16 BigEndian> Context::parse(&ctx,
~ <Seq~List <Digit 16>~Char> " ~ <PosInt 16 LittleEndian> ~ <Seq~List <Digit 16>> ~ EditTree"),
")).unwrap() Context::parse(&ctx,
.edittree( &ctx ); " ~ <PosInt 16 BigEndian> ~ EditTree"),
Context::parse(&ctx,
" ~ <PosInt 8 BigEndian> ~ EditTree"),
Context::parse(&ctx,
" ~ <PosInt 2 BigEndian> ~ EditTree"),
];
let edittree_dec_be_list = set_master(&ctx, &rt_int, editor_types.clone(), 0);
rt_int
.descend(Context::parse(&ctx, "
<PosInt 10 BigEndian>
~ <Seq~List <Digit 10>~Char>
")).unwrap()
.edittree( &ctx );
let hex_digits_view = rt_int.descend(Context::parse(&ctx, " /* list of editors
<PosInt 16 LittleEndian>
~ <Seq <Digit 16> >
~ <List <Digit 16>
~ _2^64
~ machine.UInt64 >
")).expect("descend")
.view_list::<u64>()
.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, "
<PosInt 10 LittleEndian>
~ <Seq <Digit 10>>
~ <List <Digit 10>
~ _2^64
~ machine.UInt64 >
")).expect("descend")
.view_list::<u64>()
.map(|v| TerminalAtom::from(char::from_digit(*v as u32, 10)))
.to_sequence()
.to_grid_horizontal();
/* list of both editors
*/ */
let mut list_editor = nested::editors::list::ListEditor::new(ctx.clone(), Context::parse(&ctx, "<Seq Char>")); let mut list_editor = nested::editors::list::ListEditor::new(ctx.clone(), Context::parse(&ctx, "<Seq Char>"));
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()); let mut edittree = list_editor.into_node(SingletonBuffer::new(0).get_port());
/* cursors are a bit screwed initially so fix them up /* cursors are a bit screwed initially so fix them up
* TODO: how to fix this generally? * 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{ edittree.goto(TreeCursor{
leaf_mode: nested::editors::list::ListCursorMode::Insert, leaf_mode: nested::editors::list::ListCursorMode::Insert,
tree_addr: vec![0,0] tree_addr: vec![0,0]
@ -146,37 +136,23 @@ async fn main() {
*/ */
let ctx = ctx.clone(); let ctx = ctx.clone();
let rt_int = rt_int.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| { move |ev| {
let cur = edittree.read().unwrap().get_cursor(); let cur = edittree.read().unwrap().get_cursor();
if cur.tree_addr.len() > 0 { if cur.tree_addr.len() > 0 {
match cur.tree_addr[0] { let mut li = last_idx.write().unwrap();
0 => { let ci = cur.tree_addr[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, " ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char> ~ EditTree"),
dst_type: Context::parse(&ctx, " ~ <PosInt 10 BigEndian> ~ <Seq~List <Digit 10> ~ Char> ~ EditTree")
});
*li = 0; if *li != ci {
} eprintln!("----------------------------------");
} set_master(
1 => { &ctx,
let mut li = last_idx.write().unwrap(); &rt_int,
if *li != 1 { editor_types.clone(),
rt_int.write().unwrap().detach( &ctx ); ci as usize
ctx.read().unwrap().apply_morphism(&rt_int, &laddertypes::MorphismType { );
src_type: Context::parse(&ctx, " ~ <PosInt 10 BigEndian> ~ <Seq~List <Digit 10> ~ Char> ~ EditTree"), *li = ci;
dst_type: Context::parse(&ctx, " ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char> ~ EditTree")
});
*li = 1;
}
}
_=>{}
} }
} }
@ -199,30 +175,21 @@ async fn main() {
{ {
let rt_edittree = rt.descend(Context::parse(&ctx, "EditTree")).expect("descend"); let rt_edittree = rt.descend(Context::parse(&ctx, "EditTree")).expect("descend");
let halo_type = rt_edittree.read().unwrap().get_halo_type().clone(); let halo_type = rt_edittree.read().unwrap().get_halo_type().clone();
let edittree = rt_edittree.read().unwrap().get_view::<dyn r3vi::view::singleton::SingletonView<Item = Arc<RwLock<EditTree>>>>().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) ) 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)))) .map_item(|_pt, atom| atom.add_style_front(TerminalStyle::fg_color((90,90,90))))
.offset(Vector2::new(1,y))); .offset(Vector2::new(1,y)));
comp.push( edittree.display_view() comp.push( edittree.get().read().unwrap().display_view()
.offset(Vector2::new(1,y+1))); .offset(Vector2::new(1,y+1)));
} }
show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, "<PosInt 16 BigEndian> ~ <Seq~List <Digit 16>~Char>")).expect(""), 1); let mut y = 1;
show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, "<PosInt 10 BigEndian> ~ <Seq~List <Digit 10>~Char>")).expect(""), 4); for t in editor_types.iter() {
show_edit_tree(&ctx, &mut comp, &rt_int.descend(t.clone()).expect(""), y);
/* project the seq of u64 representations to a view y += 3;
*/ }
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)))
}));
} }
/* write the changes in the view of `term_port` to the terminal /* write the changes in the view of `term_port` to the terminal