From 0a8c77772c77f769df326f02022ce976892239c3 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 13 Nov 2022 15:26:25 +0100 Subject: [PATCH] replace TerminalTreeEditor with Nested trait --- nested/src/char_editor.rs | 7 ++- nested/src/core/context.rs | 12 ++-- nested/src/core/port.rs | 1 + nested/src/integer/editor.rs | 9 +-- nested/src/lib.rs | 15 ++++- nested/src/list/editor.rs | 9 +-- nested/src/list/nav.rs | 7 ++- nested/src/list/pty_editor.rs | 19 +++--- nested/src/list/segment.rs | 14 ++--- nested/src/make_editor.rs | 78 ++++++++++++++++++------- nested/src/product/editor.rs | 10 ++-- nested/src/product/nav.rs | 7 ++- nested/src/product/segment.rs | 6 +- nested/src/sum/editor.rs | 17 ++++-- nested/src/tree/cursor.rs | 35 +++++++++++ nested/src/tree/mod.rs | 18 ++++++ nested/src/{tree_nav.rs => tree/nav.rs} | 38 ++---------- shell/src/command.rs | 66 +++++++++++++-------- shell/src/main.rs | 5 +- shell/src/process.rs | 9 +-- 20 files changed, 244 insertions(+), 138 deletions(-) create mode 100644 nested/src/tree/cursor.rs create mode 100644 nested/src/tree/mod.rs rename nested/src/{tree_nav.rs => tree/nav.rs} (81%) diff --git a/nested/src/char_editor.rs b/nested/src/char_editor.rs index 225c640..bb63295 100644 --- a/nested/src/char_editor.rs +++ b/nested/src/char_editor.rs @@ -8,8 +8,9 @@ use { TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, }, - tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult}, - diagnostics::Diagnostics + tree::{TreeCursor, TreeNav, TreeNavResult}, + diagnostics::Diagnostics, + Nested }, std::sync::Arc, std::sync::RwLock, @@ -69,6 +70,6 @@ impl TerminalEditor for CharEditor { } } -impl TerminalTreeEditor for CharEditor {} +impl Nested for CharEditor {} diff --git a/nested/src/core/context.rs b/nested/src/core/context.rs index a783512..ec82346 100644 --- a/nested/src/core/context.rs +++ b/nested/src/core/context.rs @@ -4,9 +4,7 @@ use { type_term::{TypeDict, TypeTerm, TypeID}, AnyOuterViewPort, OuterViewPort, View, }, - tree_nav::{ - TerminalTreeEditor - } + Nested }, std::{ collections::HashMap, @@ -244,7 +242,7 @@ pub struct Context { objects: HashMap, /// editors - editor_ctors: HashMap Option>> + Send + Sync>>, + editor_ctors: HashMap Option>> + Send + Sync>>, /// morphisms default_constructors: HashMap Object + Send + Sync>>, @@ -281,7 +279,7 @@ impl Context { self.type_dict.type_term_to_str(&t) } - pub fn add_editor_ctor(&mut self, tn: &str, mk_editor: Box Option>> + Send + Sync>) { + pub fn add_editor_ctor(&mut self, tn: &str, mk_editor: Box Option>> + Send + Sync>) { if let Some(tid) = self.type_dict.get_typeid(&tn.into()) { self.editor_ctors.insert(tid, mk_editor); } else { @@ -289,7 +287,7 @@ impl Context { } } - pub fn make_editor(&self, type_term: TypeTerm) -> Option>> { + pub fn make_editor(&self, type_term: TypeTerm) -> Option>> { if let TypeTerm::Type{ id, args } = type_term.clone() { let mk_editor = self.editor_ctors.get(&id)?; mk_editor(self, type_term) @@ -297,7 +295,7 @@ impl Context { None } } - + pub fn add_morphism( &mut self, morph_type: MorphismType, diff --git a/nested/src/core/port.rs b/nested/src/core/port.rs index 0cba29d..b20ec50 100644 --- a/nested/src/core/port.rs +++ b/nested/src/core/port.rs @@ -330,3 +330,4 @@ where } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + diff --git a/nested/src/integer/editor.rs b/nested/src/integer/editor.rs index d4dd244..003b845 100644 --- a/nested/src/integer/editor.rs +++ b/nested/src/integer/editor.rs @@ -10,8 +10,9 @@ use { TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, make_label }, - tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult}, - diagnostics::{Diagnostics, Message} + tree::{TreeCursor, TreeNav, TreeNavResult}, + diagnostics::{Diagnostics, Message}, + Nested }, std::sync::Arc, std::sync::RwLock, @@ -95,7 +96,7 @@ impl TerminalEditor for DigitEditor { } } -impl TerminalTreeEditor for DigitEditor {} +impl Nested for DigitEditor {} impl Diagnostics for DigitEditor { fn get_msg_port(&self) -> OuterViewPort> { @@ -205,5 +206,5 @@ impl TerminalEditor for PosIntEditor { } } -impl TerminalTreeEditor for PosIntEditor {} +impl Nested for PosIntEditor {} diff --git a/nested/src/lib.rs b/nested/src/lib.rs index 83d3384..842e9d6 100644 --- a/nested/src/lib.rs +++ b/nested/src/lib.rs @@ -20,7 +20,7 @@ pub mod vec; pub mod product; pub mod sum; pub mod list; -pub mod tree_nav; +pub mod tree; pub mod diagnostics; // high-level types @@ -35,3 +35,16 @@ pub mod terminal; pub fn magic_header() { eprintln!("<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>"); } + +use crate::terminal::{TerminalEditor}; +use crate::diagnostics::{Diagnostics}; +use crate::tree::{TreeNav, TreeType}; + +pub trait Nested + : TerminalEditor + + TreeNav +// + TreeType + + Diagnostics + + Send +{} + diff --git a/nested/src/list/editor.rs b/nested/src/list/editor.rs index 78d9c94..799116e 100644 --- a/nested/src/list/editor.rs +++ b/nested/src/list/editor.rs @@ -12,7 +12,8 @@ use { make_label, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, }, - tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult}, + Nested, + tree::{TreeCursor, TreeNav, TreeNavResult}, vec::VecBuffer, color::{bg_style_from_depth, fg_style_from_depth} }, @@ -23,7 +24,7 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub struct ListEditor -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { pub(super) cursor: SingletonBuffer, pub(super) data: VecBuffer>>, @@ -34,7 +35,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static } impl ListEditor -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { pub fn new(make_item_editor: impl Fn() -> Arc> + Send + Sync + 'static, depth: usize) -> Self { ListEditor { @@ -58,7 +59,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static ); segment_view_port.into_outer().map(move |segment| segment.pty_view()) } - + pub fn get_data_port(&self) -> OuterViewPort>>> { self.data.get_port().to_sequence() } diff --git a/nested/src/list/nav.rs b/nested/src/list/nav.rs index 9728d2f..bcc9c8b 100644 --- a/nested/src/list/nav.rs +++ b/nested/src/list/nav.rs @@ -12,9 +12,10 @@ use { make_label, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, }, - tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult}, + tree::{TreeCursor, TreeNav, TreeNavResult}, vec::VecBuffer, - color::{bg_style_from_depth, fg_style_from_depth} + color::{bg_style_from_depth, fg_style_from_depth}, + Nested }, std::sync::{Arc, RwLock}, termion::event::{Event, Key}, @@ -22,7 +23,7 @@ use { }; impl TreeNav for ListEditor -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { fn get_cursor_warp(&self) -> TreeCursor { let cur = self.cursor.get(); diff --git a/nested/src/list/pty_editor.rs b/nested/src/list/pty_editor.rs index 8f402fc..8235b36 100644 --- a/nested/src/list/pty_editor.rs +++ b/nested/src/list/pty_editor.rs @@ -12,10 +12,11 @@ use { make_label, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, }, - tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult}, + tree::{TreeCursor, TreeNav, TreeNavResult}, diagnostics::{Diagnostics}, vec::VecBuffer, - color::{bg_style_from_depth, fg_style_from_depth} + color::{bg_style_from_depth, fg_style_from_depth}, + Nested }, std::sync::{Arc, RwLock}, termion::event::{Event, Key}, @@ -23,7 +24,7 @@ use { }; pub struct PTYListEditor -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { pub editor: ListEditor, @@ -36,7 +37,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static } impl PTYListEditor -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { pub fn new( make_item_editor: impl Fn() -> Arc> + Send + Sync + 'static, @@ -92,7 +93,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static } impl TerminalEditor for PTYListEditor -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { fn get_term_view(&self) -> OuterViewPort { self.editor @@ -229,7 +230,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static } impl TreeNav for PTYListEditor -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { fn get_cursor_warp(&self) -> TreeCursor { self.editor.get_cursor_warp() @@ -249,7 +250,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static } impl Diagnostics for PTYListEditor -where ItemEditor: TerminalTreeEditor + Diagnostics + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + Diagnostics + ?Sized + Send + Sync + 'static { fn get_msg_port(&self) -> OuterViewPort> { self.editor @@ -273,8 +274,8 @@ where ItemEditor: TerminalTreeEditor + Diagnostics + ?Sized + Send + Sync + 'sta } } -impl TerminalTreeEditor for PTYListEditor -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +impl Nested for PTYListEditor +where ItemEditor: Nested + ?Sized + Send + Sync + 'static {} use crate::{ diff --git a/nested/src/list/segment.rs b/nested/src/list/segment.rs index 6e125e8..0aa2819 100644 --- a/nested/src/list/segment.rs +++ b/nested/src/list/segment.rs @@ -6,7 +6,7 @@ use { sequence::SequenceView, singleton::SingletonView, terminal::{TerminalView, TerminalStyle, make_label}, - tree_nav::TerminalTreeEditor, + Nested, color::{bg_style_from_depth, fg_style_from_depth} }, std::sync::Arc, @@ -14,7 +14,7 @@ use { }; pub enum ListSegment -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { InsertCursor, Item { @@ -29,7 +29,7 @@ pub trait PTYSegment { } impl PTYSegment for ListSegment -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { fn pty_view(&self) -> OuterViewPort { match self { @@ -64,7 +64,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static } pub struct ListSegmentSequence -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { data: Arc>>>, cursor: Arc>, @@ -77,13 +77,13 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static } impl View for ListSegmentSequence -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { type Msg = usize; } impl SequenceView for ListSegmentSequence -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { type Item = ListSegment; @@ -136,7 +136,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static } impl ListSegmentSequence -where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static +where ItemEditor: Nested + ?Sized + Send + Sync + 'static { pub fn new( cursor_port: OuterViewPort>, diff --git a/nested/src/make_editor.rs b/nested/src/make_editor.rs index 929b7d8..7f3e4ce 100644 --- a/nested/src/make_editor.rs +++ b/nested/src/make_editor.rs @@ -1,26 +1,60 @@ use { crate::{ - core::{TypeLadder, Context}, - terminal::{TerminalView}, - tree_nav::{TerminalTreeEditor}, + core::{TypeLadder, Context, OuterViewPort}, + terminal::{TerminalView, TerminalEditor, TerminalEvent, TerminalEditorResult, make_label}, + tree::{TreeNav}, integer::PosIntEditor, list::{ListEditor, PTYListEditor}, sequence::{decorator::{SeqDecorStyle}}, product::editor::ProductEditor, - char_editor::CharEditor + char_editor::CharEditor, + diagnostics::Diagnostics, + Nested }, cgmath::{Vector2, Point2}, std::sync::{Arc, RwLock}, }; -pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> Arc> { +enum RhsNode { + Sum ( + Arc >> + ), + Product ( + Arc >> + ), + String( + Arc >> + ) +} + +impl TreeNav for RhsNode {} + +impl TerminalEditor for RhsNode { + fn get_term_view(&self) -> OuterViewPort { + make_label("todo") + } + + fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult { + TerminalEditorResult::Continue + } +} + +impl Diagnostics for RhsNode {} +impl Nested for RhsNode {} + +struct GrammarRuleEditor { + lhs: Arc>>, + rhs: Arc>> +} + +pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> Arc> { let c = ctx.read().unwrap(); if t[0] == c.type_term_from_str("( PosInt 16 BigEndian )").unwrap() { - Arc::new(RwLock::new(PosIntEditor::new(16))) as Arc> + Arc::new(RwLock::new(PosIntEditor::new(16))) as Arc> } else if t[0] == c.type_term_from_str("( PosInt 10 BigEndian )").unwrap() { - Arc::new(RwLock::new(PosIntEditor::new(10))) as Arc> + Arc::new(RwLock::new(PosIntEditor::new(10))) as Arc> } else if t[0] == c.type_term_from_str("( String )").unwrap() { Arc::new(RwLock::new( @@ -64,7 +98,7 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A '"', depth ) - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( List Symbol )").unwrap() { Arc::new(RwLock::new( PTYListEditor::new( @@ -83,7 +117,7 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A ' ', depth ) - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( List Char )").unwrap() { Arc::new(RwLock::new( @@ -95,7 +129,7 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A '\n', depth+1 ) - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( List ℕ )").unwrap() { Arc::new(RwLock::new( @@ -107,7 +141,7 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A ',', depth ) - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( Path )").unwrap() { Arc::new(RwLock::new(PTYListEditor::new( @@ -126,7 +160,7 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A SeqDecorStyle::Path, '/', depth - ))) as Arc> + ))) as Arc> } else if t[0] == c.type_term_from_str("( List Path )").unwrap() { Arc::new(RwLock::new( @@ -146,11 +180,11 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A ',', depth ) - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( List RGB )").unwrap() { Arc::new(RwLock::new( - PTYListEditor::::new( + PTYListEditor::::new( { let d = depth+1; let ctx = ctx.clone(); @@ -162,7 +196,7 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A ',', depth ) - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( RGB )").unwrap() { Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) @@ -174,7 +208,7 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A .with_t(Point2::new(1, 3), "b: ") .with_n(Point2::new(2, 3), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() ] ) .with_t(Point2::new(0, 4), "} ") - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( Vec3i )").unwrap() { Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) @@ -186,11 +220,11 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A .with_t(Point2::new(1, 3), "z: ") .with_n(Point2::new(2, 3), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] ) .with_t(Point2::new(0, 4), "}") - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( Json )").unwrap() { Arc::new(RwLock::new( - PTYListEditor::::new( + PTYListEditor::::new( Box::new({ let ctx = ctx.clone(); move || { @@ -198,18 +232,18 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A .with_n(Point2::new(0, 0), vec![ ctx.read().unwrap().type_term_from_str("( String )").unwrap() ] ) .with_t(Point2::new(1, 0), ": ") .with_n(Point2::new(2, 0), vec![ ctx.read().unwrap().type_term_from_str("( Json )").unwrap() ] ) - )) as Arc> + )) as Arc> } }), SeqDecorStyle::VerticalSexpr, '\n', depth ) - )) as Arc> + )) as Arc> } else if t[0] == c.type_term_from_str("( List Term )").unwrap() { Arc::new(RwLock::new( - PTYListEditor::::new( + PTYListEditor::::new( Box::new({ let ctx = ctx.clone(); move || { @@ -220,7 +254,7 @@ pub fn make_editor(ctx: Arc>, t: &TypeLadder, depth: usize) -> A '\n', depth ) - )) as Arc> + )) as Arc> } else { // else: term Arc::new(RwLock::new( diff --git a/nested/src/product/editor.rs b/nested/src/product/editor.rs index 3c36be5..4ab8357 100644 --- a/nested/src/product/editor.rs +++ b/nested/src/product/editor.rs @@ -12,9 +12,11 @@ use { sequence::{SequenceView}, make_editor::make_editor, - tree_nav::{TreeNav, TerminalTreeEditor, TreeNavResult}, + tree::{TreeNav, TreeNavResult}, diagnostics::{Diagnostics, Message}, - terminal::{TerminalStyle} + terminal::{TerminalStyle}, + + Nested }, cgmath::{Vector2, Point2}, std::sync::{Arc, RwLock}, @@ -111,7 +113,7 @@ impl ProductEditor { Some(self.get_editor_segment_mut(self.cursor?)) } - pub fn get_editor(&self, idx: isize) -> Option>> { + pub fn get_editor(&self, idx: isize) -> Option>> { if let ProductEditorSegment::N{ t: _, editor, ed_depth: _, cur_depth: _, cur_dist: _ } = self.get_editor_segment(idx) { editor } else { @@ -119,7 +121,7 @@ impl ProductEditor { } } - pub fn get_cur_editor(&self) -> Option>> { + pub fn get_cur_editor(&self) -> Option>> { self.get_editor(self.cursor?) } diff --git a/nested/src/product/nav.rs b/nested/src/product/nav.rs index 8ff801f..0a74921 100644 --- a/nested/src/product/nav.rs +++ b/nested/src/product/nav.rs @@ -1,9 +1,10 @@ use { crate::{ list::ListCursorMode, - tree_nav::{TreeNav, TreeNavResult, TreeCursor, TerminalTreeEditor}, + tree::{TreeNav, TreeNavResult, TreeCursor}, product::{segment::ProductEditorSegment, ProductEditor}, - make_editor::{make_editor} + make_editor::{make_editor}, + Nested }, cgmath::{Point2, Vector2}, std::{sync::{Arc, RwLock}, ops::{Deref, DerefMut}}, @@ -248,5 +249,5 @@ impl TreeNav for ProductEditor { } } -impl TerminalTreeEditor for ProductEditor {} +impl Nested for ProductEditor {} diff --git a/nested/src/product/segment.rs b/nested/src/product/segment.rs index 2ceefc6..5ecd717 100644 --- a/nested/src/product/segment.rs +++ b/nested/src/product/segment.rs @@ -6,8 +6,8 @@ use { make_label }, list::{ListCursorMode}, - tree_nav::{TerminalTreeEditor}, - color::{bg_style_from_depth, fg_style_from_depth} + color::{bg_style_from_depth, fg_style_from_depth}, + Nested }, std::{sync::{Arc, RwLock}, ops::{Deref, DerefMut}}, termion::event::{Event, Key}, @@ -18,7 +18,7 @@ pub enum ProductEditorSegment { T( String, usize ), N { t: TypeLadder, - editor: Option>>, + editor: Option>>, ed_depth: usize, cur_depth: usize, cur_dist: isize diff --git a/nested/src/sum/editor.rs b/nested/src/sum/editor.rs index bc590dc..d7f4bf1 100644 --- a/nested/src/sum/editor.rs +++ b/nested/src/sum/editor.rs @@ -12,9 +12,10 @@ use { sequence::{SequenceView}, make_editor::make_editor, - tree_nav::{TreeNav, TreeCursor, TerminalTreeEditor, TreeNavResult}, + tree::{TreeNav, TreeCursor, TreeNavResult}, diagnostics::{Diagnostics, Message}, - terminal::{TerminalStyle} + terminal::{TerminalStyle}, + Nested }, cgmath::{Vector2, Point2}, std::sync::{Arc, RwLock}, @@ -24,7 +25,7 @@ use { pub struct SumEditor { cur: usize, - editors: Vec< Arc> >, + editors: Vec< Arc> >, port: ViewPort< dyn TerminalView >, diag_port: OuterViewPort< dyn SequenceView > @@ -32,7 +33,7 @@ pub struct SumEditor { impl SumEditor { pub fn new( - editors: Vec< Arc> > + editors: Vec< Arc> > ) -> Self { let port = ViewPort::new(); let mut diag_buf = VecBuffer::new(); @@ -44,11 +45,12 @@ impl SumEditor { diag_port: diag_buf.get_port().to_sequence() } } - + pub fn select(&mut self, idx: usize) { self.cur = idx; let tv = self.editors[ self.cur ].read().unwrap().get_term_view(); tv.add_observer( self.port.get_cast() ); + self.port.update_hooks.write().unwrap().clear(); self.port.add_update_hook( Arc::new(tv.0.clone()) ); self.port.set_view( Some(tv.get_view_arc()) ); } @@ -81,6 +83,9 @@ impl TerminalEditor for SumEditor { match event { TerminalEvent::Input( termion::event::Event::Key(Key::Ctrl('x')) ) => { self.select( (self.cur + 1) % self.editors.len() ); + if self.editors[ self.cur ].read().unwrap().get_cursor().tree_addr.len() == 0 { + self.dn(); + } TerminalEditorResult::Continue }, event => { @@ -96,6 +101,6 @@ impl Diagnostics for SumEditor { } } -impl TerminalTreeEditor for SumEditor {} +impl Nested for SumEditor {} diff --git a/nested/src/tree/cursor.rs b/nested/src/tree/cursor.rs new file mode 100644 index 0000000..4827e96 --- /dev/null +++ b/nested/src/tree/cursor.rs @@ -0,0 +1,35 @@ +use { + crate::list::ListCursorMode, + cgmath::Vector2 +}; + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +#[derive(Clone, Eq, PartialEq)] +pub struct TreeCursor { + pub leaf_mode: ListCursorMode, + pub tree_addr: Vec, +} + +impl TreeCursor { + pub fn home() -> Self { + TreeCursor { + leaf_mode: ListCursorMode::Insert, + tree_addr: vec![0] + } + } + + pub fn none() -> Self { + TreeCursor { + leaf_mode: ListCursorMode::Select, + tree_addr: vec![], + } + } +} + +impl Default for TreeCursor { + fn default() -> Self { + TreeCursor::none() + } +} + diff --git a/nested/src/tree/mod.rs b/nested/src/tree/mod.rs new file mode 100644 index 0000000..d41e2fb --- /dev/null +++ b/nested/src/tree/mod.rs @@ -0,0 +1,18 @@ +pub mod cursor; +pub mod nav; +pub mod typeinfo; + +pub struct TreeAddr(Vec); + +impl From> for TreeAddr { + fn from(v: Vec) -> TreeAddr { + TreeAddr(v) + } +} + +pub use { + cursor::TreeCursor, + nav::{TreeNav, TreeNavResult}, + typeinfo::TreeType +}; + diff --git a/nested/src/tree_nav.rs b/nested/src/tree/nav.rs similarity index 81% rename from nested/src/tree_nav.rs rename to nested/src/tree/nav.rs index 0c18c6a..c2f23cc 100644 --- a/nested/src/tree_nav.rs +++ b/nested/src/tree/nav.rs @@ -1,5 +1,6 @@ use { crate::list::ListCursorMode, + crate::tree::TreeCursor, cgmath::Vector2 }; @@ -20,34 +21,6 @@ impl From for TerminalEditorResult { } */ -#[derive(Clone, Eq, PartialEq)] -pub struct TreeCursor { - pub leaf_mode: ListCursorMode, - pub tree_addr: Vec, -} - -impl TreeCursor { - pub fn home() -> Self { - TreeCursor { - leaf_mode: ListCursorMode::Insert, - tree_addr: vec![0] - } - } - - pub fn none() -> Self { - TreeCursor { - leaf_mode: ListCursorMode::Select, - tree_addr: vec![], - } - } -} - -impl Default for TreeCursor { - fn default() -> Self { - TreeCursor::none() - } -} - pub trait TreeNav { /* CORE */ @@ -59,6 +32,10 @@ pub trait TreeNav { TreeCursor::default() } + fn get_max_depth(&self) -> usize { + 0 + } + fn goby(&mut self, direction: Vector2) -> TreeNavResult { TreeNavResult::Exit } @@ -149,8 +126,3 @@ pub trait TreeNav { } } -use crate::terminal::{TerminalEditor}; -use crate::diagnostics::{Diagnostics}; - -pub trait TerminalTreeEditor : TerminalEditor + TreeNav + Diagnostics + Send {} - diff --git a/shell/src/command.rs b/shell/src/command.rs index 2076770..91a8717 100644 --- a/shell/src/command.rs +++ b/shell/src/command.rs @@ -16,30 +16,32 @@ use { terminal::{ TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, make_label }, - tree_nav::{TreeCursor, TreeNav, TreeNavResult, TerminalTreeEditor}, + tree::{TreeCursor, TreeNav, TreeNavResult}, diagnostics::{Diagnostics}, make_editor::make_editor, - product::ProductEditor + product::ProductEditor, + sum::SumEditor, + Nested } }; trait Action { - fn make_editor(&self, ctx: Arc>) -> Arc>; + fn make_editor(&self, ctx: Arc>) -> Arc>; } pub struct ActCd {} impl Action for ActCd { - fn make_editor(&self, ctx: Arc>) -> Arc> { + fn make_editor(&self, ctx: Arc>) -> Arc> { let depth = 1; Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) .with_n(Point2::new(0, 0), vec![ ctx.read().unwrap().type_term_from_str("( Path )").unwrap() ] ) - )) as Arc> + )) as Arc> } } pub struct ActLs {} impl Action for ActLs { - fn make_editor(&self, ctx: Arc>) -> Arc> { + fn make_editor(&self, ctx: Arc>) -> Arc> { let depth = 1; Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) .with_t(Point2::new(1, 0), " Files") @@ -47,24 +49,39 @@ impl Action for ActLs { .with_t(Point2::new(1, 1), " Options") .with_n(Point2::new(0, 1), vec![ ctx.read().unwrap().type_term_from_str("( List String )").unwrap() ] ) - )) as Arc> + )) as Arc> } } pub struct ActEcho {} impl Action for ActEcho { - fn make_editor(&self, ctx: Arc>) -> Arc> { + fn make_editor(&self, ctx: Arc>) -> Arc> { let depth = 1; - Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) + + let a = Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) .with_n(Point2::new(0, 0), vec![ ctx.read().unwrap().type_term_from_str("( String )").unwrap() ] ) - )) as Arc> + )) as Arc>; + + let b = Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) + .with_n(Point2::new(0, 0), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() ] ) + + )) as Arc>; + + let mut x = Arc::new(RwLock::new( SumEditor::new( + vec![ + a, b + ] + ) )); + + x.write().unwrap().select(0); + x } } pub struct ActCp {} impl Action for ActCp { - fn make_editor(&self, ctx: Arc>) -> Arc> { + fn make_editor(&self, ctx: Arc>) -> Arc> { let depth = 1; Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) .with_t(Point2::new(1, 1), " Source") @@ -73,13 +90,13 @@ impl Action for ActCp { .with_n(Point2::new(0, 2), vec![ ctx.read().unwrap().type_term_from_str("( Path )").unwrap() ] ) .with_t(Point2::new(1, 3), " Options") .with_n(Point2::new(0, 3), vec![ ctx.read().unwrap().type_term_from_str("( List Symbol )").unwrap() ] ) - )) as Arc> + )) as Arc> } } pub struct ActNum {} impl Action for ActNum { - fn make_editor(&self, ctx: Arc>) -> Arc> { + fn make_editor(&self, ctx: Arc>) -> Arc> { let depth = 1; Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) .with_t(Point2::new(1, 1), " Value") @@ -87,7 +104,7 @@ impl Action for ActNum { .with_t(Point2::new(1, 2), " Radix") .with_n(Point2::new(0, 2), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] ) - )) as Arc> + )) as Arc> // Arc::new(RwLock::new(nested::integer::PosIntEditor::new(10))) } @@ -95,7 +112,7 @@ impl Action for ActNum { pub struct ActColor {} impl Action for ActColor { - fn make_editor(&self, ctx: Arc>) -> Arc> { + fn make_editor(&self, ctx: Arc>) -> Arc> { let depth = 1; Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) .with_t(Point2::new(1, 1), " RGB") @@ -104,19 +121,19 @@ impl Action for ActColor { .with_n(Point2::new(0, 2), vec![ ctx.read().unwrap().type_term_from_str("( RGB )").unwrap() ] ) .with_t(Point2::new(1, 3), " HSL") .with_n(Point2::new(0, 3), vec![ ctx.read().unwrap().type_term_from_str("( RGB )").unwrap() ] ) - )) as Arc> + )) as Arc> } } pub struct ActLet {} impl Action for ActLet { - fn make_editor(&self, ctx: Arc>) -> Arc> { + fn make_editor(&self, ctx: Arc>) -> Arc> { let depth = 1; Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone()) .with_n(Point2::new(0, 0), vec![ ctx.read().unwrap().type_term_from_str("( Symbol )").unwrap() ] ) .with_t(Point2::new(1, 0), " := ") .with_n(Point2::new(2, 0), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] ) - )) as Arc> + )) as Arc> } } @@ -127,7 +144,7 @@ pub struct Commander { valid: Arc>, confirmed: bool, symbol_editor: PTYListEditor, - cmd_editor: Option>>, + cmd_editor: Option>>, view_elements: VecBuffer>, out_port: OuterViewPort, @@ -209,9 +226,12 @@ impl TerminalEditor for Commander { if let (Some(cmd_editor), true) = (self.cmd_editor.as_ref(), self.confirmed) { match event { TerminalEvent::Input(Event::Key(Key::Char('\n'))) => { - if cmd_editor.write().unwrap().nexd() == TreeNavResult::Exit { + let mut c = cmd_editor.write().unwrap(); + if let TerminalEditorResult::Exit = c.handle_terminal_event(&TerminalEvent::Input(Event::Key(Key::Char('\n')))) { + // run - cmd_editor.write().unwrap().goto(TreeCursor::none()); + c.goto(TreeCursor::none()); + c.up(); TerminalEditorResult::Exit } else { @@ -245,7 +265,7 @@ impl TerminalEditor for Commander { self.m_buf.clear(); self.m_buf.push(b.get_port().to_sequence()); } - + TerminalEditorResult::Continue } @@ -323,5 +343,5 @@ impl TreeNav for Commander { } } -impl TerminalTreeEditor for Commander {} +impl Nested for Commander {} diff --git a/shell/src/main.rs b/shell/src/main.rs index 75dc2a1..c380af0 100644 --- a/shell/src/main.rs +++ b/shell/src/main.rs @@ -22,10 +22,11 @@ use { make_label, Terminal, TerminalAtom, TerminalCompositor, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, }, - tree_nav::{TreeNav, TerminalTreeEditor, TreeCursor, TreeNavResult}, + tree::{TreeNav, TreeCursor, TreeNavResult}, vec::VecBuffer, integer::{PosIntEditor}, - diagnostics::{Diagnostics} + diagnostics::{Diagnostics}, + Nested }, std::sync::{Arc, RwLock}, termion::event::{Event, Key}, diff --git a/shell/src/process.rs b/shell/src/process.rs index 8fa456c..a25f1fc 100644 --- a/shell/src/process.rs +++ b/shell/src/process.rs @@ -10,8 +10,9 @@ use { TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, }, - tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult}, - diagnostics::Diagnostics + tree::{TreeCursor, TreeNav, TreeNavResult}, + diagnostics::Diagnostics, + Nested }, std::sync::Arc, std::sync::RwLock, @@ -77,7 +78,7 @@ impl TreeNav for ProcessArg { impl Diagnostics for ProcessArg { } -impl TerminalTreeEditor for ProcessArg {} +impl Nested for ProcessArg {} pub struct ProcessLauncher { cmd_editor: PTYListEditor, @@ -272,5 +273,5 @@ impl TreeNav for ProcessLauncher { impl Diagnostics for ProcessLauncher { } -impl TerminalTreeEditor for ProcessLauncher {} +impl Nested for ProcessLauncher {}