wip: Refactor ReprTree; add ReprTreeBuilder

This commit is contained in:
Michael Sippel 2024-12-24 12:50:02 +01:00
parent 41c02465be
commit 26186b3375
Signed by: senvas
GPG key ID: F96CF119C34B64A6
20 changed files with 924 additions and 563 deletions
examples/tty-07-color

View file

@ -0,0 +1,19 @@
[package]
name = "tty-06-color"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
laddertypes = { path = "../../../lib-laddertypes" }
r3vi = { path = "../../../lib-r3vi" }
nested = { path = "../../lib-nested-core" }
nested-tty = { path = "../../lib-nested-tty" }
termion = "*"
cgmath = "*"
[dependencies.async-std]
version = "1.9.0"
features = ["unstable", "attributes"]

View file

@ -0,0 +1,232 @@
extern crate cgmath;
extern crate nested;
extern crate nested_tty;
extern crate r3vi;
extern crate termion;
use {
cgmath::Vector2,
nested::{
editors::{
ObjCommander
},
repr_tree::{Context, ReprTree, ReprTreeExt, ReprLeaf},
edit_tree::{EditTree, TreeNav, TreeCursor}
},
nested_tty::{
DisplaySegment, TTYApplication,
TerminalCompositor, TerminalStyle, TerminalView,
TerminalAtom, TerminalEvent
},
r3vi::{
buffer::{singleton::*, vec::*},
view::{port::UpdateTask, singleton::*, list::*, sequence::*},
projection::*
},
std::sync::{Arc, RwLock},
};
#[async_std::main]
async fn main() {
/* setup context
*/
let ctx = Arc::new(RwLock::new(Context::new()));
nested::editors::char::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() );
nested_tty::setup_edittree_hook(&ctx);
eprintln!(
"Char = {:?}\nu64 = {:?}\nEditTree = {:?}, <Vec EditTree> = {:?}",
Context::parse(&ctx, "Char"),
Context::parse(&ctx, "machine.UInt64"),
Context::parse(&ctx, "EditTree"),
Context::parse(&ctx, "<Vec EditTree>")
);
let mut red = nested::repr_tree::ReprTree::from_str(Context::parse(&ctx, "
~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char> ~ <Vec Char>
"),
"221"
);
ctx.read().unwrap().apply_morphism( &red,
&laddertypes::MorphismType {
src_type: Context::parse(&ctx, " ~ <PosInt 10 BigEndian> ~ <Seq~List <Digit 10> ~ Char > ~ <Vec Char>"),
dst_type: Context::parse(&ctx, "~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char > ~ EditTree")
});
let red_edit = ctx.read().unwrap().setup_edittree(
red.descend(Context::parse(&ctx, "
~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16>~Char>
")).unwrap(),
SingletonBuffer::new(0).get_port()
).unwrap();
let mut green = nested::repr_tree::ReprTree::from_str(Context::parse(&ctx, "
~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char> ~ <Vec Char>
"),
"220"
);
ctx.read().unwrap().apply_morphism( &green,
&laddertypes::MorphismType {
src_type: Context::parse(&ctx, " ~ <PosInt 10 BigEndian> ~ <Seq~List <Digit 10> ~ Char > ~ <Vec Char>"),
dst_type: Context::parse(&ctx, "~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char > ~ EditTree")
});
let green_edit = ctx.read().unwrap().setup_edittree(green.descend(Context::parse(&ctx, "
~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16>~Char>
")).unwrap(),
SingletonBuffer::new(0).get_port()
).unwrap();
let mut blue = nested::repr_tree::ReprTree::from_str(Context::parse(&ctx, "
~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char> ~ <Vec Char>
"),
"5"
);
ctx.read().unwrap().apply_morphism( &blue,
&laddertypes::MorphismType {
src_type: Context::parse(&ctx, " ~ <PosInt 10 BigEndian> ~ <Seq~List <Digit 10> ~ Char > ~ <Vec Char>"),
dst_type: Context::parse(&ctx, "~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char > ~ EditTree")
});
let blue_edit = ctx.read().unwrap().setup_edittree(
blue.descend(Context::parse(&ctx, "
~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16>~Char>
")).unwrap(),
SingletonBuffer::new(0).get_port()
).unwrap();
eprintln!("======\n M A K E L I S T E D I T O R\n======\n");
let mut color = nested::repr_tree::ReprTree::new_arc(
Context::parse(&ctx, "<List >")
);
color.insert_leaf(
Context::parse(&ctx, "
<List
~ <PosInt 16 BigEndian>
~ <Seq <Digit 16>>
~ <List <Digit 16>
~ Char >
>
~ <List EditTree>
~ <Vec EditTree>
"),
ReprLeaf::from_vec_buffer(VecBuffer::<
Arc<RwLock< EditTree >>
>::with_data(vec![
red_edit.get(),
green_edit.get(),
blue_edit.get()
]))
);
ctx.read().unwrap().apply_morphism(
&color,
&laddertypes::MorphismType {
src_type: Context::parse(&ctx, "<List ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char > ~ EditTree > ~ <Vec EditTree>"),
dst_type: Context::parse(&ctx, "<List ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char > > ~ EditTree")
});
let edit = ctx.read().unwrap().setup_edittree(
color.descend(Context::parse(&ctx, "
<List
~ < PosInt 16 BigEndian >
~ < Seq~List <Digit 16>
~ Char >
>
")).unwrap(),
SingletonBuffer::new(0).get_port()
).unwrap();
eprintln!(" edittree => list list char ");
ctx.read().unwrap().apply_morphism(
&color,
&laddertypes::MorphismType {
src_type: Context::parse(&ctx, "<List ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char > > ~ EditTree"),
dst_type: Context::parse(&ctx, "<List ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char > >")
});
eprintln!("list char ==> list u64");
ctx.read().unwrap().apply_morphism(
&color,
&laddertypes::MorphismType {
src_type: Context::parse(&ctx, "<List ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ Char>>"),
dst_type: Context::parse(&ctx, "<List ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ _2^64 ~ machine.UInt64>>")
});
return;
ctx.read().unwrap().apply_morphism(
&color,
&laddertypes::MorphismType {
src_type: Context::parse(&ctx, "<List ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16> ~ _2^64 ~ machine.UInt64 > >"),
dst_type: Context::parse(&ctx, "<List ~ <PosInt 10 BigEndian> ~ <Seq~List <Digit 10> ~ Char > >")
});
/*
let edit2 = ctx.read().unwrap().setup_edittree(
color.descend(Context::parse(&ctx, "
<List
~ < PosInt 10 BigEndian >
~ < Seq~List <Digit 10>
~ Char >
>
")).unwrap(),
SingletonBuffer::new(0).get_port()
).unwrap();
*/
return;
/* setup terminal
*/
let app = TTYApplication::new({
/* event handler
*/
let ctx = ctx.clone();
let edit = edit.get().clone();
edit.write().unwrap().goto(TreeCursor::home());
move |ev| {
edit.write().unwrap().send_cmd_obj( ev.to_repr_tree(&ctx) );
}
});
/* Setup the compositor to serve as root-view
* by routing it to the `app.port` Viewport,
* so it will be displayed on TTY-output.
*/
let compositor = TerminalCompositor::new(app.port.inner());
/* Now add some views to our compositor
*/
{
let mut comp = compositor.write().unwrap();
fn show_edit_tree( ctx: &Arc<RwLock<Context>>, comp: &mut TerminalCompositor, rt: &Arc<RwLock<ReprTree>>, y: i16 )
{
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::<dyn r3vi::view::singleton::SingletonView<Item = Arc<RwLock<EditTree>>>>().unwrap().get().read().unwrap().clone();
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()
.offset(Vector2::new(1,y+1)));
}
show_edit_tree( &ctx, &mut comp, &color.descend(Context::parse(&ctx, "<List ~ <PosInt 16 BigEndian> ~ <Seq~List <Digit 16>~Char>>")).unwrap(), 1 );
show_edit_tree( &ctx, &mut comp, &color.descend(Context::parse(&ctx, "<List ~ <PosInt 10 BigEndian> ~ <Seq~List <Digit 10>~Char>>")).unwrap(), 3 );
}
/* write the changes in the view of `term_port` to the terminal
*/
app.show().await.expect("output error!");
}