From d15077aca077e2965d0b7f03018afb2285dc56e4 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Wed, 29 Nov 2023 02:22:44 +0100 Subject: [PATCH] add TreeNavCmd and keymap functions --- examples/tty-02-node/src/main.rs | 40 +++----- lib-nested-core/src/edit_tree/nav.rs | 14 +++ lib-nested-core/src/edit_tree/node.rs | 21 +++- lib-nested-core/src/editors/list/editor.rs | 2 - lib-nested-tty/src/edit_tree/keymap.rs | 109 +++++++++++++++++++++ lib-nested-tty/src/edit_tree/mod.rs | 1 + lib-nested-tty/src/editors/list.rs | 1 - 7 files changed, 155 insertions(+), 33 deletions(-) create mode 100644 lib-nested-tty/src/edit_tree/keymap.rs diff --git a/examples/tty-02-node/src/main.rs b/examples/tty-02-node/src/main.rs index eb3515e..4a027f5 100644 --- a/examples/tty-02-node/src/main.rs +++ b/examples/tty-02-node/src/main.rs @@ -9,6 +9,7 @@ use { nested::{ edit_tree::{NestedNode, TreeCursor, TreeNav}, repr_tree::{Context, ReprTree}, + editors::ObjCommander }, nested_tty::{ terminal::TermOutWriter, DisplaySegment, Terminal, TerminalAtom, TerminalCompositor, @@ -96,7 +97,7 @@ async fn main() { /* Create a Char-Node with editor & view */ - let mut n1 = Context::make_node( + let mut node1 = Context::make_node( &ctx, // node type Context::parse(&ctx, "Char"), @@ -105,7 +106,7 @@ async fn main() { ).unwrap(); // add a display view to the node - n1 = node_make_view( n1 ); + node1 = node_make_view( node1 ); /* Create a -Node with editor & view */ @@ -127,32 +128,13 @@ async fn main() { */ let ctx = ctx.clone(); - let mut node = n1.clone(); - node.goto(TreeCursor::home()); - - - let node2 = node2.clone(); + let mut node1 = node1.clone(); + let mut 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) - ); - } - } - _ => {} - } + let mut node1 = node1.clone(); + let mut node2 = node2.clone(); + node1.send_cmd_obj( ev.to_repr_tree(&ctx) ); + node2.send_cmd_obj( ev.to_repr_tree(&ctx) ); } }); @@ -170,12 +152,12 @@ async fn main() { ); - let label = ctx.read().unwrap().type_term_to_str( &n1.get_type() ); + let label = ctx.read().unwrap().type_term_to_str( &node1.get_type() ); compositor.write().unwrap() .push(nested_tty::make_label( &label ).offset(Vector2::new(0, 2))); compositor.write().unwrap() - .push(n1.display_view().offset(Vector2::new(15, 2))); + .push(node1.display_view().offset(Vector2::new(15, 2))); let label2 = ctx.read().unwrap().type_term_to_str( &node2.get_type() ); diff --git a/lib-nested-core/src/edit_tree/nav.rs b/lib-nested-core/src/edit_tree/nav.rs index 3dae406..f974c08 100644 --- a/lib-nested-core/src/edit_tree/nav.rs +++ b/lib-nested-core/src/edit_tree/nav.rs @@ -26,6 +26,20 @@ pub enum TreeNavResult { Continue, Exit } #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum TreeHeightOp { P, Q, Max } +#[derive(Clone, Copy, Debug)] +pub enum TreeNavCmd { + pxev, nexd, up, dn, + qpxev, qnexd, dup, qdn, + + dn_pxev, + up_nexd, + pxev_dn_qnexd +} + +impl TreeNavCmd { + +} + pub trait TreeNav { /* CORE */ diff --git a/lib-nested-core/src/edit_tree/node.rs b/lib-nested-core/src/edit_tree/node.rs index 61cd789..2458766 100644 --- a/lib-nested-core/src/edit_tree/node.rs +++ b/lib-nested-core/src/edit_tree/node.rs @@ -282,9 +282,28 @@ impl TreeNav for NestedNode { } } +use crate::edit_tree::nav::TreeNavCmd; + impl ObjCommander for NestedNode { fn send_cmd_obj(&mut self, cmd_obj: Arc>) -> TreeNavResult { - if let Some(cmd) = self.cmd.get() { + + if cmd_obj.read().unwrap().get_type() == &Context::parse(&self.ctx, "TreeNavCmd") { + if let Some(cmd) = cmd_obj.read().unwrap().get_view::>() { + match cmd.get() { + TreeNavCmd::pxev => self.pxev(), + TreeNavCmd::nexd => self.nexd(), + TreeNavCmd::qpxev => self.qpxev(), + TreeNavCmd::qnexd => self.qnexd(), + + TreeNavCmd::up => self.up(), + TreeNavCmd::dn => self.dn(), + + _ => TreeNavResult::Continue + } + } else { + TreeNavResult::Exit + } + } else if let Some(cmd) = self.cmd.get() { // todo: filter out tree-nav cmds and send them to tree_nav cmd.write().unwrap().send_cmd_obj(cmd_obj) } else { diff --git a/lib-nested-core/src/editors/list/editor.rs b/lib-nested-core/src/editors/list/editor.rs index 7006ac9..06b8bbb 100644 --- a/lib-nested-core/src/editors/list/editor.rs +++ b/lib-nested-core/src/editors/list/editor.rs @@ -230,8 +230,6 @@ impl ListEditor { /// insert a new element pub fn insert(&mut self, item: Arc>) { - eprintln!("list insert"); - item.read().unwrap().depth.0.set_view( self.depth.map(|d| d+1).get_view() ); diff --git a/lib-nested-tty/src/edit_tree/keymap.rs b/lib-nested-tty/src/edit_tree/keymap.rs new file mode 100644 index 0000000..54e4679 --- /dev/null +++ b/lib-nested-tty/src/edit_tree/keymap.rs @@ -0,0 +1,109 @@ +use { + termion::event::{Event, Key}, + r3vi::{ + buffer::singleton::* + }, + nested::{ + repr_tree::{Context, ReprTree}, + editors::list::ListCmd, + edit_tree::nav::TreeNavCmd + }, + crate::{ + TerminalEvent + }, + std::sync::{Arc, RwLock} +}; + +fn neo2_treenav_keymap( key: &Key ) -> Option { + match key { + Key::Ctrl(c) => { + match c { + + // left hand + 'l' => Some(TreeNavCmd::up), + 'i' => Some(TreeNavCmd::qnexd), + 'a' => Some(TreeNavCmd::dn), + 'e' => Some(TreeNavCmd::pxev), + + // right hand + 'n' => Some(TreeNavCmd::nexd), + 'r' => Some(TreeNavCmd::dn_pxev), + 't' => Some(TreeNavCmd::qnexd), + 'g' => Some(TreeNavCmd::up_nexd), + + _ => None + } + } + _ => None + } +} + +fn universal_treenav_keymap( key: &Key ) -> Option { + match key { + Key::Left => Some(TreeNavCmd::pxev), + Key::Right => Some(TreeNavCmd::nexd), + Key::Up => Some(TreeNavCmd::up), + Key::Down => Some(TreeNavCmd::dn), + Key::Home => Some(TreeNavCmd::qpxev), + Key::End => Some(TreeNavCmd::qnexd), + Key::PageUp => Some(TreeNavCmd::up_nexd), + Key::PageDown => Some(TreeNavCmd::pxev_dn_qnexd), + _ => None + } +} + +fn tty_list_keymap( key: &Key ) -> Option { + match key { +// Key::Char('\t') => Some( ListCmd::ToggleLeafMode ), + + Key::Backspace => Some( ListCmd::DeletePxev ), + Key::Delete => Some( ListCmd::DeleteNexd ), + + _ => None + } +} + +impl TerminalEvent { + pub fn to_repr_tree( &self, ctx: &Arc> ) -> Arc> { + match self { + TerminalEvent::Input(Event::Key(key)) => { + if let Some(tree_nav_cmd) = neo2_treenav_keymap(key) { + ReprTree::new_leaf( + Context::parse(&ctx, "TreeNavCmd"), + SingletonBuffer::new(tree_nav_cmd).get_port().into() + ) + } else if let Some(tree_nav_cmd) = universal_treenav_keymap(key) { + ReprTree::new_leaf( + Context::parse(&ctx, "TreeNavCmd"), + SingletonBuffer::new(tree_nav_cmd).get_port().into() + ) + } else { + if let Some(list_cmd) = tty_list_keymap(key) { + ReprTree::new_leaf( + Context::parse(&ctx, "ListCmd"), + SingletonBuffer::new(list_cmd).get_port().into() + ) + } else { + match key { + Key::Char(c) => { + ReprTree::from_char(&ctx, *c) + } + _ => { + ReprTree::new_leaf( + Context::parse(&ctx, "TerminalEvent"), + SingletonBuffer::new(self.clone()).get_port().into() + ) + } + } + } + } + } + _ => { + ReprTree::new_leaf( + Context::parse(&ctx, "TerminalEvent"), + SingletonBuffer::new(self.clone()).get_port().into() + ) + } + } + } +} diff --git a/lib-nested-tty/src/edit_tree/mod.rs b/lib-nested-tty/src/edit_tree/mod.rs index e5f34a1..fdf1de4 100644 --- a/lib-nested-tty/src/edit_tree/mod.rs +++ b/lib-nested-tty/src/edit_tree/mod.rs @@ -1,3 +1,4 @@ pub mod color; +pub mod keymap; diff --git a/lib-nested-tty/src/editors/list.rs b/lib-nested-tty/src/editors/list.rs index f0f87e7..0fff8ec 100644 --- a/lib-nested-tty/src/editors/list.rs +++ b/lib-nested-tty/src/editors/list.rs @@ -212,7 +212,6 @@ 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();