move toy widgets from shell into lib
This commit is contained in:
parent
883cd01f99
commit
97000cab7a
8 changed files with 108 additions and 34 deletions
nested/src
122
nested/src/ascii_box.rs
Normal file
122
nested/src/ascii_box.rs
Normal file
|
@ -0,0 +1,122 @@
|
|||
use {
|
||||
cgmath::{Point2, Vector2},
|
||||
nested::{
|
||||
core::{InnerViewPort, Observer, ObserverBroadcast, OuterViewPort, View},
|
||||
index::{IndexArea, IndexView},
|
||||
terminal::{TerminalAtom, TerminalView},
|
||||
},
|
||||
std::sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
pub struct AsciiBox {
|
||||
content: Option<Arc<dyn TerminalView>>,
|
||||
extent: Vector2<i16>,
|
||||
|
||||
cast: Arc<RwLock<ObserverBroadcast<dyn TerminalView>>>,
|
||||
}
|
||||
|
||||
impl AsciiBox {
|
||||
pub fn new(
|
||||
extent: Vector2<i16>,
|
||||
content_port: OuterViewPort<dyn TerminalView>,
|
||||
output_port: InnerViewPort<dyn TerminalView>,
|
||||
) -> Arc<RwLock<Self>> {
|
||||
let ascii_box = Arc::new(RwLock::new(AsciiBox {
|
||||
content: None,
|
||||
extent,
|
||||
cast: output_port.get_broadcast(),
|
||||
}));
|
||||
|
||||
output_port
|
||||
.0
|
||||
.update_hooks
|
||||
.write()
|
||||
.unwrap()
|
||||
.push(Arc::new(content_port.0.clone()));
|
||||
output_port.set_view(Some(ascii_box.clone()));
|
||||
content_port.add_observer(ascii_box.clone());
|
||||
|
||||
ascii_box
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, new_extent: Vector2<i16>) {
|
||||
if self.extent != new_extent {
|
||||
let old_extent = self.extent;
|
||||
self.extent = new_extent;
|
||||
self.cast.notify(&IndexArea::Range(
|
||||
Point2::new(0, 0)
|
||||
..=Point2::new(
|
||||
1 + std::cmp::max(old_extent.x, new_extent.x),
|
||||
1 + std::cmp::max(old_extent.y, new_extent.y),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fit_content(&mut self) {
|
||||
if let Some(c) = self.content.as_ref() {
|
||||
let p = *c.area().range().end();
|
||||
self.resize(Vector2::new(p.x + 1, p.y + 1));
|
||||
} else {
|
||||
self.resize(Vector2::new(0, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Observer<dyn TerminalView> for AsciiBox {
|
||||
fn reset(&mut self, new_content: Option<Arc<dyn TerminalView>>) {
|
||||
self.content = new_content;
|
||||
self.fit_content();
|
||||
}
|
||||
|
||||
fn notify(&mut self, area: &IndexArea<Point2<i16>>) {
|
||||
self.cast.notify(&area.map(|pt| pt + Vector2::new(1, 1)));
|
||||
self.fit_content();
|
||||
}
|
||||
}
|
||||
|
||||
impl View for AsciiBox {
|
||||
type Msg = IndexArea<Point2<i16>>;
|
||||
}
|
||||
|
||||
impl IndexView<Point2<i16>> for AsciiBox {
|
||||
type Item = TerminalAtom;
|
||||
|
||||
fn get(&self, pt: &Point2<i16>) -> Option<TerminalAtom> {
|
||||
if pt.x == 0 || pt.x == self.extent.x + 1 {
|
||||
// vertical line
|
||||
if pt.y == 0 && pt.x == 0 {
|
||||
Some(TerminalAtom::from('╭'))
|
||||
} else if pt.y == 0 && pt.x == self.extent.x + 1 {
|
||||
Some(TerminalAtom::from('╮'))
|
||||
} else if pt.y > 0 && pt.y < self.extent.y + 1 {
|
||||
Some(TerminalAtom::from('│'))
|
||||
} else if pt.y == self.extent.y + 1 && pt.x == 0 {
|
||||
Some(TerminalAtom::from('╰'))
|
||||
} else if pt.y == self.extent.y + 1 && pt.x == self.extent.x + 1 {
|
||||
Some(TerminalAtom::from('╯'))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else if pt.y == 0 || pt.y == self.extent.y + 1 {
|
||||
// horizontal line
|
||||
if pt.x > 0 && pt.x < self.extent.x + 1 {
|
||||
Some(TerminalAtom::from('─'))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else if pt.x > 0 && pt.y > 0 && pt.x < self.extent.x + 1 && pt.y < self.extent.y + 1 {
|
||||
Some(
|
||||
self.content
|
||||
.get(&(pt - Vector2::new(1, 1)))
|
||||
.unwrap_or(TerminalAtom::from(' ')),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn area(&self) -> IndexArea<Point2<i16>> {
|
||||
IndexArea::Range(Point2::new(0, 0)..=Point2::new(1, 1) + self.extent)
|
||||
}
|
||||
}
|
51
nested/src/monstera.rs
Normal file
51
nested/src/monstera.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use {
|
||||
cgmath::Point2,
|
||||
nested::{
|
||||
core::{OuterViewPort, ViewPort},
|
||||
terminal::{make_label, TerminalStyle, TerminalView},
|
||||
vec::VecBuffer,
|
||||
},
|
||||
};
|
||||
|
||||
pub fn make_monstera() -> OuterViewPort<dyn TerminalView> {
|
||||
let monstera_lines_port = ViewPort::new();
|
||||
let _monstera_lines = VecBuffer::with_data(
|
||||
vec![
|
||||
make_label(" |"),
|
||||
make_label(" |"),
|
||||
make_label(" _..._ | _..._"),
|
||||
make_label(" .( \\|/ )."),
|
||||
make_label(" ( | )"),
|
||||
make_label(" .__>. <> | <> .<__."),
|
||||
make_label(" / | \\"),
|
||||
make_label(" | .___ _ | _ ___. |"),
|
||||
make_label(" _./___>. / \\ | / \\ .<___\\._ "),
|
||||
make_label(" / \\_/ | \\_/ \\"),
|
||||
make_label(" ( .____. | .____. )"),
|
||||
make_label(" ( /____ \\ _ | _ / ____\\ )"),
|
||||
make_label(" _* \\.) / \\ | / \\ (./ *_"),
|
||||
make_label(" / \\_/ | \\_/ \\"),
|
||||
make_label(" ( .__. | .__. )"),
|
||||
make_label(" ( / __ \\ | / __ \\ )"),
|
||||
make_label(" * / \\.) O | O (./ \\ *"),
|
||||
make_label(" / .___. | .___. \\"),
|
||||
make_label(" ( / .---\\ | /---. \\ )"),
|
||||
make_label(" *. ( | ) .*"),
|
||||
make_label(" \\_ . | . _/"),
|
||||
make_label(" \\ | /"),
|
||||
make_label(" ."),
|
||||
],
|
||||
monstera_lines_port.inner(),
|
||||
);
|
||||
|
||||
monstera_lines_port
|
||||
.outer()
|
||||
.to_sequence()
|
||||
.to_index()
|
||||
.map_key(
|
||||
|idx| Point2::new(0 as i16, *idx as i16),
|
||||
|pt| if pt.x == 0 { Some(pt.y as usize) } else { None },
|
||||
)
|
||||
.flatten()
|
||||
.map_item(|_p, at| at.add_style_back(TerminalStyle::fg_color((0, 100, 10))))
|
||||
}
|
94
nested/src/plot.rs
Normal file
94
nested/src/plot.rs
Normal file
|
@ -0,0 +1,94 @@
|
|||
use {
|
||||
cgmath::Point2,
|
||||
nested::{
|
||||
core::{InnerViewPort, Observer, ObserverBroadcast, OuterViewPort, View},
|
||||
sequence::{SequenceView},
|
||||
index::{IndexArea, IndexView},
|
||||
projection::ProjectionHelper,
|
||||
terminal::{TerminalAtom, TerminalView},
|
||||
},
|
||||
std::sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue