lib-nested/nested/src/tree_nav.rs

157 lines
3.6 KiB
Rust
Raw Normal View History

2022-06-19 23:13:21 +02:00
use {
crate::list::ListCursorMode,
cgmath::Vector2
};
2021-08-23 05:00:57 +02:00
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum TreeNavResult {
Continue,
2021-11-19 12:19:52 +01:00
Exit,
}
2022-05-08 23:30:49 +02:00
/*
impl From<TreeNavResult> for TerminalEditorResult {
fn from(v: TreeNavResult) -> TerminalEditorResult {
match v {
TreeNavResult::Continue => TerminalEditorResult::Continue,
TreeNavResult::Exit => TerminalEditorResult::Exit
}
}
}
2022-05-08 23:30:49 +02:00
*/
2021-08-23 05:00:57 +02:00
#[derive(Clone, Eq, PartialEq)]
pub struct TreeCursor {
pub leaf_mode: ListCursorMode,
2022-06-19 23:13:21 +02:00
pub tree_addr: Vec<isize>,
2021-08-23 05:00:57 +02:00
}
2022-05-08 23:30:49 +02:00
impl TreeCursor {
pub fn home() -> Self {
TreeCursor {
2022-06-19 23:13:21 +02:00
leaf_mode: ListCursorMode::Insert,
2022-05-08 23:30:49 +02:00
tree_addr: vec![0]
}
}
2022-06-19 23:13:21 +02:00
pub fn none() -> Self {
TreeCursor {
leaf_mode: ListCursorMode::Select,
2021-11-19 12:19:52 +01:00
tree_addr: vec![],
}
}
}
2022-06-19 23:13:21 +02:00
impl Default for TreeCursor {
fn default() -> Self {
TreeCursor::none()
}
}
pub trait TreeNav {
2022-06-19 23:13:21 +02:00
/* CORE
*/
fn get_cursor(&self) -> TreeCursor {
TreeCursor::default()
}
2022-06-19 23:13:21 +02:00
fn get_cursor_warp(&self) -> TreeCursor {
TreeCursor::default()
}
2022-06-19 23:13:21 +02:00
fn goby(&mut self, direction: Vector2<isize>) -> TreeNavResult {
TreeNavResult::Exit
}
2022-06-19 23:13:21 +02:00
fn goto(&mut self, _new_cursor: TreeCursor) -> TreeNavResult {
TreeNavResult::Exit
}
2022-06-19 23:13:21 +02:00
/* HULL
*/
fn set_addr(&mut self, addr: isize) -> TreeNavResult {
let mut c = self.get_cursor();
c.tree_addr[0] = addr;
self.goto(c)
}
2022-06-19 23:13:21 +02:00
fn set_leaf_mode(&mut self, new_leaf_mode: ListCursorMode) -> TreeNavResult {
let mut c = self.get_cursor();
c.leaf_mode = new_leaf_mode;
self.goto(c)
}
2021-08-16 00:10:25 +02:00
2022-06-19 23:13:21 +02:00
fn up(&mut self) -> TreeNavResult {
self.goby(Vector2::new(0, -1))
}
2021-08-16 00:10:25 +02:00
2022-06-19 23:13:21 +02:00
fn dn(&mut self) -> TreeNavResult {
self.goby(Vector2::new(0, 1))
}
fn pxev(&mut self) -> TreeNavResult {
self.goby(Vector2::new(-1, 0))
}
fn nexd(&mut self) -> TreeNavResult {
self.goby(Vector2::new(1, 0))
}
// TODO
fn qpxev(&mut self) -> TreeNavResult {
let mut c = self.get_cursor();
match c.tree_addr.len() {
0 => {
self.goto(TreeCursor::home())
},
depth => {
if c.tree_addr[depth-1] != 0 {
c.tree_addr[depth-1] = 0;
} else {
for i in (0..depth-1).rev() {
if c.tree_addr[i] == 0 {
c.tree_addr[i] = -1;
} else {
c.tree_addr[i] -=1;
break;
}
}
}
self.goto(c)
}
}
}
fn qnexd(&mut self) -> TreeNavResult {
let mut c = self.get_cursor_warp();
match c.tree_addr.len() {
0 => {
TreeNavResult::Exit
},
depth => {
if c.tree_addr[depth-1] != -1 {
c.tree_addr[depth-1] = -1;
} else {
for i in (0..depth-1).rev() {
if c.tree_addr[i] == -1 {
c.tree_addr[i] = 0;
} else {
c.tree_addr[i] += 1;
break;
}
}
}
self.goto(c)
}
}
2021-08-16 00:10:25 +02:00
}
}
2021-11-19 12:19:52 +01:00
use crate::terminal::{TerminalEditor};
2022-10-23 19:29:50 +02:00
use crate::diagnostics::{Diagnostics};
2022-10-23 19:29:50 +02:00
pub trait TerminalTreeEditor : TerminalEditor + TreeNav + Diagnostics + Send {}
2022-05-08 23:30:49 +02:00