From 25d8acdb72ef72d323a9ddc2657f35b2a81e48a6 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Wed, 29 Nov 2023 01:23:41 +0100 Subject: [PATCH] first steps in reactivating pty-list editor --- examples/tty-02-node/src/main.rs | 155 ++++++++++++++---- lib-nested-core/src/edit_tree/node.rs | 56 +------ lib-nested-core/src/editors/char/mod.rs | 7 - lib-nested-core/src/editors/list/editor.rs | 6 +- .../src/{edit-tree => edit_tree}/color.rs | 2 +- .../{edit-tree => edit_tree}/cursor_widget.rs | 0 .../{edit-tree => edit_tree}/diagnostics.rs | 0 lib-nested-tty/src/edit_tree/mod.rs | 3 + lib-nested-tty/src/editors/list.rs | 54 +++--- lib-nested-tty/src/editors/mod.rs | 3 + lib-nested-tty/src/editors/mod.rsr | 0 lib-nested-tty/src/lib.rs | 24 ++- 12 files changed, 191 insertions(+), 119 deletions(-) rename lib-nested-tty/src/{edit-tree => edit_tree}/color.rs (96%) rename lib-nested-tty/src/{edit-tree => edit_tree}/cursor_widget.rs (100%) rename lib-nested-tty/src/{edit-tree => edit_tree}/diagnostics.rs (100%) create mode 100644 lib-nested-tty/src/edit_tree/mod.rs create mode 100644 lib-nested-tty/src/editors/mod.rs delete mode 100644 lib-nested-tty/src/editors/mod.rsr diff --git a/examples/tty-02-node/src/main.rs b/examples/tty-02-node/src/main.rs index fb5b1f8..eb3515e 100644 --- a/examples/tty-02-node/src/main.rs +++ b/examples/tty-02-node/src/main.rs @@ -7,7 +7,7 @@ extern crate termion; use { cgmath::Vector2, nested::{ - edit_tree::NestedNode, + edit_tree::{NestedNode, TreeCursor, TreeNav}, repr_tree::{Context, ReprTree}, }, nested_tty::{ @@ -17,7 +17,8 @@ use { }, r3vi::{ buffer::singleton::*, - view::{port::UpdateTask, singleton::*, ViewPort}, + view::{port::UpdateTask, singleton::*, sequence::*, ViewPort}, + projection::decorate_sequence::* }, std::sync::{Arc, Mutex, RwLock}, termion::event::{Event, Key}, @@ -26,48 +27,134 @@ use { fn node_make_char_view( node: NestedNode ) -> NestedNode { - let char_view = node.data - .read() - .unwrap() - .get_port::>() - .expect("unable to get Char-view") - .map(move |c| TerminalAtom::from(if c == '\0' { ' ' } else { c })) - .to_grid(); + node.display + .write().unwrap() + .insert_branch(ReprTree::new_leaf( + Context::parse(&node.ctx, "TerminalView"), + node.data + .read() + .unwrap() + .get_port::>() + .expect("unable to get Char-view") + .map(move |c| TerminalAtom::from(if c == '\0' { ' ' } else { c })) + .to_grid() + .into(), + )); - let mut display_rt = ReprTree::new(Context::parse(&node.ctx, "Display")); + node +} - display_rt.insert_branch(ReprTree::new_leaf( - Context::parse(&node.ctx, "TerminalView"), - char_view.into(), - )); +fn node_make_list_view( + mut node: NestedNode +) -> NestedNode { + eprintln!("add list display type"); + node.display + .write().unwrap() + .insert_branch(ReprTree::new_leaf( + Context::parse(&node.ctx, "TerminalView"), - node.set_view( - Arc::new(RwLock::new(display_rt)) - ) + node.data + .read() + .unwrap() + .get_port::< dyn SequenceView >() + .expect("unable to get Seq-view") + .map(move |char_node| node_make_view(char_node.clone()).display_view() ) + .wrap(nested_tty::make_label("("), nested_tty::make_label(")")) + .to_grid_horizontal() + .flatten() + .into() + )); + +// nested_tty::editors::list::PTYListStyle::for_node( &mut node, ("(", ",", ")") ); + nested_tty::editors::list::PTYListController::for_node( &mut node, None, None ); + + node +} + +fn node_make_view( + node: NestedNode +) -> NestedNode { + if node.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, "") { + node_make_list_view( node ) +// node + } else if node.data.read().unwrap().get_type() == &Context::parse(&node.ctx, "") { + node_make_list_view( node ) + } else { + eprintln!("couldnt add view"); + node + } } #[async_std::main] async fn main() { - let app = TTYApplication::new( |ev| { /* event handler */ } ); - /* setup context & create Editor-Tree */ let ctx = Arc::new(RwLock::new(Context::default())); - let mut node = Context::make_node( + + /* Create a Char-Node with editor & view + */ + let mut n1 = Context::make_node( &ctx, // node type Context::parse(&ctx, "Char"), // depth SingletonBuffer::new(0).get_port(), - ) - .unwrap(); - - // set abstract data - node.data = ReprTree::from_char(&ctx, 'Λ'); + ).unwrap(); // add a display view to the node - node = node_make_char_view( node ); + n1 = node_make_view( n1 ); + + /* Create a -Node with editor & view + */ + let mut node2 = Context::make_node( + &ctx, + // node type + Context::parse(&ctx, ""), + // depth + SingletonBuffer::new(0).get_port(), + ).unwrap(); + + // add a display view to the node + node2 = node_make_view( node2 ); + + /* setup terminal + */ + let app = TTYApplication::new({ + /* event handler + */ + + let ctx = ctx.clone(); + let mut node = n1.clone(); + node.goto(TreeCursor::home()); + + + let node2 = node2.clone(); + move |ev| { + + if let Some(cmd) =node2.cmd.get() { + cmd.write().unwrap().send_cmd_obj( + ReprTree::new_leaf( + Context::parse(&ctx, "TerminalEvent"), + SingletonBuffer::new(ev.clone()).get_port().into() + ) + ); + } + + match ev { + TerminalEvent::Input(Event::Key(Key::Char(c))) => { + if let Some(cmd) = node.cmd.get() { + cmd.write().unwrap().send_cmd_obj( + ReprTree::from_char(&ctx, c) + ); + } + } + _ => {} + } + } + }); /* setup display view routed to `app.port` */ @@ -82,17 +169,21 @@ async fn main() { .offset(Vector2::new(5, 0)), ); + + let label = ctx.read().unwrap().type_term_to_str( &n1.get_type() ); compositor.write().unwrap() - .push(nested_tty::make_label("Char").offset(Vector2::new(0, 2))); + .push(nested_tty::make_label( &label ).offset(Vector2::new(0, 2))); compositor.write().unwrap() - .push(node.display_view().offset(Vector2::new(15, 2))); + .push(n1.display_view().offset(Vector2::new(15, 2))); + + + let label2 = ctx.read().unwrap().type_term_to_str( &node2.get_type() ); + compositor.write().unwrap() + .push(nested_tty::make_label( &label2 ).offset(Vector2::new(0, 3))); compositor.write().unwrap() - .push(nested_tty::make_label("").offset(Vector2::new(0, 3))); - - compositor.write().unwrap() - .push(nested_tty::make_label("---").offset(Vector2::new(15, 3))); + .push(node2.display_view().offset(Vector2::new(15, 3))); /* write the changes in the view of `term_port` to the terminal */ diff --git a/lib-nested-core/src/edit_tree/node.rs b/lib-nested-core/src/edit_tree/node.rs index b2a8fca..61cd789 100644 --- a/lib-nested-core/src/edit_tree/node.rs +++ b/lib-nested-core/src/edit_tree/node.rs @@ -73,7 +73,7 @@ pub struct NestedNode { pub data: Arc>, /// display view - pub view: Option< Arc> >, + pub display: Arc>, /// diagnostics pub diag: Option< OuterViewPort> >, @@ -103,16 +103,17 @@ pub struct NestedNode { impl NestedNode { pub fn new(ctx: Arc>, data: Arc>, depth: OuterViewPort>) -> Self { NestedNode { - ctx, data, - view: None, + display: Arc::new(RwLock::new(ReprTree::new(Context::parse(&ctx, "Display")))), diag: None, depth, editor: SingletonBuffer::new(None), spillbuf: Arc::new(RwLock::new(Vec::new())), cmd: SingletonBuffer::new(None), close_char: SingletonBuffer::new(None), - tree_nav: SingletonBuffer::new(None) + tree_nav: SingletonBuffer::new(None), + + ctx } } @@ -129,20 +130,7 @@ impl NestedNode { ), SingletonBuffer::new(0).get_port() ) - /* - .set_view(buf.get_port() - .map(|c| TerminalAtom::from(c)) - .to_index() - .map_key( - |_x| { - Point2::new(0, 0) - }, - |p| { - if *p == Point2::new(0,0) { Some(()) } else { None } - }) - ) - */ - .set_editor(Arc::new(RwLock::new(buf))) +// .set_editor(Arc::new(RwLock::new(buf))) } @@ -163,11 +151,6 @@ impl NestedNode { self } - pub fn set_view(mut self, view: Arc>) -> Self { - self.view = Some(view); - self - } - pub fn set_cmd(mut self, cmd: Arc>) -> Self { self.cmd.set(Some(cmd)); self @@ -189,10 +172,6 @@ impl NestedNode { self.diag.clone().unwrap_or(ViewPort::new().into_outer()) } - pub fn get_view(&self) -> Option< Arc> > { - self.view.clone() - } - pub fn get_data_port<'a, V: View + ?Sized + 'static>(&'a self, type_str: impl Iterator) -> Option> where V::Msg: Clone { let ctx = self.ctx.clone(); @@ -245,29 +224,6 @@ impl TreeType for NestedNode { } */ -/* TODO: remove that at some point -*/ -/* -impl TerminalEditor for NestedNode { - fn get_term_view(&self) -> OuterViewPort { - self.get_view() - } - - fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult { - let buf = SingletonBuffer::new(event.clone()); - - if let Some(cmd) = self.cmd.get() { - cmd.write().unwrap().send_cmd_obj( - ReprTree::new_leaf( - self.ctx.read().unwrap().type_term_from_str("TerminalEvent").unwrap(), - AnyOuterViewPort::from(buf.get_port()) - )); - } - - TerminalEditorResult::Continue - } -} -*/ impl TreeNav for NestedNode { fn get_cursor(&self) -> TreeCursor { if let Some(tn) = self.tree_nav.get() { diff --git a/lib-nested-core/src/editors/char/mod.rs b/lib-nested-core/src/editors/char/mod.rs index 3262f2b..8294576 100644 --- a/lib-nested-core/src/editors/char/mod.rs +++ b/lib-nested-core/src/editors/char/mod.rs @@ -82,13 +82,6 @@ impl CharEditor { ), depth ) - /* todo: move to lib-nested-term - .set_view(data - .get_port() - .map(move |c| TerminalAtom::from(if c == '\0' { ' ' } else { c })) - .to_grid() - ) - */ .set_cmd( editor.clone() ) .set_editor( editor.clone() ) } diff --git a/lib-nested-core/src/editors/list/editor.rs b/lib-nested-core/src/editors/list/editor.rs index 6776057..7006ac9 100644 --- a/lib-nested-core/src/editors/list/editor.rs +++ b/lib-nested-core/src/editors/list/editor.rs @@ -15,7 +15,7 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub struct ListEditor { - pub(super) cursor: SingletonBuffer, + pub cursor: SingletonBuffer, // todo: (?) remove RwLock<..> around NestedNode ?? pub data: VecBuffer< Arc> >, @@ -27,10 +27,10 @@ pub struct ListEditor { depth: OuterViewPort>, - pub(crate) ctx: Arc>, + pub ctx: Arc>, /// item type - pub(super) typ: TypeTerm, + pub typ: TypeTerm, } impl ListEditor { diff --git a/lib-nested-tty/src/edit-tree/color.rs b/lib-nested-tty/src/edit_tree/color.rs similarity index 96% rename from lib-nested-tty/src/edit-tree/color.rs rename to lib-nested-tty/src/edit_tree/color.rs index 532a7fe..42f36fc 100644 --- a/lib-nested-tty/src/edit-tree/color.rs +++ b/lib-nested-tty/src/edit_tree/color.rs @@ -1,5 +1,5 @@ use { - crate::terminal::TerminalStyle, + crate::atom::TerminalStyle, }; pub fn bg_style_from_depth(depth: usize) -> TerminalStyle { diff --git a/lib-nested-tty/src/edit-tree/cursor_widget.rs b/lib-nested-tty/src/edit_tree/cursor_widget.rs similarity index 100% rename from lib-nested-tty/src/edit-tree/cursor_widget.rs rename to lib-nested-tty/src/edit_tree/cursor_widget.rs diff --git a/lib-nested-tty/src/edit-tree/diagnostics.rs b/lib-nested-tty/src/edit_tree/diagnostics.rs similarity index 100% rename from lib-nested-tty/src/edit-tree/diagnostics.rs rename to lib-nested-tty/src/edit_tree/diagnostics.rs diff --git a/lib-nested-tty/src/edit_tree/mod.rs b/lib-nested-tty/src/edit_tree/mod.rs new file mode 100644 index 0000000..e5f34a1 --- /dev/null +++ b/lib-nested-tty/src/edit_tree/mod.rs @@ -0,0 +1,3 @@ + +pub mod color; + diff --git a/lib-nested-tty/src/editors/list.rs b/lib-nested-tty/src/editors/list.rs index 663079d..f0f87e7 100644 --- a/lib-nested-tty/src/editors/list.rs +++ b/lib-nested-tty/src/editors/list.rs @@ -4,23 +4,24 @@ use { projection::decorate_sequence::*, }, nested::{ - type_system::{Context, ReprTree}, + repr_tree::{Context, ReprTree}, editors::list::*, - tree::{TreeCursor, TreeNav, TreeNavResult}, - tree::NestedNode, - PtySegment + edit_tree::{TreeCursor, TreeNav, TreeNavResult, NestedNode}, }, crate::{ - TerminalEvent, TerminalView, make_label - } + DisplaySegment, + TerminalStyle, + TerminalEvent, TerminalView, make_label, + edit_tree::color::{bg_style_from_depth, fg_style_from_depth} + }, std::sync::{Arc, RwLock}, termion::event::{Event, Key} }; //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> -impl PtySegment for ListSegment { - fn pty_view(&self) -> OuterViewPort { +impl DisplaySegment for ListSegment { + fn display_view(&self) -> OuterViewPort { match self { ListSegment::InsertCursor => { make_label("|") @@ -32,7 +33,7 @@ impl PtySegment for ListSegment { ListSegment::Item{ editor, cur_dist } => { let e = editor.clone(); let cur_dist = *cur_dist; - editor.get_view().map_item(move |_pt, atom| { + editor.display_view().map_item(move |_pt, atom| { let c = e.get_cursor(); let cur_depth = c.tree_addr.len(); let select = @@ -71,7 +72,7 @@ impl PTYListStyle { editor.get_data_port() ); let se = seg_seq.read().unwrap(); - se.get_view().map(move |segment| segment.pty_view()) + se.get_view().map(move |segment| segment.display_view()) } pub fn pty_view(&self, editor: &ListEditor) -> OuterViewPort { @@ -83,7 +84,7 @@ impl PTYListStyle { seg_seq .get_view() - .map(move |segment| segment.pty_view()) + .map(move |segment| segment.display_view()) .separate(make_label(&self.style.1)) .wrap(make_label(&self.style.0), make_label(&self.style.2)) .to_grid_horizontal() @@ -91,12 +92,15 @@ impl PTYListStyle { } pub fn for_node(node: &mut NestedNode, style: (&str, &str, &str)) { - node.view = Some( - Self::new(style) - .pty_view( - &node.get_edit::().unwrap().read().unwrap() - ) - ); + node.display + .write().unwrap() + .insert_branch(ReprTree::new_leaf( + Context::parse(&node.ctx, "TerminalView"), + Self::new(style) + .pty_view( + &node.get_edit::().unwrap().read().unwrap() + ).into() + )); } } @@ -166,15 +170,22 @@ impl PTYListController { self.editor.read().unwrap().get_item() } - pub fn handle_term_event(&mut self, event: &TerminalEvent, _cmd_obj: Arc>) -> TreeNavResult { + pub fn handle_term_event(&mut self, event: &TerminalEvent, cmd_obj: Arc>) -> TreeNavResult { let mut e = self.editor.write().unwrap(); match event { TerminalEvent::Input(Event::Key(Key::Insert)) => { e.toggle_leaf_mode(); TreeNavResult::Continue } - _ => TreeNavResult::Continue - } + TerminalEvent::Input(Event::Key(Key::Char(c))) => { + let ctx = e.ctx.clone(); + drop(e); + self.handle_any_event( + ReprTree::from_char(&ctx, *c) + ) + } + _ => TreeNavResult::Continue + } } pub fn handle_meta_char(&mut self, c: char, child_close_char: Option) -> TreeNavResult { @@ -201,6 +212,7 @@ impl PTYListController { } pub fn handle_any_event(&mut self, cmd_obj: Arc>) -> TreeNavResult { + eprintln!("ANY EVENT"); let mut e = self.editor.write().unwrap(); let cur = e.cursor.get(); let ctx = e.ctx.clone(); @@ -255,7 +267,7 @@ impl PTYListController { } use r3vi::view::singleton::SingletonView; -use crate::commander::ObjCommander; +use nested::editors::ObjCommander; impl ObjCommander for PTYListController { fn send_cmd_obj(&mut self, cmd_obj: Arc>) -> TreeNavResult { diff --git a/lib-nested-tty/src/editors/mod.rs b/lib-nested-tty/src/editors/mod.rs new file mode 100644 index 0000000..dbaef38 --- /dev/null +++ b/lib-nested-tty/src/editors/mod.rs @@ -0,0 +1,3 @@ + +pub mod list; + diff --git a/lib-nested-tty/src/editors/mod.rsr b/lib-nested-tty/src/editors/mod.rsr deleted file mode 100644 index e69de29..0000000 diff --git a/lib-nested-tty/src/lib.rs b/lib-nested-tty/src/lib.rs index 687ceca..a95835e 100644 --- a/lib-nested-tty/src/lib.rs +++ b/lib-nested-tty/src/lib.rs @@ -11,7 +11,8 @@ pub mod ansi_parser; pub mod terminal; pub mod tty_application; -//pub mod edit_tree; +pub mod editors; +pub mod edit_tree; //pub mod widgets; // <<<<>>>><<>><><<>><<< * >>><<>><><<>><<<<>>>> \\ @@ -43,11 +44,24 @@ use std::sync::{Arc, RwLock}; impl DisplaySegment for nested::edit_tree::NestedNode { fn display_view(&self) -> OuterViewPort { - self.view.as_ref().unwrap() + if let Some( tv_repr ) = self.display .read().unwrap() - .descend( Context::parse(&self.ctx, "TerminalView") ).expect("terminal backend not supported by view") - .read().unwrap() - .get_port::().unwrap() + .descend( Context::parse(&self.ctx, "TerminalView") ) + { + if let Some(port) = + tv_repr + .read().unwrap() + .get_port::() { + port + } + + else { + make_label("no TerminalView port found") + } + } else { + make_label("TTY Display not supported") + .map_item(|_p,a| a.add_style_back(TerminalStyle::fg_color((220, 30, 30)))) + } } }