add diagnostics; contanis some bugs

This commit is contained in:
Michael Sippel 2022-10-23 19:29:50 +02:00
parent 906cc51329
commit b2f437d7df
12 changed files with 340 additions and 43 deletions

View file

@ -9,6 +9,7 @@ use {
TerminalView,
},
tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult},
diagnostics::Diagnostics
},
std::sync::Arc,
std::sync::RwLock,
@ -37,6 +38,8 @@ impl CharEditor {
}
impl TreeNav for CharEditor {}
impl Diagnostics for CharEditor {}
impl TerminalEditor for CharEditor {
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
self.data

View file

@ -6,7 +6,7 @@ pub fn bg_style_from_depth(depth: usize) -> TerminalStyle {
match depth {
0 => TerminalStyle::bg_color((150,80,230)),
1 => TerminalStyle::bg_color((35,35,35)),
2 => TerminalStyle::bg_color((10,10,10)),
2 => TerminalStyle::bg_color((20,20,20)),
_ => TerminalStyle::default(),
}
}

91
nested/src/diagnostics.rs Normal file
View file

@ -0,0 +1,91 @@
use {
crate::{
core::{OuterViewPort, ViewPort},
sequence::{SequenceView, SequenceViewExt, decorator::{PTYSeqDecorate, SeqDecorStyle}},
vec::{VecBuffer},
index::{buffer::IndexBuffer},
terminal::{
TerminalView, TerminalStyle, make_label
}
},
cgmath::Point2
};
#[derive(Clone)]
pub struct Message {
pub addr: Vec<usize>,
pub port: OuterViewPort<dyn TerminalView>
}
pub trait Diagnostics {
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = Message>> {
VecBuffer::new().get_port().to_sequence()
}
}
pub fn make_error(msg: OuterViewPort<dyn TerminalView>) -> Message {
let mut mb = IndexBuffer::new();
mb.insert_iter(vec![
(Point2::new(0, 0),
make_label("error: ")
.map_item(|p,a| a
.add_style_back(TerminalStyle::bold(true))
.add_style_back(TerminalStyle::fg_color((200,0,0))))
),
(Point2::new(1, 0),
msg
.map_item(|p,a| a
.add_style_back(TerminalStyle::fg_color((180,180,180))))
)
]);
Message {
addr: vec![],
port: mb.get_port().flatten()
}
}
pub fn make_warn(msg: OuterViewPort<dyn TerminalView>) -> Message {
let mut mb = IndexBuffer::new();
mb.insert_iter(vec![
(Point2::new(0, 0),
make_label("warning: ")
.map_item(|p,a| a
.add_style_back(TerminalStyle::bold(true))
.add_style_back(TerminalStyle::fg_color((200,200,0))))
),
(Point2::new(1, 0),
msg
.map_item(|p,a| a
.add_style_back(TerminalStyle::fg_color((180,180,180))))
)
]);
Message {
addr: vec![],
port: mb.get_port().flatten()
}
}
pub fn make_todo(msg: OuterViewPort<dyn TerminalView>) -> Message {
let mut mb = IndexBuffer::new();
mb.insert_iter(vec![
(Point2::new(0, 0),
make_label("todo: ")
.map_item(|p,a| a
.add_style_back(TerminalStyle::bold(true))
.add_style_back(TerminalStyle::fg_color((180,180,250))))
),
(Point2::new(1, 0),
msg
.map_item(|p,a| a
.add_style_back(TerminalStyle::fg_color((180,180,180))))
)
]);
Message {
addr: vec![],
port: mb.get_port().flatten()
}
}

View file

@ -4,23 +4,27 @@ use {
list::{PTYListEditor},
sequence::{SequenceView, SequenceViewExt, decorator::{PTYSeqDecorate, SeqDecorStyle}},
singleton::{SingletonBuffer, SingletonView},
vec::{VecBuffer},
index::{buffer::IndexBuffer},
terminal::{
TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle,
TerminalView,
TerminalView, make_label
},
tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult},
diagnostics::{Diagnostics, Message}
},
std::sync::Arc,
std::sync::RwLock,
termion::event::{Event, Key},
cgmath::Vector2
cgmath::{Vector2, Point2}
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct DigitEditor {
radix: u32,
data: SingletonBuffer<Option<char>>
data: SingletonBuffer<Option<char>>,
msg: VecBuffer<Message>,
}
impl DigitEditor {
@ -28,6 +32,7 @@ impl DigitEditor {
DigitEditor {
radix,
data: SingletonBuffer::new(None),
msg: VecBuffer::new(),
}
}
@ -63,11 +68,26 @@ impl TerminalEditor for DigitEditor {
| TerminalEvent::Input(Event::Key(Key::Char('\n'))) => TerminalEditorResult::Exit,
TerminalEvent::Input(Event::Key(Key::Char(c))) => {
self.data.set(Some(*c));
self.msg.clear();
if c.to_digit(self.radix).is_none() {
let mut mb = IndexBuffer::new();
mb.insert_iter(vec![
(Point2::new(1, 0), make_label("invalid digit '")),
(Point2::new(2, 0), make_label(&format!("{}", *c))
.map_item(|p,a| a.add_style_back(TerminalStyle::fg_color((140,140,250))))),
(Point2::new(3, 0), make_label("'"))
]);
self.msg.push(crate::diagnostics::make_error(mb.get_port().flatten()));
}
TerminalEditorResult::Exit
}
TerminalEvent::Input(Event::Key(Key::Backspace))
| TerminalEvent::Input(Event::Key(Key::Delete)) => {
self.data.set(None);
self.msg.clear();
self.msg.push(crate::diagnostics::make_warn(make_label("empty digit")));
TerminalEditorResult::Exit
}
_ => TerminalEditorResult::Continue,
@ -77,6 +97,12 @@ impl TerminalEditor for DigitEditor {
impl TerminalTreeEditor for DigitEditor {}
impl Diagnostics for DigitEditor {
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>> {
self.msg.get_port().to_sequence()
}
}
pub struct PosIntEditor {
radix: u32,
digits_editor: PTYListEditor<DigitEditor>
@ -123,6 +149,12 @@ impl PosIntEditor {
}
}
impl Diagnostics for PosIntEditor {
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>> {
self.digits_editor.get_msg_port()
}
}
impl TreeNav for PosIntEditor {
fn get_cursor(&self) -> TreeCursor {
self.digits_editor.get_cursor()
@ -140,10 +172,24 @@ impl TreeNav for PosIntEditor {
impl TerminalEditor for PosIntEditor {
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
match self.radix {
10 => {
self.digits_editor.editor
.get_seg_seq_view()
.pty_decorate(SeqDecorStyle::Plain, 0)
},
16 => {
self.digits_editor.editor
.get_seg_seq_view()
.pty_decorate(SeqDecorStyle::Hex, 0)
}
_ => {
self.digits_editor.editor
.get_seg_seq_view()
.pty_decorate(SeqDecorStyle::Plain, 0)
}
}
}
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
match event {

View file

@ -17,9 +17,10 @@ pub mod grid;
pub mod vec;
// editors
pub mod tree_nav;
pub mod product;
pub mod list;
pub mod tree_nav;
pub mod diagnostics;
// high-level types
pub mod char_editor;

View file

@ -13,6 +13,7 @@ use {
TerminalView,
},
tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult},
diagnostics::{Diagnostics},
vec::VecBuffer,
color::{bg_style_from_depth, fg_style_from_depth}
},
@ -215,6 +216,30 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
}
}
impl<ItemEditor> Diagnostics for PTYListEditor<ItemEditor>
where ItemEditor: TerminalTreeEditor + Diagnostics + ?Sized + Send + Sync + 'static
{
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>> {
self.editor
.get_data_port()
.enumerate()
.map(
|(idx, item_editor)| {
let idx = *idx;
item_editor.read().unwrap()
.get_msg_port()
.map(
move |msg| {
let mut msg = msg.clone();
msg.addr.insert(0, idx);
msg
}
)
}
)
.flatten()
}
}
impl<ItemEditor> TerminalTreeEditor for PTYListEditor<ItemEditor>
where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static

View file

@ -5,13 +5,15 @@ use {
TerminalEditor, TerminalEditorResult,
TerminalEvent, TerminalView
},
sequence::{SequenceView},
tree_nav::{TreeNav, TerminalTreeEditor, TreeNavResult},
vec::{VecBuffer, MutableVecAccess},
index::{buffer::{IndexBuffer, MutableIndexAccess}, IndexView},
list::ListCursorMode,
product::{segment::ProductEditorSegment},
make_editor::make_editor
sequence::{SequenceView},
make_editor::make_editor,
tree_nav::{TreeNav, TerminalTreeEditor, TreeNavResult},
diagnostics::{Diagnostics, Message},
},
cgmath::{Vector2, Point2},
std::sync::{Arc, RwLock},
@ -20,6 +22,8 @@ use {
};
pub struct ProductEditor {
msg_buf: VecBuffer<Option<OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>>>>,
msg_port: OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>>,
segments: IndexBuffer<Point2<i16>, ProductEditorSegment>,
pub(super) n_indices: Vec<Point2<i16>>,
@ -30,8 +34,28 @@ pub struct ProductEditor {
impl ProductEditor {
pub fn new(depth: usize, ctx: Arc<RwLock<Context>>) -> Self {
let msg_buf = VecBuffer::new();
ProductEditor {
segments: IndexBuffer::new(),
msg_port: msg_buf.get_port()
.to_sequence()
.enumerate()
.filter_map(|(idx, msgs): &(usize, Option<OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>>>)| {
let idx = *idx;
if let Some(msgs) = msgs {
Some(msgs.map(
move |msg| {
let mut msg = msg.clone();
msg.addr.insert(0, idx);
msg
}))
} else {
None
}
})
.flatten(),
msg_buf,
n_indices: Vec::new(),
ctx,
cursor: None,
@ -46,13 +70,17 @@ impl ProductEditor {
pub fn with_n(mut self, pos: Point2<i16>, n: TypeLadder) -> Self {
self.segments.insert(pos, ProductEditorSegment::N{
t: n,
t: n.clone(),
editor: None,
ed_depth: self.depth + 1,
cur_depth: 0,
cur_dist: isize::MAX
});
self.n_indices.push(pos);
let mut b = VecBuffer::new();
b.push(crate::diagnostics::make_todo(crate::terminal::make_label(&format!("complete {}", self.ctx.read().unwrap().type_term_to_str(&n[0])))));
self.msg_buf.push(Some(b.get_port().to_sequence()));
self
}
@ -101,7 +129,7 @@ impl ProductEditor {
}
pub fn update_segment(&mut self, idx: isize) {
if let Some(ProductEditorSegment::N{ t: _, editor, ed_depth: _, cur_depth, cur_dist }) = self.get_editor_segment_mut(idx).deref_mut() {
if let Some(ProductEditorSegment::N{ t, editor, ed_depth: _, cur_depth, cur_dist }) = self.get_editor_segment_mut(idx).deref_mut() {
let cur = self.get_cursor();
if cur.tree_addr.len() > 0 {
@ -113,6 +141,16 @@ impl ProductEditor {
} else {
*cur_dist = isize::MAX;
};
if let Some(e) = editor {
self.msg_buf.update(idx as usize, Some(e.read().unwrap().get_msg_port()));
} else {
let mut b = VecBuffer::new();
b.push(crate::diagnostics::make_todo(crate::terminal::make_label(&format!("complete {}", self.ctx.read().unwrap().type_term_to_str(&t[0])))));
self.msg_buf.update(idx as usize, Some(b.get_port().to_sequence()));
}
} else {
unreachable!()
}
@ -139,14 +177,13 @@ impl TerminalEditor for ProductEditor {
if let Some(ProductEditorSegment::N{ t, editor, ed_depth, cur_depth, cur_dist }) = segment.deref_mut() {
*cur_depth = self.get_cursor().tree_addr.len();
if let Some(e) = editor.clone() {
let result = if let Some(e) = editor.clone() {
let mut ce = e.write().unwrap();
match ce.handle_terminal_event(event) {
TerminalEditorResult::Exit =>
match event {
TerminalEvent::Input(Event::Key(Key::Backspace)) => {
*editor = None;
*cur_depth = 1;
TerminalEditorResult::Continue
}
_ => {
@ -170,7 +207,10 @@ impl TerminalEditor for ProductEditor {
let x = e.write().unwrap().handle_terminal_event(event);
*cur_depth = e.write().unwrap().get_cursor().tree_addr.len();
x
}
};
self.update_cur_segment();
result
} else {
unreachable!()
}
@ -180,3 +220,10 @@ impl TerminalEditor for ProductEditor {
}
}
impl Diagnostics for ProductEditor {
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = crate::diagnostics::Message>> {
self.msg_port.clone()
}
}

View file

@ -117,6 +117,9 @@ where
s.cast.notify(&(idx + chunk_offset));
s.cast.notify_each(dirty_idx);
} else {
let dirty_idx = s.update_all_offsets();
s.cast.notify_each(dirty_idx);
}
},
),
@ -124,7 +127,6 @@ where
);
chunk_port.0.update();
let dirty_idx = self.update_all_offsets();
self.cast.notify_each(dirty_idx);
} else {
@ -161,8 +163,11 @@ where
let old_length = self.length;
self.length = cur_offset;
/* FIXXME: causes hangup
if self.length < old_length {
dirty_idx.extend(self.length..old_length);
}
*/
dirty_idx
}

View file

@ -150,6 +150,7 @@ pub trait TreeNav {
}
use crate::terminal::{TerminalEditor};
use crate::diagnostics::{Diagnostics};
pub trait TerminalTreeEditor : TerminalEditor + TreeNav + Send {}
pub trait TerminalTreeEditor : TerminalEditor + TreeNav + Diagnostics + Send {}

View file

@ -8,7 +8,7 @@ use {
nested::{
vec::VecBuffer,
list::{ListEditor, PTYListEditor},
sequence::decorator::{Separate, Wrap, SeqDecorStyle},
sequence::{SequenceView, decorator::{Separate, Wrap, SeqDecorStyle}},
core::{TypeTerm, Context},
core::{OuterViewPort, ViewPort},
index::{IndexArea, IndexView},
@ -17,6 +17,7 @@ use {
TerminalAtom, TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalStyle, TerminalView, make_label
},
tree_nav::{TreeCursor, TreeNav, TreeNavResult, TerminalTreeEditor},
diagnostics::{Diagnostics},
make_editor::make_editor,
product::ProductEditor
}
@ -76,6 +77,22 @@ impl Action for ActCp {
}
}
pub struct ActNum {}
impl Action for ActNum {
fn make_editor(&self, ctx: Arc<RwLock<Context>>) -> Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>> {
let depth = 1;
Arc::new(RwLock::new(ProductEditor::new(depth, ctx.clone())
.with_t(Point2::new(1, 1), " Value")
.with_n(Point2::new(0, 1), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() ] )
.with_t(Point2::new(1, 2), " Radix")
.with_n(Point2::new(0, 2), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] )
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
// Arc::new(RwLock::new(nested::integer::PosIntEditor::new(10)))
}
}
pub struct Commander {
ctx: Arc<RwLock<Context>>,
cmds: HashMap<String, Arc<dyn Action + Send + Sync>>,
@ -87,6 +104,9 @@ pub struct Commander {
view_elements: VecBuffer<OuterViewPort<dyn TerminalView>>,
out_port: OuterViewPort<dyn TerminalView>,
m_buf: VecBuffer<OuterViewPort<dyn SequenceView<Item = nested::diagnostics::Message>>>,
msg_port: OuterViewPort<dyn SequenceView<Item = nested::diagnostics::Message>>
}
impl Commander {
@ -123,7 +143,9 @@ impl Commander {
cmds.insert("echo".into(), Arc::new(ActEcho{}) as Arc<dyn Action + Send + Sync>);
cmds.insert("ls".into(), Arc::new(ActLs{}) as Arc<dyn Action + Send + Sync>);
cmds.insert("cp".into(), Arc::new(ActCp{}) as Arc<dyn Action + Send + Sync>);
cmds.insert("num".into(), Arc::new(ActNum{}) as Arc<dyn Action + Send + Sync>);
let m_buf = VecBuffer::new();
let mut c = Commander {
ctx,
cmds,
@ -136,7 +158,12 @@ impl Commander {
.to_sequence()
.separate(make_label(" "))
.to_grid_horizontal()
.flatten()
.flatten(),
msg_port: m_buf.get_port()
.to_sequence()
.flatten(),
m_buf
};
c
@ -154,6 +181,8 @@ impl TerminalEditor for Commander {
TerminalEvent::Input(Event::Key(Key::Char('\n'))) => {
// run
cmd_editor.write().unwrap().goto(TreeCursor::none());
TerminalEditorResult::Exit
}
event => {
@ -171,6 +200,8 @@ impl TerminalEditor for Commander {
*self.view_elements.get_mut(1) = editor.read().unwrap().get_term_view();
self.m_buf.push(editor.read().unwrap().get_msg_port());
if *event == TerminalEvent::Input(Event::Key(Key::Char('\n'))) {
return self.handle_terminal_event(event);
}
@ -194,7 +225,6 @@ impl TerminalEditor for Commander {
} else {
*self.view_elements.get_mut(1) = editor.read().unwrap().get_term_view().map_item(|p,a| a.add_style_front(TerminalStyle::fg_color((80,80,80))));
}
self.cmd_editor = Some(editor);
*self.valid.write().unwrap() = true;
} else {
@ -213,6 +243,12 @@ impl TerminalEditor for Commander {
}
}
impl Diagnostics for Commander {
fn get_msg_port(&self) -> OuterViewPort<dyn SequenceView<Item = nested::diagnostics::Message>> {
self.msg_port.clone()
}
}
impl TreeNav for Commander {
fn get_cursor(&self) -> TreeCursor {
if let (Some(cmd_editor), true) = (self.cmd_editor.as_ref(), self.confirmed) {
@ -242,7 +278,6 @@ impl TreeNav for Commander {
self.symbol_editor.goto(cur)
}
}
}
impl TerminalTreeEditor for Commander {}

View file

@ -24,6 +24,8 @@ use {
},
tree_nav::{TreeNav, TerminalTreeEditor, TreeCursor, TreeNavResult},
vec::VecBuffer,
integer::{PosIntEditor},
diagnostics::{Diagnostics}
},
std::sync::{Arc, RwLock},
termion::event::{Event, Key},
@ -55,7 +57,17 @@ async fn main() {
"PosInt", "Digit", "LittleEndian", "BigEndian",
"DiffStream", "", "List", "Path", "Term", "RGB", "Vec3i"
] { ctx.write().unwrap().add_typename(tn.into()); }
/*
let mut process_list_editor = PTYListEditor::new(
Box::new(
move || {
Arc::new(RwLock::new(PosIntEditor::new(16)))
}
),
SeqDecorStyle::VerticalSexpr,
0
);
*/
let mut process_list_editor = PTYListEditor::new(
Box::new({let ctx = ctx.clone(); move || Arc::new(RwLock::new(Commander::new(ctx.clone())))}),
/*
@ -86,19 +98,12 @@ async fn main() {
let mut cur_size = nested::singleton::SingletonBuffer::new(Vector2::new(10, 10));
let mut status_chars = VecBuffer::new();
let mut plist = VecBuffer::new();
let mut plist_port = plist.get_port();
async_std::task::spawn(async move {
let (w, _h) = termion::terminal_size().unwrap();
let mut x: usize = 0;
loop {
let val = (5.0
+ (x as f32 / 3.0).sin() * 5.0
+ 2.0
+ ((7 + x) as f32 / 5.0).sin() * 2.0
+ 2.0
+ ((9 + x) as f32 / 10.0).cos() * 3.0) as usize;
let mut plist = VecBuffer::new(); let mut plist_port =
plist.get_port(); async_std::task::spawn(async move { let (w,
_h) = termion::terminal_size().unwrap(); let mut x: usize = 0;
loop { let val = (5.0 + (x as f32 / 3.0).sin() * 5.0 + 2.0 +
((7 + x) as f32 / 5.0).sin() * 2.0 + 2.0 + ((9 + x) as f32 /
10.0).cos() * 3.0) as usize;
if x < w as usize {
plist.push(val);
@ -133,6 +138,36 @@ async fn main() {
.separate(make_label(" ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~").map_item(|p,a| a.add_style_front(TerminalStyle::fg_color((40,40,40)))))
.to_grid_vertical()
.flatten()),
(Point2::new(0, 5), make_label(" ")),
(Point2::new(0, 6), make_label("-~~--~~--~~--~~--~~--~~--~~--~~--~~--~~").map_item(|p,a| a.add_style_front(TerminalStyle::fg_color((200,200,200))))),
(Point2::new(0, 7), process_list_editor.get_msg_port().map(
|entry| {
let mut b = VecBuffer::new();
b.push(
make_label("@")
.map_item(|p,a| a
.add_style_back(TerminalStyle::bold(true))
.add_style_back(TerminalStyle::fg_color((180,180,0))))
);
for x in entry.addr.iter() {
b.push(
make_label(&format!("{}", x))
);
b.push(
make_label(".")
.map_item(|p,a| a
.add_style_back(TerminalStyle::bold(true))
.add_style_back(TerminalStyle::fg_color((180,180,0))))
);
}
b.push(entry.port.clone());
b.get_port().to_sequence().to_grid_horizontal().flatten()
}
).to_grid_vertical().flatten())
]);
let (w, h) = termion::terminal_size().unwrap();
@ -219,7 +254,7 @@ async fn main() {
term_port.inner().get_broadcast().notify(&IndexArea::Full);
continue;
}
/*
if let Some(process_editor) = process_list_editor.get_item() {
let mut pe = process_editor.write().unwrap();
/*
@ -233,7 +268,7 @@ async fn main() {
}
*/
}
*/
match ev {
TerminalEvent::Input(Event::Key(Key::Ctrl('d'))) => break,
TerminalEvent::Input(Event::Key(Key::Ctrl('l'))) => {
@ -241,7 +276,7 @@ async fn main() {
leaf_mode: ListCursorMode::Insert,
tree_addr: vec![0],
});
process_list_editor.clear();
//process_list_editor.clear();
}
TerminalEvent::Input(Event::Key(Key::Left)) => {
process_list_editor.pxev();

View file

@ -11,6 +11,7 @@ use {
TerminalView,
},
tree_nav::{TerminalTreeEditor, TreeCursor, TreeNav, TreeNavResult},
diagnostics::Diagnostics
},
std::sync::Arc,
std::sync::RwLock,
@ -73,6 +74,9 @@ impl TreeNav for ProcessArg {
}
}
impl Diagnostics for ProcessArg {
}
impl TerminalTreeEditor for ProcessArg {}
pub struct ProcessLauncher {
@ -263,4 +267,8 @@ impl TreeNav for ProcessLauncher {
}
impl Diagnostics for ProcessLauncher {
}
impl TerminalTreeEditor for ProcessLauncher {}