shell: first commander prototype

This commit is contained in:
Michael Sippel 2022-08-12 18:57:40 +02:00
parent 55eb594521
commit 4e02b41a77
14 changed files with 220 additions and 140 deletions

View file

@ -30,6 +30,10 @@ impl CharEditor {
pub fn get_port(&self) -> OuterViewPort<dyn SingletonView<Item = Option<char>>> {
self.data.get_port()
}
pub fn get(&self) -> char {
self.get_port().get_view().unwrap().get().unwrap_or('?')
}
}
impl TreeNav for CharEditor {}

View file

@ -3,22 +3,23 @@ use {
};
pub fn bg_style_from_depth(depth: usize) -> TerminalStyle {
if depth == 0 {
TerminalStyle::default()
} else {
TerminalStyle::bg_color((
(30.0 / ( 0.90*depth as f64 )) as u8,
(30.0 / ( 0.93*depth as f64 )) as u8,
(50.0 / ( 0.95*depth as f64 )) as u8
))
match depth {
0 => TerminalStyle::default(),
1 => TerminalStyle::bg_color((20,20,20)),
2 => TerminalStyle::default(),
3 => TerminalStyle::default(),
4 => TerminalStyle::default(),
5 => TerminalStyle::default(),
_ => TerminalStyle::bg_color((80,80,80))
}
}
pub fn fg_style_from_depth(depth: usize) -> TerminalStyle {
match depth % 3 {
0 => TerminalStyle::fg_color((200, 200, 80)),
1 => TerminalStyle::fg_color((80, 200, 200)),
2 => TerminalStyle::fg_color((150, 150, 200)),
1 => TerminalStyle::fg_color((80, 200, 200)).add(TerminalStyle::bold(true)),
2 => TerminalStyle::fg_color((80, 80, 200)),
3 => TerminalStyle::fg_color((200, 80, 200)),
_ => TerminalStyle::default()
}
}

View file

@ -87,7 +87,7 @@ impl PosIntEditor {
PosIntEditor {
radix,
digits_editor: PTYListEditor::new(
Box::new(move || Arc::new(RwLock::new(DigitEditor::new(radix)))),
Box::new(move || Arc::new(RwLock::new(DigitEditor::new(radix)))) as Box<dyn Fn() -> Arc<RwLock<DigitEditor>> + Send + Sync>,
SeqDecorStyle::Hex,
0
),
@ -136,7 +136,6 @@ impl TreeNav for PosIntEditor {
fn goby(&mut self, cur: Vector2<isize>) -> TreeNavResult {
self.digits_editor.goby(cur)
}
}
impl TerminalEditor for PosIntEditor {

View file

@ -36,11 +36,11 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
impl<ItemEditor> ListEditor<ItemEditor>
where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
{
pub fn new(make_item_editor: impl Into<Box<dyn Fn() -> Arc<RwLock<ItemEditor>> + Send + Sync>>, depth: usize) -> Self {
pub fn new(make_item_editor: impl Fn() -> Arc<RwLock<ItemEditor>> + Send + Sync + 'static, depth: usize) -> Self {
ListEditor {
cursor: SingletonBuffer::new(ListCursor::default()),
data: VecBuffer::<Arc<RwLock<ItemEditor>>>::new(),
make_item_editor: make_item_editor.into(),
make_item_editor: Box::new(make_item_editor),
depth,
cur_dist: Arc::new(RwLock::new(0)),
}
@ -58,7 +58,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
);
segment_view_port.into_outer().map(move |segment| segment.pty_view())
}
pub fn get_data_port(&self) -> OuterViewPort<dyn SequenceView<Item = Arc<RwLock<ItemEditor>>>> {
self.data.get_port().to_sequence()
}

View file

@ -133,6 +133,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
if direction.y < 0 {
// up
self.cursor.set(ListCursor::none());
TreeNavResult::Exit
} else if direction.y > 0 {
// dn

View file

@ -36,7 +36,7 @@ impl<ItemEditor> PTYListEditor<ItemEditor>
where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
{
pub fn new(
make_item_editor: Box<dyn Fn() -> Arc<RwLock<ItemEditor>> + Send + Sync>,
make_item_editor: impl Fn() -> Arc<RwLock<ItemEditor>> + Send + Sync + 'static,
style: SeqDecorStyle,
depth: usize
) -> Self {
@ -49,6 +49,20 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
}
}
pub fn from_editor(
editor: ListEditor<ItemEditor>,
style: SeqDecorStyle,
depth: usize
) -> Self {
let port = ViewPort::new();
PTYListEditor {
editor,
style,
depth,
port
}
}
pub fn get_data_port(&self) -> OuterViewPort<dyn SequenceView<Item = Arc<RwLock<ItemEditor>>>> {
self.editor.get_data_port()
}
@ -206,3 +220,15 @@ impl<ItemEditor> TerminalTreeEditor for PTYListEditor<ItemEditor>
where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
{}
use crate::{
char_editor::CharEditor,
sequence::SequenceViewExt
};
impl PTYListEditor<CharEditor> {
pub fn get_string(&self) -> String {
self.get_data_port().map(|ce| ce.read().unwrap().get()).get_view().unwrap().iter().collect::<String>()
}
}

View file

@ -46,7 +46,7 @@ where ItemEditor: TerminalTreeEditor + ?Sized + Send + Sync + 'static
editor.read().unwrap().get_term_view().map_item(move |_pt, atom| {
let cur_depth = e.read().unwrap().get_cursor().tree_addr.len();
atom.add_style_back(bg_style_from_depth(cur_depth))
.add_style_back(fg_style_from_depth(d+cur_depth))
.add_style_back(fg_style_from_depth(d))
})
}
}

View file

@ -76,13 +76,13 @@ pub fn make_editor(ctx: Arc<RwLock<Context>>, t: &TypeLadder, depth: usize) -> A
} else if t[0] == c.type_term_from_str("( List RGB )").unwrap() {
Arc::new(RwLock::new(
PTYListEditor::<dyn TerminalTreeEditor + Send +Sync>::new(
Box::new({
{
let d = depth+1;
let ctx = ctx.clone();
move || {
Box::new(move || {
make_editor(ctx.clone(), &vec![ ctx.read().unwrap().type_term_from_str("( RGB )").unwrap() ], d)
}
}),
})
},
SeqDecorStyle::VerticalSexpr,
depth
)
@ -97,7 +97,7 @@ pub fn make_editor(ctx: Arc<RwLock<Context>>, t: &TypeLadder, depth: usize) -> A
.with_n(Point2::new(2, 2), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() ] )
.with_t(Point2::new(1, 3), "b: ")
.with_n(Point2::new(2, 3), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() ] )
.with_t(Point2::new(0, 4), " }")
.with_t(Point2::new(0, 4), "}")
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
} else if t[0] == c.type_term_from_str("( Vec3i )").unwrap() {
@ -109,7 +109,7 @@ pub fn make_editor(ctx: Arc<RwLock<Context>>, t: &TypeLadder, depth: usize) -> A
.with_n(Point2::new(2, 2), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] )
.with_t(Point2::new(1, 3), "z: ")
.with_n(Point2::new(2, 3), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 10 BigEndian )").unwrap() ] )
.with_t(Point2::new(0, 4), " }")
.with_t(Point2::new(0, 4), "}")
)) as Arc<RwLock<dyn TerminalTreeEditor + Send + Sync>>
} else if t[0] == c.type_term_from_str("( Json )").unwrap() {
@ -147,14 +147,13 @@ pub fn make_editor(ctx: Arc<RwLock<Context>>, t: &TypeLadder, depth: usize) -> A
} else { // else: term
Arc::new(RwLock::new(
PTYListEditor::new(
Box::new(|| {
|| {
Arc::new(RwLock::new(CharEditor::new()))
}),
},
SeqDecorStyle::DoubleQuote,
depth
)
))
}
}

