From bf8a949cddec852b1ac361e126aa0ae0c95afa95 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 19 Nov 2021 12:04:37 +0100 Subject: [PATCH] move plot to separate file --- nested/src/list/editor.rs | 2 +- shell/src/main.rs | 120 +------------------------------- shell/src/plot.rs | 139 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 118 deletions(-) create mode 100644 shell/src/plot.rs diff --git a/nested/src/list/editor.rs b/nested/src/list/editor.rs index 261efe9..95070ce 100644 --- a/nested/src/list/editor.rs +++ b/nested/src/list/editor.rs @@ -38,7 +38,7 @@ where ItemEditor: TerminalEditor + ?Sized + Send + Sync + 'static, FnMakeItemEditor: Fn() -> Arc> { cursor: SingletonBuffer, - data: VecBuffer>>, + pub data: VecBuffer>>, cursor_port: ViewPort>, data_port: ViewPort>>>>, diff --git a/shell/src/main.rs b/shell/src/main.rs index 93e0a5d..8bae99f 100644 --- a/shell/src/main.rs +++ b/shell/src/main.rs @@ -6,6 +6,8 @@ mod process; mod pty; mod ascii_box; +mod plot; + use{ std::sync::{Arc, RwLock}, cgmath::{Point2, Vector2}, @@ -47,122 +49,6 @@ use{ } }; -struct TestView {} - -impl View for TestView { - type Msg = IndexArea>; -} - -impl IndexView> for TestView { - type Item = TerminalAtom; - - fn get(&self, pt: &Point2) -> Option { - Some(TerminalAtom::from('.')) - } - - fn area(&self) -> IndexArea> { - IndexArea::Full - } -} - -struct Plot { - limit: usize, - data: Arc>, - cast: Arc>>, - proj_helper: ProjectionHelper<(), Self> -} - -impl View for Plot { - type Msg = IndexArea>; -} - -impl IndexView> for Plot { - type Item = TerminalAtom; - - fn get(&self, pt: &Point2) -> Option { - if pt.y >= 0 { - if let Some(cur_val) = self.data.get(&(pt.x as usize)) { - if cur_val <= self.limit { - if pt.y == (self.limit - cur_val) as i16 { - return Some(TerminalAtom::from( - if cur_val < 4 { 'o' } - else if cur_val < 8 { 'O' } - else { '*' } - )); - } - } - if pt.x > 0 { - if let Some(prev_val) = self.data.get(&((pt.x-1) as usize)) { - if - ( - pt.y > (self.limit - prev_val) as i16 && - pt.y < (self.limit - cur_val) as i16 - ) - || - ( - pt.y < (self.limit - prev_val) as i16 && - pt.y > (self.limit - cur_val) as i16 - ) - { - return Some(TerminalAtom::from('.')); - } - } - } - } - } - None - } - - fn area(&self) -> IndexArea> { - IndexArea::Range( - Point2::new(0,0) - ..= Point2::new( - self.data.len().unwrap_or(0) as i16, - self.limit as i16 - ) - ) - } -} - -impl Plot { - pub fn new( - data_port: OuterViewPort>, - out_port: InnerViewPort - ) -> Arc> { - let mut proj_helper = ProjectionHelper::new(out_port.0.update_hooks.clone()); - let proj = Arc::new(RwLock::new( - Plot { - data: proj_helper.new_sequence_arg( - (), - data_port, - |s: &mut Self, idx| { - let val = s.data.get(idx).unwrap_or(0); - - if val > s.limit { - s.limit = val; - s.cast.notify(&s.area()); - } else { - s.cast.notify(&IndexArea::Range( - Point2::new(*idx as i16, 0) - ..= Point2::new(*idx as i16, s.limit as i16) - )); - } - } - ), - - limit: 0, - cast: out_port.get_broadcast(), - proj_helper - } - )); - - proj.write().unwrap().proj_helper.set_proj(&proj); - out_port.set_view(Some(proj.clone())); - - proj - } -} - #[async_std::main] async fn main() { let term_port = ViewPort::new(); @@ -235,7 +121,7 @@ async fn main() { }); let plot_port = ViewPort::new(); - let plot = Plot::new(plist_vec_port.outer().to_sequence(), plot_port.inner()); + let plot = crate::plot::Plot::new(plist_vec_port.outer().to_sequence(), plot_port.inner()); table_buf.insert_iter(vec![ (Point2::new(0, 0), magic.clone()), diff --git a/shell/src/plot.rs b/shell/src/plot.rs new file mode 100644 index 0000000..acaefc3 --- /dev/null +++ b/shell/src/plot.rs @@ -0,0 +1,139 @@ +use{ + std::sync::{Arc, RwLock}, + cgmath::{Point2, Vector2}, + termion::event::{Event, Key}, + nested::{ + core::{ + View, + ViewPort, + InnerViewPort, + OuterViewPort, + Observer, + ObserverExt, + ObserverBroadcast, + context::{ReprTree, Object, MorphismType, MorphismMode, Context}, + port::{UpdateTask}}, + index::{IndexView, IndexArea}, + grid::{GridWindowIterator}, + sequence::{SequenceView, SequenceViewExt}, + vec::{VecBuffer}, + integer::{RadixProjection, DigitEditor, PosIntEditor}, + terminal::{ + Terminal, + TerminalStyle, + TerminalAtom, + TerminalCompositor, + TerminalEvent, + make_label, + TerminalView, + TerminalEditor, + TerminalEditorResult + }, + string_editor::{StringEditor}, + tree_nav::{TreeNav, TreeNavResult, TreeCursor, TerminalTreeEditor}, + list::{SExprView, ListCursorMode, ListEditor, ListEditorStyle}, + projection::ProjectionHelper + }, + crate::{ + process::ProcessLauncher + } +}; + +pub struct Plot { + limit: usize, + data: Arc>, + cast: Arc>>, + proj_helper: ProjectionHelper<(), Self> +} + +impl View for Plot { + type Msg = IndexArea>; +} + +impl IndexView> for Plot { + type Item = TerminalAtom; + + fn get(&self, pt: &Point2) -> Option { + if pt.y >= 0 { + if let Some(cur_val) = self.data.get(&(pt.x as usize)) { + if cur_val <= self.limit { + if pt.y == (self.limit - cur_val) as i16 { + return Some(TerminalAtom::from( + if cur_val < 4 { 'o' } + else if cur_val < 8 { 'O' } + else { '*' } + )); + } + } + if pt.x > 0 { + if let Some(prev_val) = self.data.get(&((pt.x-1) as usize)) { + if + ( + pt.y > (self.limit - prev_val) as i16 && + pt.y < (self.limit - cur_val) as i16 + ) + || + ( + pt.y < (self.limit - prev_val) as i16 && + pt.y > (self.limit - cur_val) as i16 + ) + { + return Some(TerminalAtom::from('.')); + } + } + } + } + } + None + } + + fn area(&self) -> IndexArea> { + IndexArea::Range( + Point2::new(0,0) + ..= Point2::new( + self.data.len().unwrap_or(0) as i16, + self.limit as i16 + ) + ) + } +} + +impl Plot { + pub fn new( + data_port: OuterViewPort>, + out_port: InnerViewPort + ) -> Arc> { + let mut proj_helper = ProjectionHelper::new(out_port.0.update_hooks.clone()); + let proj = Arc::new(RwLock::new( + Plot { + data: proj_helper.new_sequence_arg( + (), + data_port, + |s: &mut Self, idx| { + let val = s.data.get(idx).unwrap_or(0); + + if val > s.limit { + s.limit = val; + s.cast.notify(&s.area()); + } else { + s.cast.notify(&IndexArea::Range( + Point2::new(*idx as i16, 0) + ..= Point2::new(*idx as i16, s.limit as i16) + )); + } + } + ), + + limit: 0, + cast: out_port.get_broadcast(), + proj_helper + } + )); + + proj.write().unwrap().proj_helper.set_proj(&proj); + out_port.set_view(Some(proj.clone())); + + proj + } +} +