From bf8a949cddec852b1ac361e126aa0ae0c95afa95 Mon Sep 17 00:00:00 2001 From: Michael Sippel <micha@fragmental.art> 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<RwLock<ItemEditor>> { cursor: SingletonBuffer<ListCursor>, - data: VecBuffer<Arc<RwLock<ItemEditor>>>, + pub data: VecBuffer<Arc<RwLock<ItemEditor>>>, cursor_port: ViewPort<dyn SingletonView<Item = ListCursor>>, data_port: ViewPort<RwLock<Vec<Arc<RwLock<ItemEditor>>>>>, 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<Point2<i16>>; -} - -impl IndexView<Point2<i16>> for TestView { - type Item = TerminalAtom; - - fn get(&self, pt: &Point2<i16>) -> Option<TerminalAtom> { - Some(TerminalAtom::from('.')) - } - - fn area(&self) -> IndexArea<Point2<i16>> { - IndexArea::Full - } -} - -struct Plot { - limit: usize, - data: Arc<dyn SequenceView<Item = usize>>, - cast: Arc<RwLock<ObserverBroadcast<dyn TerminalView>>>, - proj_helper: ProjectionHelper<(), Self> -} - -impl View for Plot { - type Msg = IndexArea<Point2<i16>>; -} - -impl IndexView<Point2<i16>> for Plot { - type Item = TerminalAtom; - - fn get(&self, pt: &Point2<i16>) -> Option<TerminalAtom> { - 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<Point2<i16>> { - 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<dyn SequenceView<Item = usize>>, - out_port: InnerViewPort<dyn TerminalView> - ) -> Arc<RwLock<Self>> { - 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<dyn SequenceView<Item = usize>>, + cast: Arc<RwLock<ObserverBroadcast<dyn TerminalView>>>, + proj_helper: ProjectionHelper<(), Self> +} + +impl View for Plot { + type Msg = IndexArea<Point2<i16>>; +} + +impl IndexView<Point2<i16>> for Plot { + type Item = TerminalAtom; + + fn get(&self, pt: &Point2<i16>) -> Option<TerminalAtom> { + 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<Point2<i16>> { + 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<dyn SequenceView<Item = usize>>, + out_port: InnerViewPort<dyn TerminalView> + ) -> Arc<RwLock<Self>> { + 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 + } +} +