From 96e47c3cd52a02834213e37bf485646f27a352e3 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sat, 5 Nov 2022 22:33:46 +0100 Subject: [PATCH] sum editor --- nested/src/lib.rs | 1 + nested/src/sum/editor.rs | 101 +++++++++++++++++++++++++++++++++++++++ nested/src/sum/mod.rs | 4 ++ 3 files changed, 106 insertions(+) create mode 100644 nested/src/sum/editor.rs create mode 100644 nested/src/sum/mod.rs diff --git a/nested/src/lib.rs b/nested/src/lib.rs index 604bb3a..83d3384 100644 --- a/nested/src/lib.rs +++ b/nested/src/lib.rs @@ -18,6 +18,7 @@ pub mod vec; // editors pub mod product; +pub mod sum; pub mod list; pub mod tree_nav; pub mod diagnostics; diff --git a/nested/src/sum/editor.rs b/nested/src/sum/editor.rs new file mode 100644 index 0000000..bc590dc --- /dev/null +++ b/nested/src/sum/editor.rs @@ -0,0 +1,101 @@ +use { + crate::{ + core::{ViewPort, OuterViewPort, TypeLadder, Context}, + terminal::{ + TerminalEditor, TerminalEditorResult, + TerminalEvent, TerminalView + }, + vec::{VecBuffer, MutableVecAccess}, + index::{buffer::{IndexBuffer, MutableIndexAccess}, IndexView}, + list::ListCursorMode, + product::{segment::ProductEditorSegment}, + sequence::{SequenceView}, + make_editor::make_editor, + + tree_nav::{TreeNav, TreeCursor, TerminalTreeEditor, TreeNavResult}, + diagnostics::{Diagnostics, Message}, + terminal::{TerminalStyle} + }, + cgmath::{Vector2, Point2}, + std::sync::{Arc, RwLock}, + termion::event::{Event, Key}, + std::ops::{Deref, DerefMut} +}; + +pub struct SumEditor { + cur: usize, + editors: Vec< Arc> >, + + port: ViewPort< dyn TerminalView >, + diag_port: OuterViewPort< dyn SequenceView > +} + +impl SumEditor { + pub fn new( + editors: Vec< Arc> > + ) -> Self { + let port = ViewPort::new(); + let mut diag_buf = VecBuffer::new(); + + SumEditor { + cur: 0, + editors, + port, + 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.add_update_hook( Arc::new(tv.0.clone()) ); + self.port.set_view( Some(tv.get_view_arc()) ); + } +} + +impl TreeNav for SumEditor { + fn get_cursor(&self) -> TreeCursor { + self.editors[ self.cur ].write().unwrap().get_cursor() + } + + fn get_cursor_warp(&self) -> TreeCursor { + self.editors[ self.cur ].write().unwrap().get_cursor_warp() + } + + fn goby(&mut self, direction: Vector2) -> TreeNavResult { + self.editors[ self.cur ].write().unwrap().goby( direction ) + } + + fn goto(&mut self, new_cursor: TreeCursor) -> TreeNavResult { + self.editors[ self.cur ].write().unwrap().goto( new_cursor ) + } +} + +impl TerminalEditor for SumEditor { + fn get_term_view(&self) -> OuterViewPort { + self.port.outer() + } + + fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult { + match event { + TerminalEvent::Input( termion::event::Event::Key(Key::Ctrl('x')) ) => { + self.select( (self.cur + 1) % self.editors.len() ); + TerminalEditorResult::Continue + }, + event => { + self.editors[ self.cur ].write().unwrap().handle_terminal_event( event ) + } + } + } +} + +impl Diagnostics for SumEditor { + fn get_msg_port(&self) -> OuterViewPort> { + self.diag_port.clone() + } +} + +impl TerminalTreeEditor for SumEditor {} + + diff --git a/nested/src/sum/mod.rs b/nested/src/sum/mod.rs new file mode 100644 index 0000000..7b4d700 --- /dev/null +++ b/nested/src/sum/mod.rs @@ -0,0 +1,4 @@ + +pub mod editor; +pub use editor::SumEditor; +