move plot to separate file

This commit is contained in:
Michael Sippel 2021-11-19 12:04:37 +01:00
parent f480b79031
commit bf8a949cdd
Signed by: senvas
GPG key ID: F96CF119C34B64A6
3 changed files with 143 additions and 118 deletions

View file

@ -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>>>>>,

View file

@ -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()),

139
shell/src/plot.rs Normal file
View file

@ -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
}
}