2021-09-03 06:49:18 +02:00
|
|
|
use {
|
|
|
|
std::{
|
|
|
|
sync::Arc,
|
|
|
|
process::Command,
|
|
|
|
os::unix::io::{FromRawFd, AsRawFd},
|
|
|
|
},
|
|
|
|
std::sync::RwLock,
|
|
|
|
termion::event::{Key, Event},
|
|
|
|
cgmath::Point2,
|
|
|
|
nested::{
|
2021-09-24 23:31:09 +02:00
|
|
|
core::{ViewPort, OuterViewPort, InnerViewPort, Observer},
|
2021-09-03 06:49:18 +02:00
|
|
|
singleton::{SingletonView, SingletonBuffer},
|
|
|
|
sequence::{SequenceView, SequenceViewExt},
|
|
|
|
index::buffer::IndexBuffer,
|
|
|
|
vec::VecBuffer,
|
|
|
|
terminal::{TerminalAtom, TerminalStyle, TerminalView, TerminalEvent, TerminalEditor, TerminalEditorResult, make_label},
|
|
|
|
tree_nav::{TreeNav, TreeNavResult, TerminalTreeEditor, TreeCursor},
|
2021-11-11 22:29:37 +01:00
|
|
|
list::{ListCursorMode, ListEditor, ListEditorStyle, sexpr::ListDecoration},
|
2021-09-03 06:49:18 +02:00
|
|
|
string_editor::CharEditor,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
|
|
|
|
|
|
|
pub struct ProcessArg {
|
|
|
|
editor: ListEditor< CharEditor,
|
|
|
|
Box<dyn Fn() -> Arc<RwLock<CharEditor>> + Send + Sync + 'static> >
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcessArg {
|
|
|
|
pub fn get_data_port(&self) -> OuterViewPort<dyn SequenceView<Item = char>> {
|
|
|
|
self.editor.get_data_port()
|
|
|
|
.map(
|
|
|
|
|char_editor| char_editor.read().unwrap().get_data_port().get_view().unwrap().get().unwrap()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TerminalEditor for ProcessArg {
|
|
|
|
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
|
|
|
|
self.editor
|
|
|
|
.get_seg_seq_view()
|
|
|
|
.to_grid_horizontal()
|
|
|
|
.flatten()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
|
|
|
|
match event {
|
|
|
|
TerminalEvent::Input(Event::Key(Key::Char(' '))) |
|
|
|
|
TerminalEvent::Input(Event::Key(Key::Char('\n'))) => {
|
|
|
|
self.editor.up();
|
|
|
|
TerminalEditorResult::Exit
|
|
|
|
}
|
|
|
|
|
|
|
|
event => self.editor.handle_terminal_event(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeNav for ProcessArg {
|
|
|
|
fn get_cursor(&self) -> TreeCursor { self.editor.get_cursor() }
|
|
|
|
fn goto(&mut self, cur: TreeCursor) -> TreeNavResult { self.editor.goto(cur) }
|
|
|
|
fn goto_home(&mut self) -> TreeNavResult { self.editor.goto_home() }
|
|
|
|
fn goto_end(&mut self) -> TreeNavResult { self.editor.goto_end() }
|
|
|
|
fn pxev(&mut self) -> TreeNavResult { self.editor.pxev() }
|
|
|
|
fn nexd(&mut self) -> TreeNavResult { self.editor.nexd() }
|
|
|
|
fn up(&mut self) -> TreeNavResult { self.editor.up() }
|
|
|
|
fn dn(&mut self) -> TreeNavResult { self.editor.dn() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ProcessLauncher {
|
2021-09-24 23:31:09 +02:00
|
|
|
cmd_editor: ListEditor<
|
|
|
|
ProcessArg, Box<dyn Fn() -> Arc<RwLock<ProcessArg>> + Send + Sync + 'static>
|
|
|
|
>,
|
|
|
|
pty: Option<crate::pty::PTY>,
|
|
|
|
ptybox: Arc<RwLock<crate::ascii_box::AsciiBox>>,
|
2021-11-11 22:29:37 +01:00
|
|
|
suspended: bool,
|
2021-09-24 23:31:09 +02:00
|
|
|
|
|
|
|
pty_port: ViewPort<dyn TerminalView>,
|
2021-11-11 22:29:37 +01:00
|
|
|
status_port: ViewPort<dyn SingletonView<Item = Option<portable_pty::ExitStatus>>>,
|
|
|
|
|
2021-09-24 23:31:09 +02:00
|
|
|
comp_port: ViewPort<dyn TerminalView>,
|
|
|
|
compositor: Arc<RwLock<nested::terminal::TerminalCompositor>>
|
2021-09-03 06:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcessLauncher {
|
|
|
|
pub fn new() -> Self {
|
2021-09-24 23:31:09 +02:00
|
|
|
let pty_port = ViewPort::new();
|
2021-11-11 22:29:37 +01:00
|
|
|
let status_port = ViewPort::new();
|
2021-09-24 23:31:09 +02:00
|
|
|
let comp_port = ViewPort::new();
|
|
|
|
let box_port = ViewPort::<dyn TerminalView>::new();
|
|
|
|
let compositor = nested::terminal::TerminalCompositor::new(comp_port.inner());
|
|
|
|
|
|
|
|
let cmd_editor = ListEditor::new(
|
2021-09-03 06:49:18 +02:00
|
|
|
Box::new(
|
|
|
|
|| {
|
|
|
|
Arc::new(RwLock::new(ProcessArg {
|
|
|
|
editor: ListEditor::new(
|
|
|
|
Box::new(
|
|
|
|
|| {
|
|
|
|
Arc::new(RwLock::new(CharEditor::new()))
|
|
|
|
}
|
|
|
|
),
|
|
|
|
ListEditorStyle::Plain)
|
|
|
|
}))
|
|
|
|
}
|
2021-09-24 23:31:09 +02:00
|
|
|
) as Box::<dyn Fn() -> Arc<RwLock<ProcessArg>> + Send + Sync>,
|
2021-09-03 06:49:18 +02:00
|
|
|
ListEditorStyle::Plain
|
2021-09-24 23:31:09 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
compositor.write().unwrap().push(
|
|
|
|
box_port.outer()
|
|
|
|
.map_item(|_idx, x| x.add_style_back(TerminalStyle::fg_color((90, 120, 100))))
|
|
|
|
);
|
|
|
|
compositor.write().unwrap().push(
|
|
|
|
cmd_editor
|
|
|
|
.get_seg_seq_view()
|
|
|
|
.decorate("$(", ")", " ", 0)
|
|
|
|
.to_grid_horizontal()
|
|
|
|
.flatten()
|
|
|
|
);
|
|
|
|
|
|
|
|
ProcessLauncher {
|
|
|
|
cmd_editor,
|
|
|
|
pty: None,
|
|
|
|
ptybox: crate::ascii_box::AsciiBox::new(
|
|
|
|
cgmath::Vector2::new(80, 25),
|
|
|
|
pty_port.outer()
|
|
|
|
.map_item(|_,a:&TerminalAtom| a.add_style_back(TerminalStyle::fg_color((230, 230, 230)))),
|
|
|
|
box_port.inner()
|
2021-11-11 22:29:37 +01:00
|
|
|
),
|
|
|
|
suspended: false,
|
2021-09-24 23:31:09 +02:00
|
|
|
pty_port,
|
2021-11-11 22:29:37 +01:00
|
|
|
status_port,
|
2021-09-24 23:31:09 +02:00
|
|
|
comp_port,
|
|
|
|
compositor
|
2021-09-03 06:49:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:29:37 +01:00
|
|
|
pub fn launch_pty(&mut self) {
|
2021-09-03 06:49:18 +02:00
|
|
|
let mut strings = Vec::new();
|
|
|
|
|
2021-09-24 23:31:09 +02:00
|
|
|
let v = self.cmd_editor.get_data_port().get_view().unwrap();
|
2021-09-03 06:49:18 +02:00
|
|
|
for i in 0 .. v.len().unwrap_or(0) {
|
|
|
|
let arg_view = v.get(&i).unwrap().read().unwrap().get_data_port().get_view().unwrap();
|
|
|
|
strings.push(arg_view.iter().collect::<String>());
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.len() > 0 {
|
2021-09-15 15:24:39 +02:00
|
|
|
// Spawn a shell into the pty
|
2021-09-15 17:47:40 +02:00
|
|
|
let mut cmd = crate::pty::CommandBuilder::new(strings[0].as_str());
|
2021-09-15 15:24:39 +02:00
|
|
|
cmd.args(&strings[1..]);
|
|
|
|
|
2021-11-11 22:29:37 +01:00
|
|
|
self.cmd_editor.goto(TreeCursor {
|
|
|
|
leaf_mode: ListCursorMode::Insert,
|
|
|
|
tree_addr: vec![]
|
|
|
|
});
|
|
|
|
|
|
|
|
self.pty = crate::pty::PTY::new(cmd, self.pty_port.inner(), self.status_port.inner());
|
2021-09-03 06:49:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 22:29:37 +01:00
|
|
|
|
|
|
|
pub fn is_captured(&self) -> bool {
|
|
|
|
self.pty.is_some() && !self.suspended
|
|
|
|
}
|
2021-09-03 06:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TerminalEditor for ProcessLauncher {
|
|
|
|
fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
|
2021-09-24 23:31:09 +02:00
|
|
|
self.comp_port.outer()
|
2021-09-03 06:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
|
2021-11-11 22:29:37 +01:00
|
|
|
|
|
|
|
// todo: move to observer of status view
|
|
|
|
if let Some(status) = self.status_port.outer().get_view().get() {
|
|
|
|
self.pty = None;
|
|
|
|
self.suspended = false;
|
|
|
|
}
|
|
|
|
|
2021-09-03 06:49:18 +02:00
|
|
|
match event {
|
2021-11-11 22:29:37 +01:00
|
|
|
TerminalEvent::Input(Event::Key(Key::Ctrl('c'))) => {
|
|
|
|
self.pty = None;
|
|
|
|
self.suspended = false;
|
|
|
|
self.cmd_editor.goto(TreeCursor {
|
|
|
|
leaf_mode: ListCursorMode::Insert,
|
|
|
|
tree_addr: vec![]
|
|
|
|
});
|
|
|
|
TerminalEditorResult::Exit
|
|
|
|
},
|
|
|
|
TerminalEvent::Input(Event::Key(Key::Ctrl('z'))) => {
|
|
|
|
self.suspended = true;
|
|
|
|
self.cmd_editor.goto(TreeCursor {
|
|
|
|
leaf_mode: ListCursorMode::Insert,
|
|
|
|
tree_addr: vec![]
|
|
|
|
});
|
2021-09-03 06:49:18 +02:00
|
|
|
TerminalEditorResult::Exit
|
|
|
|
}
|
2021-11-11 22:29:37 +01:00
|
|
|
event => {
|
|
|
|
if let Some(pty) = self.pty.as_mut() {
|
|
|
|
pty.handle_terminal_event(event);
|
|
|
|
TerminalEditorResult::Continue
|
|
|
|
} else {
|
|
|
|
match event {
|
|
|
|
TerminalEvent::Input(Event::Key(Key::Char('\n'))) => {
|
|
|
|
// launch command
|
|
|
|
self.launch_pty();
|
|
|
|
TerminalEditorResult::Continue
|
|
|
|
}
|
|
|
|
event => self.cmd_editor.handle_terminal_event(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 06:49:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeNav for ProcessLauncher {
|
2021-11-11 22:29:37 +01:00
|
|
|
fn get_cursor(&self) -> TreeCursor {
|
|
|
|
self.cmd_editor.get_cursor()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn goto(&mut self, cur: TreeCursor) -> TreeNavResult {
|
|
|
|
self.suspended = false;
|
|
|
|
if let Some(status) = self.status_port.outer().get_view().get() {
|
|
|
|
self.pty = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.pty.is_none() {
|
|
|
|
self.cmd_editor.goto(cur)
|
|
|
|
} else {
|
|
|
|
self.cmd_editor.goto(TreeCursor {
|
|
|
|
leaf_mode: ListCursorMode::Select,
|
|
|
|
tree_addr: vec![]
|
|
|
|
});
|
|
|
|
TreeNavResult::Continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn goto_home(&mut self) -> TreeNavResult {
|
|
|
|
self.cmd_editor.goto_home()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn goto_end(&mut self) -> TreeNavResult {
|
|
|
|
self.cmd_editor.goto_end()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pxev(&mut self) -> TreeNavResult {
|
|
|
|
self.cmd_editor.pxev()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nexd(&mut self) -> TreeNavResult {
|
|
|
|
self.cmd_editor.nexd()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn up(&mut self) -> TreeNavResult {
|
|
|
|
self.cmd_editor.up()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dn(&mut self) -> TreeNavResult {
|
|
|
|
self.cmd_editor.dn()
|
|
|
|
}
|
2021-09-03 06:49:18 +02:00
|
|
|
}
|
|
|
|
|