lib-nested/shell/src/main.rs

517 lines
18 KiB
Rust
Raw Normal View History

//mod monstera;
use{
std::sync::{Arc, RwLock},
cgmath::{Point2, Vector2},
2021-06-05 23:30:40 +02:00
termion::event::{Event, Key},
nested::{
core::{
View,
2021-06-05 23:30:40 +02:00
ViewPort,
OuterViewPort,
Observer,
ObserverExt,
context::{ReprTree, Object, MorphismType, MorphismMode, Context},
port::{UpdateTask}},
index::{IndexView},
sequence::{SequenceView},
vec::{VecBuffer},
2021-06-05 23:30:40 +02:00
integer::{RadixProjection},
terminal::{
Terminal,
TerminalStyle,
TerminalAtom,
TerminalCompositor,
TerminalEvent,
make_label,
TerminalView,
TerminalEditor},
string_editor::StringEditor,
list::{SExprView, ListEditor}
2021-06-05 23:30:40 +02:00
}
};
struct GridFill<T: Send + Sync + Clone>(T);
impl<T: Send + Sync + Clone> View for GridFill<T> {
type Msg = Point2<i16>;
}
impl<T: Send + Sync + Clone> IndexView<Point2<i16>> for GridFill<T> {
type Item = T;
fn area(&self) -> Option<Vec<Point2<i16>>> {
None
}
fn get(&self, _: &Point2<i16>) -> Option<T> {
Some(self.0.clone())
}
}
2021-06-06 18:44:41 +02:00
2021-06-05 23:30:40 +02:00
#[async_std::main]
async fn main() {
/* todo:
open::
>0:
( Path )
( Sequence ( Sequence UnicodeChar ) )
( Sequence UnicodeChar )
<1:
( FileDescriptor )
( MachineInt )
read::
>0:
( FileDescriptor )
( MachineInt )
<1:
( Sequence MachineSyllab )
( Vec MachineSyllab )
write::
>0
( FileDescriptor )
( MachineInt )
>1:
( Sequence MachineSyllab )
( Vec MachineSyllab )
*/
let mut args = std::env::args();
/*
2021-06-05 23:30:40 +02:00
let arg0_port = ViewPort::new();
let _arg0 = VecBuffer::<char>::with_data(
args.next().expect("Arg $0 missing!")
.chars().collect::<Vec<char>>(),
arg0_port.inner()
);
*/
2021-06-05 23:30:40 +02:00
let arg1_vec_port = ViewPort::new();
let mut arg1 = VecBuffer::<char>::with_data(
vec!['1'],
/*
2021-06-05 23:30:40 +02:00
args.next().expect("Arg $1 missing!")
.chars().collect::<Vec<char>>(),
*/
2021-06-05 23:30:40 +02:00
arg1_vec_port.inner()
);
/*
let _arg1_vec = args.next().expect("Arg $1 missing!")
2021-06-05 23:30:40 +02:00
.chars().collect::<Vec<char>>();
*/
2021-06-05 23:30:40 +02:00
let term_port = ViewPort::new();
let compositor = TerminalCompositor::new(term_port.inner());
let mut ed = StringEditor::new();
let mut term = Terminal::new(term_port.outer());
let term_writer = term.get_writer();
async_std::task::spawn(
async move {
let mut ctx = Context::new();
for tn in vec![
"MachineWord", "MachineInt", "MachineSyllab", "Bits",
"Vec", "Stream", "Json",
"Sequence", "UTF-8-String", "UnicodeChar",
"PositionalInt", "Digit", "LittleEndian", "BigEndian",
"DiffStream", ""
] { ctx.add_typename(tn.into()); }
let src_type =
ctx.type_term_from_str("( Vec UnicodeChar )").unwrap();
let dst_type =
ctx.type_term_from_str("( Sequence UnicodeChar )").unwrap();
ctx.add_morphism(
MorphismType {
mode: MorphismMode::Epi,
src_type: src_type.clone(),
dst_type: dst_type.clone()
},
Box::new(move |src| {
assert!(src.type_tag == src_type);
Object {
type_tag: dst_type.clone(),
repr: ReprTree::new_leaf(
src.get_port::<RwLock<Vec<char>>>().unwrap()
.to_sequence()
.into()
)
}
})
);
let src_type = ctx.type_term_from_str("( Sequence UnicodeChar )").unwrap();
let dst_type = ctx.type_term_from_str("( Sequence ( Bits 32 ) )").unwrap();
ctx.add_morphism(
MorphismType {
mode: MorphismMode::Mono,
src_type: src_type.clone(),
dst_type: dst_type.clone()
},
Box::new({
move |src| {
assert!(src.type_tag == src_type);
Object {
type_tag: dst_type.clone(),
repr: ReprTree::new_leaf(
src.get_port::<dyn SequenceView<Item = char>>().unwrap()
.map(
|c| *c as u32
)
.into()
)
}
}
})
);
/*
let src_type = vec![
ctx.type_term_from_str("( PositionalInteger )").unwrap(),
];
let dst_type = ctx.type_term_from_str("( Sequence MachineInt )").unwrap();
ctx.add_morphism(
MorphismType {
mode: MorphismMode::Epi,
src_type: src_type.clone(),
dst_type: dst_type.clone()
},
Box::new({
move |src| {
assert!(src.type_tag == src_type);
Object {
type_tag: dst_type.clone(),
repr: ReprTree::new_leaf(
vec![ dst_type.clone() ].into_iter(),
src.get_port::<RwLock<Vec<usize>>>().unwrap().to_sequence().into()
)
}
}
})
);
*/
2021-06-05 23:30:40 +02:00
let arg1_vec_port = ed.get_data_port();
ctx.add_obj("$1".into(), "( Vec UnicodeChar )");
ctx.insert_repr(
"$1",
vec![].into_iter(),
arg1_vec_port.clone().into()
);
ctx.epi_cast("$1", "( Sequence UnicodeChar )");
ctx.epi_cast("$1", "( Sequence ( Digit 10 ) )");
ctx.epi_cast("$1", "( PositionalInt 10 LittleEndian )");
ctx.epi_cast("$1", "( )");
let arg1_dec_unic_port: OuterViewPort<dyn SequenceView<Item = char>> =
ctx.mono_view(
"$1",
vec![
"( PositionalInt 10 LittleEndian )",
"( Sequence ( Digit 10 ) )",
"( Sequence UnicodeChar )"
].into_iter()
).unwrap();
let arg1_dec_mint_port: OuterViewPort<dyn SequenceView<Item = usize>> =
arg1_dec_unic_port
.map(|c| c.to_digit(10).map(|x| x as usize))
.filter(|d| d.is_some())
.map(|d| d.unwrap());
ctx.insert_repr(
"$1",
vec![
"( PositionalInt 10 LittleEndian )",
"( Sequence ( Digit 10 ) )",
"( Sequence MachineInt )"
].into_iter(),
arg1_dec_mint_port.clone().into()
);
let arg1_hex_mint_port: ViewPort<RwLock<Vec<usize>>>
= ViewPort::new();
let _radix_proj = RadixProjection::new(
10,
16,
arg1_dec_mint_port.clone(),
arg1_hex_mint_port.inner()
);
ctx.insert_repr(
"$1",
vec![
"( PositionalInt 16 LittleEndian )",
"( Sequence ( Digit 16 ) )",
"( Sequence MachineInt )"
].into_iter(),
arg1_hex_mint_port.outer().to_sequence().into()
);
let arg1_hex_unic_port: OuterViewPort<dyn SequenceView<Item = char>> =
arg1_hex_mint_port.outer().to_sequence()
.map(
|d| char::from_digit(*d as u32, 16).unwrap()
);
ctx.insert_repr(
"$1",
vec![
"( PositionalInt 16 LittleEndian )",
"( Sequence ( Digit 16 ) )",
"( Sequence UnicodeChar )"
].into_iter(),
arg1_hex_unic_port.clone().into()
);
let magic = make_label("<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>")
2021-06-05 23:30:40 +02:00
.map_item(
|pos, atom|
atom.add_style_back(
2021-06-05 23:30:40 +02:00
TerminalStyle::fg_color(
(5,
((80+(pos.x*30)%100) as u8),
(55+(pos.x*15)%180) as u8)
2021-06-05 23:30:40 +02:00
)
)
);
2021-06-05 23:30:40 +02:00
{
compositor.write().unwrap().push(magic.offset(Vector2::new(40, 4)));
//compositor.write().unwrap().push(magic.offset(Vector2::new(40, 20)));
/*
let monstera_port = monstera::make_monstera();
compositor.write().unwrap().push(monstera_port.clone());
compositor.write().unwrap().push(monstera_port.offset(Vector2::new(83,0)));
*/
2021-06-05 23:30:40 +02:00
}
/*
// list views
2021-06-06 18:44:41 +02:00
{
let items_port = ViewPort::new();
let items = VecBuffer::with_data(
vec![
arg1_hex_unic_port.clone()
.map(|c| TerminalAtom::from(c))
.to_index()
.map_key(
|idx| Point2::new(*idx as i16, 0 as i16),
|pt| if pt.y == 0 { Some(pt.x as usize) } else { None }
),
ed.insert_view()
.map_item(
|_pos, atom|
TerminalAtom::new(
atom.c.unwrap_or(' '),
TerminalStyle::fg_color(
if let Some(c) = atom.c {
if c == '|' {
(200, 200, 90)
} else if c.is_digit(10) {
(0, 200, 0)
} else {
(255, 0, 0)
}
} else {
(0, 0, 0)
}
).add(
TerminalStyle::bg_color((0,0,0))
)
)
),
2021-06-06 18:44:41 +02:00
arg1_hex_unic_port.clone()
.map(|c| TerminalAtom::from(c))
.to_index()
.map_key(
|idx| Point2::new(*idx as i16, 0 as i16),
|pt| if pt.y == 0 { Some(pt.x as usize) } else { None }
),
2021-06-06 18:44:41 +02:00
],
items_port.inner()
);
let par_items_port = ViewPort::new();
let par_items = VecBuffer::with_data(
vec![
items_port.outer().to_sequence().sexpr_view(1),
2021-06-06 18:44:41 +02:00
arg1_hex_unic_port.clone()
.map(|c| TerminalAtom::from(c))
.to_index()
.map_key(
|idx| Point2::new(*idx as i16, 0 as i16),
|pt| if pt.y == 0 { Some(pt.x as usize) } else { None }
),
2021-06-06 18:44:41 +02:00
],
par_items_port.inner()
);
compositor.write().unwrap().push(
par_items_port.outer()
.to_sequence()
.sexpr_view(0)
.offset(Vector2::new(45, 5))
2021-06-06 18:44:41 +02:00
);
}
2021-06-06 18:44:41 +02:00
for c in _arg1_vec {
ed.insert(c);
}
*/
let cur_size_port = ViewPort::new();
let mut cur_size = nested::singleton::SingletonBuffer::new(Vector2::new(10, 10), cur_size_port.inner());
// compositor.write().unwrap()
// .push(
let label_port = cur_size_port.outer()
.map(
|size| make_label(format!("Current Size: {:?}", size).as_str())
);
// );
//term_port.update();
//eprintln!("start loop:");
{
// let history_port = ViewPort::new();
// let mut history = VecBuffer::new(history_port.inner());
/*
2021-06-06 18:44:41 +02:00
compositor.write().unwrap().push(
history_port.into_outer()
.to_sequence()
.map(
|
2021-06-06 18:44:41 +02:00
)
.to_grid_vertical()
.flatten()
.offset(Vector2::new(45, 5))
2021-06-06 18:44:41 +02:00
);
*/
};
let mut y = 5;
2021-06-05 23:30:40 +02:00
// TypeEditor
let make_sub_editor = || {
std::sync::Arc::new(std::sync::RwLock::new(StringEditor::new()))
};
let mut te = ListEditor::new(make_sub_editor.clone());
2021-06-05 23:30:40 +02:00
compositor.write().unwrap().push(
te.segment_seq.horizontal_sexpr_view(0).offset(cgmath::Vector2::new(40,y))
2021-06-05 23:30:40 +02:00
);
y += 1;
2021-06-05 23:30:40 +02:00
let mut p = te.get_data_port().map(|string_editor| string_editor.read().unwrap().get_data_port());
2021-06-05 23:30:40 +02:00
loop {
term_port.update();
2021-06-05 23:30:40 +02:00
match term.next_event().await {
TerminalEvent::Resize(new_size) => {
cur_size.set(new_size);
term_port.inner().get_broadcast().notify_each(
nested::grid::GridWindowIterator::from(
Point2::new(0,0) .. Point2::new(new_size.x, new_size.y)
)
);
}
TerminalEvent::Input(Event::Key(Key::Ctrl('c'))) |
TerminalEvent::Input(Event::Key(Key::Ctrl('g'))) |
TerminalEvent::Input(Event::Key(Key::Ctrl('d'))) => break,
TerminalEvent::Input(Event::Key(Key::Char('\n'))) => {
let mut strings = Vec::new();
let v = p.get_view().unwrap();
for i in 0 .. v.len().unwrap_or(0) {
strings.push(
v
.get(&i).unwrap()
.get_view().unwrap()
.read().unwrap()
.iter().collect::<String>()
);
}
if strings.len() == 0 { continue; }
if let Ok(output) =
std::process::Command::new(strings[0].as_str())
.args(&strings[1..])
.output()
{
// take output and update terminal view
let mut line_port = ViewPort::new();
let mut line = VecBuffer::new(line_port.inner());
for byte in output.stdout {
match byte {
b'\n' => {
compositor.write().unwrap().push(
line_port.outer()
.to_sequence()
.map(|c| TerminalAtom::new(*c, TerminalStyle::fg_color((130,90,90))))
.to_grid_horizontal()
.offset(Vector2::new(45, y))
);
y += 1;
line_port = ViewPort::new();
line = VecBuffer::new(line_port.inner());
}
byte => {
line.push(byte as char);
}
}
}
} else {
compositor.write().unwrap().push(
make_label("Command not found")
.map_item(|idx, a| a.add_style_back(TerminalStyle::fg_color((200,0,0))))
.offset(Vector2::new(45, y))
);
y+=1;
}
te.handle_terminal_event(
&TerminalEvent::Input(Event::Key(Key::Up))
);
te.handle_terminal_event(
&TerminalEvent::Input(Event::Key(Key::Home))
);
te = ListEditor::new(make_sub_editor.clone());
compositor.write().unwrap().push(magic.offset(Vector2::new(40, y)));
y += 1;
compositor.write().unwrap().push(
te.segment_seq.horizontal_sexpr_view(0).offset(cgmath::Vector2::new(40,y))
);
y += 1;
p = te.get_data_port().map(|string_editor| string_editor.read().unwrap().get_data_port());
},
ev => {
te.handle_terminal_event(&ev);
}
2021-06-05 23:30:40 +02:00
_ => {}
}
}
//drop(term);
2021-06-05 23:30:40 +02:00
}
);
term_writer.show().await.expect("output error!");
}