lib-nested/nested/src/tree/nav.rs

201 lines
5.3 KiB
Rust
Raw Normal View History

2022-06-19 23:13:21 +02:00
use {
2023-02-13 18:39:45 +01:00
r3vi::{
view::{
OuterViewPort,
singleton::*,
sequence::*,
},
buffer::{
singleton::SingletonBuffer,
vec::VecBuffer
},
projection::{
decorate_sequence::*,
}
},
crate::{
2023-02-13 18:39:45 +01:00
editors::list::ListCursorMode,
tree::TreeCursor,
terminal::{TerminalView, TerminalProjections, make_label}
},
cgmath::Vector2,
2022-06-19 23:13:21 +02:00
};
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
*/
pub trait TreeNav {
2022-06-19 23:13:21 +02:00
/* CORE
*/
fn get_cursor(&self) -> TreeCursor {
TreeCursor::default()
}
fn get_addr_view(&self) -> OuterViewPort<dyn SequenceView<Item = isize>> {
VecBuffer::<isize>::new().get_port().to_sequence()
}
fn get_mode_view(&self) -> OuterViewPort<dyn SingletonView<Item = ListCursorMode>> {
SingletonBuffer::new(ListCursorMode::Select).get_port()
}
2022-06-19 23:13:21 +02:00
fn get_cursor_warp(&self) -> TreeCursor {
TreeCursor::default()
}
fn get_max_depth(&self) -> usize {
0
}
2022-12-18 01:39:01 +01: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
2023-02-13 18:39:45 +01:00
fn get_leaf_mode(&mut self) -> ListCursorMode {
self.get_cursor().leaf_mode
}
fn toggle_leaf_mode(&mut self) -> TreeNavResult {
let old_mode = self.get_leaf_mode();
self.set_leaf_mode(
match old_mode {
ListCursorMode::Insert => ListCursorMode::Select,
ListCursorMode::Select => ListCursorMode::Insert
}
);
TreeNavResult::Continue
}
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;
}
}
}
2022-06-19 23:13:21 +02:00
self.goto(c)
}
}
2021-08-16 00:10:25 +02:00
}
fn get_cursor_widget(&self) -> OuterViewPort<dyn TerminalView> {
VecBuffer::with_data(
vec![
make_label("@").with_fg_color((150, 80,230)),
self.get_addr_view()
.map(|i|
make_label(&format!("{}", i)).with_fg_color((0, 100, 20)))
.separate(make_label(".").with_fg_color((150, 80,230)))
.to_grid_horizontal()
.flatten(),
make_label(":").with_fg_color((150, 80,230)),
self.get_mode_view()
.map(|mode| {
make_label(
match mode {
ListCursorMode::Insert => "INSERT",
ListCursorMode::Select => "SELECT"
})
.with_fg_color((200, 200, 20))
})
.to_grid()
.flatten(),
make_label(":").with_fg_color((150, 80,230))
]
).get_port()
.to_sequence()
.to_grid_horizontal()
.flatten()
}
}