View file

@ -119,10 +119,11 @@ impl TerminalEditor for ProductEditor {
match event {
TerminalEvent::Input(Event::Key(Key::Backspace)) => {
*editor = None;
*cur_depth -= 1;
*cur_depth = 1;
TerminalEditorResult::Continue
}
_ => {
*cur_depth = ce.get_cursor().tree_addr.len();
drop(ce);
match self.nexd() {
TreeNavResult::Continue => TerminalEditorResult::Continue,
@ -130,15 +131,17 @@ impl TerminalEditor for ProductEditor {
}
}
},
TerminalEditorResult::Continue =>
TerminalEditorResult::Continue
TerminalEditorResult::Continue => {
*cur_depth = ce.get_cursor().tree_addr.len();
TerminalEditorResult::Continue
}
}
} else {
let e = make_editor(self.ctx.clone(), t, self.depth+1);
*editor = Some(e.clone());
e.write().unwrap().dn();
let x = e.write().unwrap().handle_terminal_event(event);
*cur_depth = self.get_cursor().tree_addr.len()+1;
*cur_depth = e.write().unwrap().get_cursor().tree_addr.len();
x
}
} else {

View file

@ -66,9 +66,15 @@ impl TreeNav for ProductEditor {
self.cursor = Some(crate::modulo(c.tree_addr.remove(0), self.n_indices.len() as isize));
if let Some(mut element) = self.get_cur_segment_mut() {
if let Some(ProductEditorSegment::N{ t: _t, editor, cur_depth }) = element.deref_mut() {
if let Some(ProductEditorSegment::N{ t, editor, cur_depth }) = element.deref_mut() {
if let Some(e) = editor {
e.write().unwrap().goto(c.clone());
} else if c.tree_addr.len() > 0 {
// create editor
let e = make_editor(self.ctx.clone(), t, self.depth+1);
*editor = Some(e.clone());
let mut e = e.write().unwrap();
e.goto(c.clone());
}
*cur_depth = c.tree_addr.len();
}
@ -111,7 +117,7 @@ impl TreeNav for ProductEditor {
if let Some(e) = editor {
let mut e = e.write().unwrap();
e.goby(direction);
*cur_depth = e.get_cursor().tree_addr.len();
*cur_depth = e.get_cursor().tree_addr.len() + 1;
} else {
// create editor
let e = make_editor(self.ctx.clone(), t, self.depth+1);
@ -133,6 +139,12 @@ impl TreeNav for ProductEditor {
self.cursor = None;
TreeNavResult::Exit
} else {
if let Some(mut element) = self.get_cur_segment_mut() {
if let Some(ProductEditorSegment::N{ t, editor, cur_depth }) = element.deref_mut() {
*cur_depth = 0;
}
}
// horizontal
if (cur.tree_addr[0]+direction.x >= 0) &&
(cur.tree_addr[0]+direction.x < self.n_indices.len() as isize)
@ -142,6 +154,7 @@ impl TreeNav for ProductEditor {
*cur_depth = 0;
}
}
self.cursor = Some(cur.tree_addr[0] + direction.x);
if let Some(mut element) = self.get_cur_segment_mut() {
if let Some(ProductEditorSegment::N{ t, editor, cur_depth }) = element.deref_mut() {
@ -165,21 +178,31 @@ impl TreeNav for ProductEditor {
//\\//\\//\\//\\
match ce.goby(direction) {
TreeNavResult::Exit => {
*cur_depth = 0;
*cur_depth = 1;
drop(ce);
drop(e);
if direction.y < 0 {
if depth <= (1-direction.y) as usize {
// up
TreeNavResult::Exit
*cur_depth = 1;
TreeNavResult::Continue
} else {
panic!("unplausible direction.y on exit");
TreeNavResult::Continue
}
} else if direction.y > 0 {
// dn
*cur_depth = depth + direction.y as usize;
TreeNavResult::Continue
} else if direction.y == 0 {
// horizontal
if direction.x != 0 {
*cur_depth = 0;
}
if (cur.tree_addr[0]+direction.x >= 0) &&
(cur.tree_addr[0]+direction.x < self.n_indices.len() as isize)
{
@ -205,9 +228,7 @@ impl TreeNav for ProductEditor {
}
}
TreeNavResult::Continue => {
if direction.y > 0 {
*cur_depth = depth + direction.y as usize - 1;
}
*cur_depth = (depth as isize + direction.y - 1) as usize;
TreeNavResult::Continue
}
}

View file

@ -37,16 +37,20 @@ impl ProductEditorSegment {
ProductEditorSegment::N { t: _, editor: Some(e), cur_depth } =>
e.read().unwrap()
.get_term_view()
.map_item({ let cur_depth = *cur_depth;//e.read().unwrap().get_cursor().tree_addr.len()+1;
move |i, x| x
.map_item({
let e = e.clone();
move |i, x| {
let cur_depth = e.read().unwrap().get_cursor().tree_addr.len();
x
.add_style_back(fg_style_from_depth(cur_depth))//fg_color((250,210,0)))
.add_style_back(bg_style_from_depth(cur_depth))
}
}),
ProductEditorSegment::N{ t, editor: None, cur_depth } =>
make_label(&ctx.read().unwrap().type_term_to_str(&t[0]))
.map_item({
let cur_depth = *cur_depth;
let cur_depth = 0;
move |i, x| x
.add_style_back(TerminalStyle::fg_color((130,90,40)))
.add_style_back(bg_style_from_depth(cur_depth))

View file

@ -24,6 +24,7 @@ use {
},
};
#[derive(PartialEq, Eq)]
pub enum TerminalEvent {
Resize(Vector2<i16>),
Input(termion::event::Event),