From 87ed4ef1cab43882098b0129b85b441641d26132 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 17 Jun 2022 15:48:13 +0200 Subject: [PATCH] buffers: store inner viewport instead of cast --- nested/src/core/port.rs | 7 +++- nested/src/index/buffer.rs | 16 +++++--- nested/src/integer/add.rs | 2 +- nested/src/integer/editor.rs | 65 +++++++++--------------------- nested/src/integer/radix.rs | 2 +- nested/src/singleton/buffer.rs | 18 ++++++--- nested/src/terminal/ansi_parser.rs | 8 ++-- nested/src/vec/buffer.rs | 36 ++++++++++------- 8 files changed, 75 insertions(+), 79 deletions(-) diff --git a/nested/src/core/port.rs b/nested/src/core/port.rs index 5c666da..5d03f0d 100644 --- a/nested/src/core/port.rs +++ b/nested/src/core/port.rs @@ -9,7 +9,7 @@ pub trait UpdateTask: Send + Sync { fn update(&self); } -/*\ + /*\ <<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> View Port <<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> @@ -44,12 +44,17 @@ where self.cast.write().unwrap().reset(view); } + pub fn get_cast(&self) -> Arc>> { + self.cast.clone() + } + pub fn add_observer(&self, observer: Arc>>) { self.update(); self.cast .write() .unwrap() .add_observer(Arc::downgrade(&observer)); + observer .write() .unwrap() diff --git a/nested/src/index/buffer.rs b/nested/src/index/buffer.rs index 98f2777..bf86073 100644 --- a/nested/src/index/buffer.rs +++ b/nested/src/index/buffer.rs @@ -1,6 +1,6 @@ use { crate::{ - core::{InnerViewPort, Observer, ObserverBroadcast, View}, + core::{InnerViewPort, ViewPort, Observer, ObserverBroadcast, View}, index::{IndexArea, IndexView}, }, std::sync::RwLock, @@ -42,7 +42,7 @@ where Item: Clone + Send + Sync + 'static, { data: Arc>>, - cast: Arc>>>, + port: InnerViewPort>, } impl IndexBuffer @@ -50,19 +50,23 @@ where Key: Clone + Hash + Eq + Send + Sync + 'static, Item: Clone + Send + Sync + 'static, { - pub fn new(port: InnerViewPort>) -> Self { + pub fn with_port(port: InnerViewPort>) -> Self { let data = Arc::new(RwLock::new(HashMap::::new())); port.set_view(Some(Arc::new(IndexBufferView(data.clone())))); IndexBuffer { data, - cast: port.get_broadcast(), + port } } + pub fn new() -> Self { + IndexBuffer::with_port(ViewPort::new().into_inner()) + } + pub fn insert(&mut self, key: Key, item: Item) { self.data.write().unwrap().insert(key.clone(), item); - self.cast.notify(&IndexArea::Set(vec![key])); + self.port.notify(&IndexArea::Set(vec![key])); } pub fn insert_iter(&mut self, iter: T) @@ -76,6 +80,6 @@ where pub fn remove(&mut self, key: Key) { self.data.write().unwrap().remove(&key); - self.cast.notify(&IndexArea::Set(vec![key])); + self.port.notify(&IndexArea::Set(vec![key])); } } diff --git a/nested/src/integer/add.rs b/nested/src/integer/add.rs index 0f8d1a1..888234a 100644 --- a/nested/src/integer/add.rs +++ b/nested/src/integer/add.rs @@ -51,7 +51,7 @@ impl Add { radix, a: proj_helper.new_sequence_arg(0, a, |s: &mut Self, _digit_idx| s.update()), b: proj_helper.new_sequence_arg(1, b, |s: &mut Self, _digit_idx| s.update()), - c: VecBuffer::new(c), + c: VecBuffer::with_port(c), _proj_helper: proj_helper, })); add diff --git a/nested/src/integer/editor.rs b/nested/src/integer/editor.rs index 4f65b84..1c1fd17 100644 --- a/nested/src/integer/editor.rs +++ b/nested/src/integer/editor.rs @@ -1,8 +1,8 @@ use { crate::{ core::{OuterViewPort, ViewPort}, - list::{sexpr::ListDecoration, ListEditor}, - sequence::{SequenceView, SequenceViewExt}, + list::{PTYListEditor}, + sequence::{SequenceView, SequenceViewExt, decorator::{PTYSeqDecorate, SeqDecorStyle}}, singleton::{SingletonBuffer, SingletonView}, terminal::{ TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, @@ -13,29 +13,27 @@ use { std::sync::Arc, std::sync::RwLock, termion::event::{Event, Key}, + cgmath::Vector2 }; //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub struct DigitEditor { radix: u32, - data: SingletonBuffer>, - data_port: ViewPort>>, + data: SingletonBuffer> } impl DigitEditor { pub fn new(radix: u32) -> Self { - let data_port = ViewPort::new(); DigitEditor { radix, - data: SingletonBuffer::new(None, data_port.inner()), - data_port, + data: SingletonBuffer::new(None), } } pub fn get_data_port(&self) -> OuterViewPort>> { let radix = self.radix; - self.data_port.outer().map(move |c| c?.to_digit(radix)) + self.data.get_port().map(move |c| c?.to_digit(radix)) } } @@ -43,8 +41,8 @@ impl TreeNav for DigitEditor {} impl TerminalEditor for DigitEditor { fn get_term_view(&self) -> OuterViewPort { let radix = self.radix; - self.data_port - .outer() + self.data + .get_port() .map(move |c| { TerminalAtom::new( c.unwrap_or('?'), @@ -81,24 +79,24 @@ impl TerminalTreeEditor for DigitEditor {} pub struct PosIntEditor { radix: u32, - digits_editor: - ListEditor Arc> + Send + Sync + 'static>>, + digits_editor: PTYListEditor } impl PosIntEditor { pub fn new(radix: u32) -> Self { PosIntEditor { radix, - digits_editor: ListEditor::new( + digits_editor: PTYListEditor::new( Box::new(move || Arc::new(RwLock::new(DigitEditor::new(radix)))), - crate::list::ListEditorStyle::Hex, + SeqDecorStyle::Hex, + 0 ), } } pub fn get_data_port(&self) -> OuterViewPort> { let radix = self.radix; - self.digits_editor + self.digits_editor.editor .get_data_port() .filter_map(move |digit_editor| { digit_editor.read().unwrap().data.get()?.to_digit(radix) @@ -132,44 +130,17 @@ impl TreeNav for PosIntEditor { fn goto(&mut self, cur: TreeCursor) -> TreeNavResult { self.digits_editor.goto(cur) } - fn goto_home(&mut self) -> TreeNavResult { - self.digits_editor.goto_home() - } - fn goto_end(&mut self) -> TreeNavResult { - self.digits_editor.goto_end() - } - fn pxev(&mut self) -> TreeNavResult { - self.digits_editor.pxev() - } - fn nexd(&mut self) -> TreeNavResult { - self.digits_editor.nexd() - } - fn up(&mut self) -> TreeNavResult { - self.digits_editor.up() - } - fn dn(&mut self) -> TreeNavResult { - self.digits_editor.dn() + fn goby(&mut self, cur: Vector2) -> TreeNavResult { + self.digits_editor.goby(cur) } + } impl TerminalEditor for PosIntEditor { fn get_term_view(&self) -> OuterViewPort { - self.digits_editor + self.digits_editor.editor .get_seg_seq_view() - .decorate( - match self.radix { - 2 => "0b", - 8 => "0o", - 10 => "0d", - 16 => "0x", - _ => "", - }, - "", - "", - 0, - ) - .to_grid_horizontal() - .flatten() + .pty_decorate(SeqDecorStyle::Hex, 0) } fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult { diff --git a/nested/src/integer/radix.rs b/nested/src/integer/radix.rs index 7eae64c..8aca421 100644 --- a/nested/src/integer/radix.rs +++ b/nested/src/integer/radix.rs @@ -26,7 +26,7 @@ impl RadixProjection { src_radix, dst_radix, src_digits: None, - dst_digits: RwLock::new(VecBuffer::new(dst_digits)), + dst_digits: RwLock::new(VecBuffer::with_port(dst_digits)), })); src_digits.add_observer(proj.clone()); proj diff --git a/nested/src/singleton/buffer.rs b/nested/src/singleton/buffer.rs index f1f0b16..d7e9596 100644 --- a/nested/src/singleton/buffer.rs +++ b/nested/src/singleton/buffer.rs @@ -1,6 +1,6 @@ use { crate::{ - core::{InnerViewPort, Observer, ObserverBroadcast, View}, + core::{InnerViewPort, OuterViewPort, Observer, ObserverBroadcast, View, ViewPort}, singleton::SingletonView, }, std::sync::RwLock, @@ -40,23 +40,31 @@ where T: Clone + Send + Sync + 'static, { value: Arc>, - cast: Arc>>>, + port: InnerViewPort> } impl SingletonBuffer where T: Clone + Send + Sync + 'static, { - pub fn new(value: T, port: InnerViewPort>) -> Self { + pub fn with_port(value: T, port: InnerViewPort>) -> Self { let value = Arc::new(RwLock::new(value)); port.set_view(Some(Arc::new(SingletonBufferView(value.clone())))); SingletonBuffer { value, - cast: port.get_broadcast(), + port } } + pub fn new(value: T) -> Self { + SingletonBuffer::with_port(value, ViewPort::new().into_inner()) + } + + pub fn get_port(&self) -> OuterViewPort> { + self.port.0.outer() + } + pub fn get(&self) -> T { self.value.read().unwrap().clone() } @@ -72,7 +80,7 @@ where let mut v = self.value.write().unwrap(); *v = new_value; drop(v); - self.cast.notify(&()); + self.port.notify(&()); } } diff --git a/nested/src/terminal/ansi_parser.rs b/nested/src/terminal/ansi_parser.rs index 648a063..d7b6658 100644 --- a/nested/src/terminal/ansi_parser.rs +++ b/nested/src/terminal/ansi_parser.rs @@ -28,10 +28,10 @@ pub fn read_ansi_from( let offset_port = ViewPort::>>::new(); let mut performer = PerfAtom { - buf: IndexBuffer::new(buf_port.inner()), - size: SingletonBuffer::new(max_size, size_port.inner()), - offset: SingletonBuffer::new(Vector2::new(0, 0), offset_port.inner()), - cursor: SingletonBuffer::new(Point2::new(0, 0), cursor_port.inner()), + buf: IndexBuffer::with_port(buf_port.inner()), + size: SingletonBuffer::with_port(max_size, size_port.inner()), + offset: SingletonBuffer::with_port(Vector2::new(0, 0), offset_port.inner()), + cursor: SingletonBuffer::with_port(Point2::new(0, 0), cursor_port.inner()), cursty: TerminalStyle::default(), curinv: false, cursav: Point2::new(0, 0), diff --git a/nested/src/vec/buffer.rs b/nested/src/vec/buffer.rs index 3cda4a3..2c145b2 100644 --- a/nested/src/vec/buffer.rs +++ b/nested/src/vec/buffer.rs @@ -1,6 +1,6 @@ use { crate::{ - core::{InnerViewPort, Observer, ObserverBroadcast, View}, + core::{InnerViewPort, OuterViewPort, Observer, ObserverBroadcast, View, ViewPort}, vec::VecDiff, }, std::sync::RwLock, @@ -27,7 +27,7 @@ where T: Clone + Send + Sync + 'static, { data: Arc>>, - cast: Arc>>>>, + port: InnerViewPort>> } impl VecBuffer @@ -35,23 +35,31 @@ where T: Clone + Send + Sync + 'static, { pub fn with_data(data: Vec, port: InnerViewPort>>) -> Self { - let mut b = VecBuffer::new(port); - for x in data.into_iter() { - b.push(x); - } - - b - } - - pub fn new(port: InnerViewPort>>) -> Self { - let data = Arc::new(RwLock::new(Vec::new())); + let data = Arc::new(RwLock::new(data)); port.set_view(Some(data.clone())); + + for x in data.read().unwrap().iter().cloned() { + port.notify(&VecDiff::Push(x)); + } + VecBuffer { data, - cast: port.get_broadcast(), + port } } + pub fn with_port(port: InnerViewPort>>) -> Self { + VecBuffer::with_data(vec![], port) + } + + pub fn new() -> Self { + VecBuffer::with_port(ViewPort::new().into_inner()) + } + + pub fn get_port(&self) -> OuterViewPort>> { + self.port.0.outer() + } + pub fn apply_diff(&mut self, diff: VecDiff) { let mut data = self.data.write().unwrap(); match &diff { @@ -73,7 +81,7 @@ where } drop(data); - self.cast.notify(&diff); + self.port.notify(&diff); } pub fn len(&self) -> usize {