2021-09-15 17:47:40 +02:00
|
|
|
|
|
|
|
use {
|
|
|
|
termion::event::{Key, Event},
|
2021-09-24 23:31:09 +02:00
|
|
|
std::sync::Mutex,
|
2021-09-15 17:47:40 +02:00
|
|
|
nested::{
|
|
|
|
core::{InnerViewPort},
|
|
|
|
terminal::{TerminalView, TerminalEvent}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub use portable_pty::CommandBuilder;
|
|
|
|
|
|
|
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
|
|
|
|
|
|
|
pub struct PTY {
|
2021-09-24 23:31:09 +02:00
|
|
|
master: Mutex<Box<dyn portable_pty::MasterPty + Send>>,
|
2021-09-15 17:47:40 +02:00
|
|
|
child: Box<dyn portable_pty::Child + Send + Sync>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PTY {
|
|
|
|
pub fn new(
|
|
|
|
cmd: portable_pty::CommandBuilder,
|
|
|
|
port: InnerViewPort<dyn TerminalView>
|
|
|
|
) -> Option<Self> {
|
|
|
|
|
|
|
|
// Create a new pty
|
|
|
|
let mut pair = portable_pty::native_pty_system().openpty(portable_pty::PtySize {
|
|
|
|
rows: 25,
|
|
|
|
cols: 80,
|
|
|
|
|
|
|
|
// Not all systems support pixel_width, pixel_height,
|
|
|
|
// but it is good practice to set it to something
|
|
|
|
// that matches the size of the selected font. That
|
|
|
|
// is more complex than can be shown here in this
|
|
|
|
// brief example though!
|
|
|
|
pixel_width: 0,
|
|
|
|
pixel_height: 0,
|
|
|
|
}).unwrap();
|
|
|
|
|
|
|
|
if let Ok(child) = pair.slave.spawn_command(cmd) {
|
|
|
|
let mut reader = pair.master.try_clone_reader().unwrap();
|
|
|
|
|
|
|
|
async_std::task::spawn_blocking(
|
|
|
|
move || {
|
|
|
|
nested::terminal::ansi_parser::read_ansi_from(&mut reader, port);
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(PTY {
|
2021-09-24 23:31:09 +02:00
|
|
|
master: Mutex::new(pair.master),
|
2021-09-15 17:47:40 +02:00
|
|
|
child
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_status(&mut self) -> bool {
|
|
|
|
if let Ok(Some(status)) = self.child.try_wait() {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_terminal_event(&mut self, event: &TerminalEvent) {
|
|
|
|
match event {
|
|
|
|
TerminalEvent::Input(Event::Key(Key::Char(c))) => {
|
2021-09-24 23:31:09 +02:00
|
|
|
write!(self.master.lock().unwrap(), "{}", c);
|
2021-09-15 17:47:40 +02:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|