lib-nested/shell/src/main.rs

196 lines
8 KiB
Rust
Raw Normal View History

2021-09-03 06:49:18 +02:00
extern crate tty;
2021-08-11 17:56:42 +02:00
mod monstera;
2021-09-03 06:49:18 +02:00
mod process;
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},
2021-09-03 06:49:18 +02:00
sequence::{SequenceView, SequenceViewExt},
vec::{VecBuffer},
2021-09-03 06:49:18 +02:00
integer::{RadixProjection, DigitEditor, PosIntEditor},
terminal::{
Terminal,
TerminalStyle,
TerminalAtom,
TerminalCompositor,
TerminalEvent,
make_label,
TerminalView,
TerminalEditor},
2021-09-03 06:49:18 +02:00
string_editor::{StringEditor},
tree_nav::{TreeNav, TreeNavResult, TreeCursor, TerminalTreeEditor},
2021-08-23 05:00:57 +02:00
list::{SExprView, ListCursorMode, ListEditor, ListEditorStyle}
2021-09-03 06:49:18 +02:00
},
crate::{
process::ProcessLauncher
2021-06-05 23:30:40 +02:00
}
};
#[async_std::main]
async fn main() {
let term_port = ViewPort::new();
let compositor = TerminalCompositor::new(term_port.inner());
let mut term = Terminal::new(term_port.outer());
let term_writer = term.get_writer();
async_std::task::spawn(
async move {
2021-08-28 17:01:31 +02:00
let table_port = ViewPort::<dyn nested::grid::GridView<Item = OuterViewPort<dyn TerminalView>>>::new();
let mut table_buf = nested::index::buffer::IndexBuffer::new(table_port.inner());
let magic = make_label("<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>")
.map_item(
|pos, atom|
atom.add_style_back(
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-08-28 17:01:31 +02:00
)
);
2021-06-05 23:30:40 +02:00
let cur_size_port = ViewPort::new();
let mut cur_size = nested::singleton::SingletonBuffer::new(Vector2::new(10, 10), cur_size_port.inner());
let status_chars_port = ViewPort::new();
let mut status_chars = VecBuffer::new(status_chars_port.inner());
2021-08-28 17:01:31 +02:00
table_buf.insert_iter(vec![
(Point2::new(0, 0), magic.clone()),
2021-09-03 06:49:18 +02:00
(Point2::new(0, 1), status_chars_port.outer().to_sequence().to_grid_horizontal()),
2021-08-28 17:01:31 +02:00
]);
compositor.write().unwrap().push(monstera::make_monstera());
compositor.write().unwrap().push(table_port.outer().flatten().offset(Vector2::new(40, 2)));
2021-09-03 06:49:18 +02:00
let mut y = 2;
let mut process_launcher = ProcessLauncher::new();
table_buf.insert(Point2::new(0, y), process_launcher.get_term_view());
process_launcher.goto(TreeCursor {
leaf_mode: ListCursorMode::Insert,
tree_addr: vec![ 0 ]
});
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::Left)) => {
2021-09-03 06:49:18 +02:00
process_launcher.pxev();
}
TerminalEvent::Input(Event::Key(Key::Right)) => {
2021-09-03 06:49:18 +02:00
process_launcher.nexd();
}
2021-09-03 06:49:18 +02:00
TerminalEvent::Input(Event::Key(Key::Up)) => { process_launcher.up(); }
TerminalEvent::Input(Event::Key(Key::Down)) => {
2021-09-03 06:49:18 +02:00
if process_launcher.dn() == TreeNavResult::Continue {
process_launcher.goto_home();
}
}
TerminalEvent::Input(Event::Key(Key::Home)) => {
2021-09-03 06:49:18 +02:00
process_launcher.goto_home();
}
TerminalEvent::Input(Event::Key(Key::End)) => {
2021-09-03 06:49:18 +02:00
process_launcher.goto_end();
}
TerminalEvent::Input(Event::Key(Key::Char('\n'))) => {
2021-09-03 06:49:18 +02:00
let output_view = process_launcher.launch();
2021-09-03 06:49:18 +02:00
y += 1;
table_buf.insert(Point2::new(0, y), output_view);
2021-09-03 06:49:18 +02:00
process_launcher = ProcessLauncher::new();
process_launcher.goto(TreeCursor {
leaf_mode: ListCursorMode::Insert,
tree_addr: vec![ 0 ]
});
y += 1;
2021-09-03 06:49:18 +02:00
table_buf.insert(Point2::new(0, y), process_launcher.get_term_view());
}
ev => {
2021-09-03 06:49:18 +02:00
if process_launcher.get_cursor().leaf_mode == ListCursorMode::Select {
match ev {
2021-09-03 06:49:18 +02:00
TerminalEvent::Input(Event::Key(Key::Char('l'))) => { process_launcher.up(); },
TerminalEvent::Input(Event::Key(Key::Char('a'))) => { process_launcher.dn(); },
TerminalEvent::Input(Event::Key(Key::Char('i'))) => { process_launcher.pxev(); },
TerminalEvent::Input(Event::Key(Key::Char('e'))) => { process_launcher.nexd(); },
TerminalEvent::Input(Event::Key(Key::Char('u'))) => { process_launcher.goto_home(); },
TerminalEvent::Input(Event::Key(Key::Char('o'))) => { process_launcher.goto_end(); },
_ => {
2021-09-03 06:49:18 +02:00
process_launcher.handle_terminal_event(&ev);
}
}
} else {
2021-09-03 06:49:18 +02:00
process_launcher.handle_terminal_event(&ev);
}
}
2021-06-05 23:30:40 +02:00
}
status_chars.clear();
2021-09-03 06:49:18 +02:00
let cur = process_launcher.get_cursor();
if cur.tree_addr.len() > 0 {
2021-08-23 05:00:57 +02:00
status_chars.push(TerminalAtom::new('@', TerminalStyle::fg_color((120, 80, 80)).add(TerminalStyle::bold(true))));
for x in cur.tree_addr {
for c in format!("{}", x).chars() {
status_chars.push(TerminalAtom::new(c, TerminalStyle::fg_color((0, 100, 20))));
}
2021-08-23 05:00:57 +02:00
status_chars.push(TerminalAtom::new('.', TerminalStyle::fg_color((120, 80, 80))));
}
status_chars.push(TerminalAtom::new(':', TerminalStyle::fg_color((120, 80, 80)).add(TerminalStyle::bold(true))));
for c in
match cur.leaf_mode {
ListCursorMode::Insert => "INSERT",
ListCursorMode::Select => "SELECT",
ListCursorMode::Modify => "MODIFY"
}.chars()
{
status_chars.push(TerminalAtom::new(c, TerminalStyle::fg_color((200, 200, 20))));
}
2021-08-23 05:00:57 +02:00
status_chars.push(TerminalAtom::new(':', TerminalStyle::fg_color((120, 80, 80)).add(TerminalStyle::bold(true))));
} else {
for c in "Press <DN> to enter".chars() {
status_chars.push(TerminalAtom::new(c, TerminalStyle::fg_color((200, 200, 20))));
}
}
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!");
}