add TreeNavCmd and keymap functions

This commit is contained in:
Michael Sippel 2023-11-29 02:22:44 +01:00
parent 25d8acdb72
commit d15077aca0
Signed by: senvas
GPG key ID: F96CF119C34B64A6
7 changed files with 155 additions and 33 deletions

View file

@ -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 <List Char>-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() );

View file

@ -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
*/

View file

@ -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<RwLock<ReprTree>>) -> 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::<dyn SingletonView<Item = TreeNavCmd>>() {
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 {

View file

@ -230,8 +230,6 @@ impl ListEditor {
/// insert a new element
pub fn insert(&mut self, item: Arc<RwLock<NestedNode>>) {
eprintln!("list insert");
item.read().unwrap().depth.0.set_view(
self.depth.map(|d| d+1).get_view()
);

View file

@ -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<TreeNavCmd> {
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<TreeNavCmd> {
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<ListCmd> {
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<RwLock<Context>> ) -> Arc<RwLock<ReprTree>> {
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()
)
}
}
}
}

View file

@ -1,3 +1,4 @@
pub mod color;
pub mod keymap;

View file

@ -212,7 +212,6 @@ impl PTYListController {
}
pub fn handle_any_event(&mut self, cmd_obj: Arc<RwLock<ReprTree>>) -> TreeNavResult {
eprintln!("ANY EVENT");
let mut e = self.editor.write().unwrap();
let cur = e.cursor.get();
let ctx = e.ctx.clone();