fix some bugs and add help to demo

This commit is contained in:
Michael Sippel 2021-08-28 17:01:31 +02:00
parent 11eb30bd5b
commit a3d966df11
Signed by: senvas
GPG key ID: F96CF119C34B64A6
4 changed files with 173 additions and 73 deletions

View file

@ -115,11 +115,10 @@ where Item: 'static
} }
} }
); );
/*
if let Some(chunk) = self.chunks.get_mut(&chunk_idx) { if let Some(chunk) = self.chunks.get_mut(&chunk_idx) {
chunk.view = view; chunk.view = view;
} else { } else {
*/
self.chunks.insert( self.chunks.insert(
chunk_idx, chunk_idx,
Chunk { Chunk {
@ -128,9 +127,7 @@ where Item: 'static
view view
} }
); );
/*
} }
*/
chunk_port.0.update(); chunk_port.0.update();
self.update_all_offsets(); self.update_all_offsets();
@ -162,20 +159,24 @@ where Item: 'static
let mut col_widths = vec![0 as i16; (top_range.end.x) as usize]; let mut col_widths = vec![0 as i16; (top_range.end.x) as usize];
let mut row_heights = vec![0 as i16; (top_range.end.y) as usize]; let mut row_heights = vec![0 as i16; (top_range.end.y) as usize];
for chunk_idx in GridWindowIterator::from(self.top.range()) {
if let Some(chunk) = self.chunks.get_mut(&chunk_idx) {
let lim = chunk.view.range().end;
col_widths[chunk_idx.x as usize] = std::cmp::max(col_widths[chunk_idx.x as usize], lim.x);
row_heights[chunk_idx.y as usize] = std::cmp::max(row_heights[chunk_idx.y as usize], lim.y);
}
}
for chunk_idx in GridWindowIterator::from(self.top.range()) { for chunk_idx in GridWindowIterator::from(self.top.range()) {
if let Some(chunk) = self.chunks.get_mut(&chunk_idx) { if let Some(chunk) = self.chunks.get_mut(&chunk_idx) {
let old_offset = chunk.offset; let old_offset = chunk.offset;
let old_limit = chunk.limit; let old_limit = chunk.limit;
chunk.limit = chunk.view.range().end;
chunk.offset = Vector2::new( chunk.offset = Vector2::new(
(0 .. chunk_idx.x as usize).map(|x| col_widths[x]).sum(), (0 .. chunk_idx.x as usize).map(|x| col_widths[x]).sum(),
(0 .. chunk_idx.y as usize).map(|y| row_heights[y]).sum() (0 .. chunk_idx.y as usize).map(|y| row_heights[y]).sum()
); );
chunk.limit = chunk.view.range().end;
col_widths[chunk_idx.x as usize] = std::cmp::max(col_widths[chunk_idx.x as usize], chunk.limit.x);
row_heights[chunk_idx.y as usize] = std::cmp::max(row_heights[chunk_idx.y as usize], chunk.limit.y);
if old_offset != chunk.offset { if old_offset != chunk.offset {
dirty_idx.extend( dirty_idx.extend(
GridWindowIterator::from( GridWindowIterator::from(

View file

@ -137,6 +137,17 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
fn goto_end(&mut self) -> TreeNavResult { fn goto_end(&mut self) -> TreeNavResult {
let mut cur = self.cursor.get(); let mut cur = self.cursor.get();
let i = cur.idx.unwrap_or(0); let i = cur.idx.unwrap_or(0);
if self.data.len() == 0 && cur.idx.is_none() {
self.cursor.set(
ListCursor {
mode: ListCursorMode::Insert,
idx: Some(0)
}
);
return TreeNavResult::Continue;
}
if i < self.data.len() { if i < self.data.len() {
match cur.mode { match cur.mode {
ListCursorMode::Insert => { ListCursorMode::Insert => {
@ -150,6 +161,16 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
} }
} }
ListCursorMode::Select => { ListCursorMode::Select => {
if self.data.len() == 0 && cur.idx.is_none() {
self.cursor.set(
ListCursor {
mode: ListCursorMode::Insert,
idx: Some(0)
}
);
return TreeNavResult::Continue;
}
if i+1 < self.data.len() || cur.idx.is_none() { if i+1 < self.data.len() || cur.idx.is_none() {
cur.idx = Some(self.data.len()-1); cur.idx = Some(self.data.len()-1);
self.cursor.set(cur); self.cursor.set(cur);
@ -170,24 +191,25 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
} }
TreeNavResult::Exit => { TreeNavResult::Exit => {
drop(cur_edit); drop(cur_edit);
self.up(); self.up();
if let Some(i) = cur.idx { if i+1 < self.data.len() {
if i+1 < self.data.len() { self.set_mode(ListCursorMode::Select);
self.nexd(); self.nexd();
self.set_leaf_mode(cur_mode);
for x in 1 .. depth {
self.dn();
self.goto_home();
}
for x in 1 .. depth {
self.dn(); self.dn();
self.goto_end(); self.goto_home();
return TreeNavResult::Continue;
} }
self.set_leaf_mode(cur_mode);
self.dn();
self.goto_end();
return TreeNavResult::Continue;
} }
TreeNavResult::Exit TreeNavResult::Exit
} }
} }
@ -201,13 +223,23 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
fn goto_home(&mut self) -> TreeNavResult { fn goto_home(&mut self) -> TreeNavResult {
let cur = self.cursor.get(); let cur = self.cursor.get();
if self.data.len() == 0 && cur.idx.is_none() {
self.cursor.set(
ListCursor {
mode: ListCursorMode::Insert,
idx: Some(0)
}
);
return TreeNavResult::Continue;
}
match cur.mode { match cur.mode {
ListCursorMode::Insert | ListCursorMode::Insert |
ListCursorMode::Select => { ListCursorMode::Select => {
if cur.idx != Some(0) { if cur.idx != Some(0) {
self.cursor.set( self.cursor.set(
ListCursor { ListCursor {
mode: cur.mode, mode: if self.data.len() == 0 { ListCursorMode::Insert } else { cur.mode },
idx: Some(0) idx: Some(0)
} }
); );
@ -226,9 +258,10 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
match cur_edit.goto_home() { match cur_edit.goto_home() {
TreeNavResult::Exit => { TreeNavResult::Exit => {
drop(cur_edit); drop(cur_edit);
self.up();
if let Some(i) = cur.idx { if let Some(i) = cur.idx {
self.up();
if i > 0 { if i > 0 {
self.set_mode(ListCursorMode::Select); self.set_mode(ListCursorMode::Select);
self.pxev(); self.pxev();
@ -331,6 +364,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
self.up(); self.up();
if i > 0 { if i > 0 {
self.set_mode(ListCursorMode::Select);
self.pxev(); self.pxev();
for x in 1 .. depth { for x in 1 .. depth {
@ -341,7 +375,6 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
self.set_leaf_mode(cur_mode); self.set_leaf_mode(cur_mode);
self.dn(); self.dn();
self.goto_end(); self.goto_end();
TreeNavResult::Continue TreeNavResult::Continue
} else { } else {
TreeNavResult::Exit TreeNavResult::Exit
@ -391,17 +424,21 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static,
TreeNavResult::Exit => { TreeNavResult::Exit => {
drop(cur_edit); drop(cur_edit);
drop(ce); drop(ce);
self.up(); self.up();
if i+1 < self.data.len() { if i+1 < self.data.len() {
self.set_mode(ListCursorMode::Select);
self.nexd(); self.nexd();
for x in 0 .. depth { for x in 1 .. depth {
self.dn(); self.dn();
self.goto_home(); self.goto_home();
} }
self.set_leaf_mode(cur_mode); self.set_leaf_mode(cur_mode);
self.dn();
self.goto_home();
TreeNavResult::Continue TreeNavResult::Continue
} else { } else {
TreeNavResult::Exit TreeNavResult::Exit
@ -565,7 +602,7 @@ where ItemEditor: TerminalEditor + ?Sized + Send + Sync + 'static,
.map_item( .map_item(
|_pt, atom| |_pt, atom|
atom.add_style_back(TerminalStyle::fg_color((90,60,200))) atom.add_style_back(TerminalStyle::fg_color((90,60,200)))
.add_style_back(TerminalStyle::bg_color((0,0,0))) //.add_style_back(TerminalStyle::bg_color((0,0,0)))
.add_style_back(TerminalStyle::bold(true)) .add_style_back(TerminalStyle::bold(true))
), ),
ListEditorViewSegment::Select(sub_view) => ListEditorViewSegment::Select(sub_view) =>
@ -574,11 +611,11 @@ where ItemEditor: TerminalEditor + ?Sized + Send + Sync + 'static,
atom.add_style_front(TerminalStyle::bg_color((90,60,200))) atom.add_style_front(TerminalStyle::bg_color((90,60,200)))
), ),
ListEditorViewSegment::Modify(sub_view) => { ListEditorViewSegment::Modify(sub_view) => {
sub_view.map_item( sub_view.clone()/*.map_item(
|_pt, atom| |_pt, atom|
atom.add_style_back(TerminalStyle::bg_color((0,0,0))) atom//.add_style_back(TerminalStyle::bg_color((0,0,0)))
.add_style_back(TerminalStyle::bold(true)) //.add_style_back(TerminalStyle::bold(true))
) )*/
}, },
ListEditorViewSegment::View(sub_view) => ListEditorViewSegment::View(sub_view) =>
sub_view.clone() sub_view.clone()
@ -699,6 +736,7 @@ where ItemEditor: TerminalEditor + ?Sized + Send + Sync + 'static,
} }
cur.mode = mode; cur.mode = mode;
self.cursor.set(cur); self.cursor.set(cur);
} }
} }

View file

@ -5,7 +5,7 @@ use {
core::{ViewPort, OuterViewPort}, core::{ViewPort, OuterViewPort},
singleton::{SingletonView, SingletonBuffer}, singleton::{SingletonView, SingletonBuffer},
vec::VecBuffer, vec::VecBuffer,
terminal::{TerminalView, TerminalEvent, TerminalEditor, TerminalEditorResult}, terminal::{TerminalView, TerminalStyle, TerminalEvent, TerminalEditor, TerminalEditorResult},
tree_nav::{TreeNav, TreeNavResult} tree_nav::{TreeNav, TreeNavResult}
} }
}; };
@ -40,6 +40,10 @@ impl TerminalEditor for CharEditor {
} else { } else {
"".to_string() "".to_string()
}) })
.map_item(
|_idx, atom|
atom.add_style_back(TerminalStyle::fg_color((120, 200, 10)))
)
} }
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult { fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
@ -60,4 +64,30 @@ impl TerminalEditor for CharEditor {
} }
} }
/*
pub struct ArgListEditor {
}
impl TreeNav for ArgListEditor {
}
impl TerminalEditor for ArgListEditor {
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
}
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
match event {
TerminalEvent::Input(Event::Key(Key::Char(' '))) => {
// list.get_arg()
// split
}
_ => {
}
}
}
}
*/

View file

@ -89,45 +89,36 @@ write::
async_std::task::spawn( async_std::task::spawn(
async move { async move {
let table_port = ViewPort::<dyn nested::grid::GridView<Item = OuterViewPort<dyn TerminalView>>>::new();
let mut table_buf = nested::index::buffer::IndexBuffer::new(table_port.inner());
let magic = make_label("<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>") let magic = make_label("<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>")
.map_item( .map_item(
|pos, atom| |pos, atom|
atom.add_style_back( atom.add_style_back(
TerminalStyle::fg_color( TerminalStyle::fg_color(
(5, (5,
((80+(pos.x*30)%100) as u8), ((80+(pos.x*30)%100) as u8),
(55+(pos.x*15)%180) as u8) (55+(pos.x*15)%180) as u8)
)
) )
); )
);
{
compositor.write().unwrap().push(magic.offset(Vector2::new(40, 4)));
//compositor.write().unwrap().push(magic.offset(Vector2::new(40, 20)));
//let monstera_port = monstera::make_monstera();
//compositor.write().unwrap().push(monstera_port.clone());
//compositor.write().unwrap().push(monstera_port.offset(Vector2::new(83,0)));
}
let cur_size_port = ViewPort::new(); let cur_size_port = ViewPort::new();
let mut cur_size = nested::singleton::SingletonBuffer::new(Vector2::new(10, 10), cur_size_port.inner()); let mut cur_size = nested::singleton::SingletonBuffer::new(Vector2::new(10, 10), cur_size_port.inner());
let mut y = 5;
// TypeEditor // TypeEditor
let make_char_editor = || { let make_char_editor = || {
std::sync::Arc::new(std::sync::RwLock::new(DigitEditor::new(16))) std::sync::Arc::new(std::sync::RwLock::new(CharEditor::new()))
};
let make_subsub_editor = move || {
std::sync::Arc::new(std::sync::RwLock::new(ListEditor::new(make_char_editor.clone(), ListEditorStyle::String)))
}; };
let make_sub_editor = move || { let make_sub_editor = move || {
std::sync::Arc::new(std::sync::RwLock::new(ListEditor::new(make_char_editor.clone(), ListEditorStyle::Hex))) std::sync::Arc::new(std::sync::RwLock::new(ListEditor::new(make_subsub_editor.clone(), ListEditorStyle::HorizontalSexpr)))
}; };
let mut te = ListEditor::new(make_sub_editor.clone(), ListEditorStyle::Clist); let mut te = ListEditor::new(make_sub_editor.clone(), ListEditorStyle::VerticalSexpr);
te.goto( te.goto(
TreeCursor { TreeCursor {
@ -136,24 +127,63 @@ write::
} }
); );
compositor.write().unwrap().push(
te.get_term_view()
.offset(cgmath::Vector2::new(40,y))
);
y += 1;
let mut p = te.get_data_port().map(|sub_editor| sub_editor.read().unwrap().get_data_port()); let mut p = te.get_data_port().map(|sub_editor| sub_editor.read().unwrap().get_data_port());
let status_chars_port = ViewPort::new(); let status_chars_port = ViewPort::new();
let mut status_chars = VecBuffer::new(status_chars_port.inner()); let mut status_chars = VecBuffer::new(status_chars_port.inner());
compositor.write().unwrap().push( let help_port = ViewPort::<dyn nested::grid::GridView<Item = OuterViewPort<dyn TerminalView>>>::new();
status_chars_port.outer() let mut help_buf = nested::index::buffer::IndexBuffer::<Point2<i16>, OuterViewPort<dyn TerminalView>>::new(help_port.inner());
.to_sequence()
.to_grid_horizontal()
.offset(cgmath::Vector2::new(40, 2))
);
let table_style = TerminalStyle::fg_color((120, 100, 80));
let desc_style = TerminalStyle::italic(true);
help_buf.insert_iter(vec![
(Point2::new(0, 0), make_label("CTRL+{c,d,g}").map_item(|_idx, atom| atom.add_style_back(TerminalStyle::bold(true)))),
(Point2::new(1, 0), make_label(" | ").map_item(move |_idx, atom| atom.add_style_back(table_style))),
(Point2::new(2, 0), make_label("quit").map_item(move |_idx, atom| atom.add_style_back(desc_style))),
(Point2::new(0, 1), make_label("↞ ← ↑ ↓ → ↠").map_item(|_idx, atom| atom.add_style_back(TerminalStyle::bold(true)))),
(Point2::new(1, 1), make_label(" | ").map_item(move |_idx, atom| atom.add_style_back(table_style))),
(Point2::new(2, 1), make_label("move cursor").map_item(move |_idx, atom| atom.add_style_back(desc_style))),
(Point2::new(0, 3), make_label("<DEL> (Select)").map_item(|_idx, atom| atom.add_style_back(TerminalStyle::bold(true)))),
(Point2::new(1, 3), make_label(" | ").map_item(move |_idx, atom| atom.add_style_back(table_style))),
(Point2::new(2, 3), make_label("delete item at cursor position").map_item(move |_idx, atom| atom.add_style_back(desc_style))),
(Point2::new(0, 4), make_label("<DEL> (Insert)").map_item(|_idx, atom| atom.add_style_back(TerminalStyle::bold(true)))),
(Point2::new(1, 4), make_label(" | ").map_item(move |_idx, atom| atom.add_style_back(table_style))),
(Point2::new(2, 4), make_label("delete item right to cursor").map_item(move |_idx, atom| atom.add_style_back(desc_style))),
(Point2::new(0, 5), make_label("<BACKSPACE> (Insert)").map_item(|_idx, atom| atom.add_style_back(TerminalStyle::bold(true)))),
(Point2::new(1, 5), make_label(" | ").map_item(move |_idx, atom| atom.add_style_back(table_style))),
(Point2::new(2, 5), make_label("delete item left to cursor").map_item(move |_idx, atom| atom.add_style_back(desc_style))),
(Point2::new(0, 6), make_label("<TAB>").map_item(|_idx, atom| atom.add_style_back(TerminalStyle::bold(true)))),
(Point2::new(1, 6), make_label(" | ").map_item(move |_idx, atom| atom.add_style_back(table_style))),
(Point2::new(2, 6), make_label("toggle cursor mode (insert / select)").map_item(move |_idx, atom| atom.add_style_back(desc_style))),
]);
let help_head = make_label("─────────────────────┬─────────────────────").map_item(move |_idx, atom| atom.add_style_back(table_style));
table_buf.insert_iter(vec![
(Point2::new(0, 0), magic.clone()),
(Point2::new(0, 2), status_chars_port.outer().to_sequence().to_grid_horizontal()),
(Point2::new(0, 3), te.get_term_view()),
(Point2::new(0, 4), make_label(" ")),
(Point2::new(0, 5), help_head),
(Point2::new(0, 6), help_port.outer().flatten()),
(Point2::new(0, 7), magic.clone()),
]);
compositor.write().unwrap().push(monstera::make_monstera());
compositor.write().unwrap().push(table_port.outer().flatten().offset(Vector2::new(40, 2)));
/*
te.get_data_port()
.map(
|item_editor| item_editor.read().unwrap().get_data_port()
)
*/
loop { loop {
term_port.update(); term_port.update();
match term.next_event().await { match term.next_event().await {
@ -180,9 +210,10 @@ write::
} }
} }
TerminalEvent::Input(Event::Key(Key::Up)) => { te.up(); } TerminalEvent::Input(Event::Key(Key::Up)) => { te.up(); }
TerminalEvent::Input(Event::Key(Key::Down)) => { te.dn(); } TerminalEvent::Input(Event::Key(Key::Down)) => { te.dn(); te.goto_home(); }
TerminalEvent::Input(Event::Key(Key::Home)) => { TerminalEvent::Input(Event::Key(Key::Home)) => {
if te.goto_home() == TreeNavResult::Exit { if te.goto_home() == TreeNavResult::Exit {
te.goto_home(); te.goto_home();
} }
} }