Use Bus type for PTY thread (incomplete)

This commit is contained in:
Kyle Sutherland-Cash 2021-05-01 08:48:58 -07:00
parent ae6192d698
commit bb2369dcb8
11 changed files with 79 additions and 69 deletions

View File

@ -1,4 +1,4 @@
use crate::{common::SenderWithContext, pty_bus::VteBytes, tab::Pane, wasm_vm::PluginInstruction};
use crate::{common::SenderWithContext, common::pty::VteBytes, tab::Pane, wasm_vm::PluginInstruction};
use crate::panes::{PaneId, PositionAndSize};

View File

@ -8,7 +8,7 @@ use crate::panes::grid::Grid;
use crate::panes::terminal_character::{
CharacterStyles, TerminalCharacter, EMPTY_TERMINAL_CHARACTER,
};
use crate::pty_bus::VteBytes;
use crate::common::pty::VteBytes;
#[derive(PartialEq, Eq, Ord, PartialOrd, Hash, Clone, Copy, Debug)]
pub enum PaneId {

View File

@ -7,7 +7,7 @@ use crate::common::{input::handler::parse_keys, AppInstruction, SenderWithContex
use crate::layout::Layout;
use crate::os_input_output::OsApi;
use crate::panes::{PaneId, PositionAndSize, TerminalPane};
use crate::pty_bus::{PtyInstruction, VteBytes};
use crate::common::pty::{PtyInstruction, VteBytes};
use crate::utils::shared::adjust_to_size;
use crate::wasm_vm::PluginInstruction;
use crate::{boundaries::Boundaries, panes::PluginPane};

View File

@ -2,7 +2,7 @@
//! the instructions that are sent between threads.
use super::{AppInstruction, ASYNCOPENCALLS, OPENCALLS};
use crate::pty_bus::PtyInstruction;
use crate::common::pty::PtyInstruction;
use crate::screen::ScreenInstruction;
use std::fmt::{Display, Error, Formatter};

View File

@ -6,7 +6,7 @@ use crate::common::input::config::Config;
use crate::common::{AppInstruction, SenderWithContext, OPENCALLS};
use crate::errors::ContextType;
use crate::os_input_output::OsApi;
use crate::pty_bus::PtyInstruction;
use crate::common::pty::PtyInstruction;
use crate::screen::ScreenInstruction;
use crate::wasm_vm::PluginInstruction;
use crate::CommandIsExecuting;

View File

@ -4,7 +4,7 @@ pub mod input;
pub mod install;
pub mod ipc;
pub mod os_input_output;
pub mod pty_bus;
pub mod pty;
pub mod screen;
pub mod utils;
pub mod wasm_vm;
@ -36,7 +36,7 @@ use errors::{
use input::handler::input_loop;
use install::populate_data_dir;
use os_input_output::OsApi;
use pty_bus::{PtyBus, PtyInstruction};
use pty::{Pty, PtyInstruction};
use screen::{Screen, ScreenInstruction};
use serde::{Deserialize, Serialize};
use utils::consts::ZELLIJ_IPC_PIPE;
@ -193,11 +193,16 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let send_app_instructions =
SenderWithContext::new(SenderType::SyncSender(send_app_instructions));
let mut pty_bus = PtyBus::new(
let pty_bus = Bus::new(
receive_pty_instructions,
send_screen_instructions.clone(),
send_plugin_instructions.clone(),
os_input.clone(),
Some(&send_screen_instructions),
None,
Some(&send_plugin_instructions),
None,
Some(&os_input),
);
let mut pty = Pty::new(
pty_bus,
opts.debug,
);
@ -233,50 +238,59 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let mut command_is_executing = command_is_executing.clone();
send_pty_instructions.send(PtyInstruction::NewTab).unwrap();
move || loop {
let (event, mut err_ctx) = pty_bus
.receive_pty_instructions
let (event, mut err_ctx) = pty
.bus
.receiver
.recv()
.expect("failed to receive event on channel");
err_ctx.add_call(ContextType::Pty(PtyContext::from(&event)));
match event {
PtyInstruction::SpawnTerminal(file_to_open) => {
let pid = pty_bus.spawn_terminal(file_to_open);
pty_bus
.send_screen_instructions
let pid = pty.spawn_terminal(file_to_open);
pty.bus
.to_screen
.as_ref()
.unwrap()
.send(ScreenInstruction::NewPane(PaneId::Terminal(pid)))
.unwrap();
}
PtyInstruction::SpawnTerminalVertically(file_to_open) => {
let pid = pty_bus.spawn_terminal(file_to_open);
pty_bus
.send_screen_instructions
let pid = pty.spawn_terminal(file_to_open);
pty.bus
.to_screen
.as_ref()
.unwrap()
.send(ScreenInstruction::VerticalSplit(PaneId::Terminal(pid)))
.unwrap();
}
PtyInstruction::SpawnTerminalHorizontally(file_to_open) => {
let pid = pty_bus.spawn_terminal(file_to_open);
pty_bus
.send_screen_instructions
let pid = pty.spawn_terminal(file_to_open);
pty.bus
.to_screen
.as_ref()
.unwrap()
.send(ScreenInstruction::HorizontalSplit(PaneId::Terminal(pid)))
.unwrap();
}
PtyInstruction::NewTab => {
if let Some(layout) = maybe_layout.clone() {
pty_bus.spawn_terminals_for_layout(layout);
pty.spawn_terminals_for_layout(layout);
} else {
let pid = pty_bus.spawn_terminal(None);
pty_bus
.send_screen_instructions
let pid = pty.spawn_terminal(None);
pty.bus
.to_screen
.as_ref()
.unwrap()
.send(ScreenInstruction::NewTab(pid))
.unwrap();
}
}
PtyInstruction::ClosePane(id) => {
pty_bus.close_pane(id);
pty.close_pane(id);
command_is_executing.done_closing_pane();
}
PtyInstruction::CloseTab(ids) => {
pty_bus.close_tab(ids);
pty.close_tab(ids);
command_is_executing.done_closing_pane();
}
PtyInstruction::Quit => {
@ -302,12 +316,8 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let max_panes = opts.max_panes;
move || {
let mut screen = Screen::new(
screen_bus,
&full_screen_ws,
max_panes,
ModeInfo::default(),
);
let mut screen =
Screen::new(screen_bus, &full_screen_ws, max_panes, ModeInfo::default());
loop {
let (event, mut err_ctx) = screen
.bus

View File

@ -4,7 +4,6 @@ use ::async_std::task::*;
use ::std::collections::HashMap;
use ::std::os::unix::io::RawFd;
use ::std::pin::*;
use ::std::sync::mpsc::Receiver;
use ::std::time::{Duration, Instant};
use std::path::PathBuf;
@ -12,7 +11,8 @@ use super::{ScreenInstruction, SenderWithContext};
use crate::os_input_output::OsApi;
use crate::utils::logging::debug_to_file;
use crate::{
errors::{get_current_ctx, ContextType, ErrorContext},
common::Bus,
errors::{get_current_ctx, ContextType},
panes::PaneId,
};
use crate::{layout::Layout, wasm_vm::PluginInstruction};
@ -77,12 +77,9 @@ pub enum PtyInstruction {
Quit,
}
pub struct PtyBus {
pub send_screen_instructions: SenderWithContext<ScreenInstruction>,
pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
pub receive_pty_instructions: Receiver<(PtyInstruction, ErrorContext)>,
pub struct Pty {
pub bus: Bus<PtyInstruction>,
pub id_to_child_pid: HashMap<RawFd, RawFd>,
os_input: Box<dyn OsApi>,
debug_to_file: bool,
task_handles: HashMap<RawFd, JoinHandle<()>>,
}
@ -156,31 +153,29 @@ fn stream_terminal_bytes(
})
}
impl PtyBus {
impl Pty {
pub fn new(
receive_pty_instructions: Receiver<(PtyInstruction, ErrorContext)>,
send_screen_instructions: SenderWithContext<ScreenInstruction>,
send_plugin_instructions: SenderWithContext<PluginInstruction>,
os_input: Box<dyn OsApi>,
bus: Bus<PtyInstruction>,
debug_to_file: bool,
) -> Self {
PtyBus {
send_screen_instructions,
send_plugin_instructions,
receive_pty_instructions,
os_input,
Pty {
bus,
id_to_child_pid: HashMap::new(),
debug_to_file,
task_handles: HashMap::new(),
}
}
pub fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) -> RawFd {
let (pid_primary, pid_secondary): (RawFd, RawFd) =
self.os_input.spawn_terminal(file_to_open);
let (pid_primary, pid_secondary): (RawFd, RawFd) = self
.bus
.os_input
.as_mut()
.unwrap()
.spawn_terminal(file_to_open);
let task_handle = stream_terminal_bytes(
pid_primary,
self.send_screen_instructions.clone(),
self.os_input.clone(),
self.bus.to_screen.as_ref().unwrap().clone(),
self.bus.os_input.as_ref().unwrap().clone(),
self.debug_to_file,
);
self.task_handles.insert(pid_primary, task_handle);
@ -191,11 +186,15 @@ impl PtyBus {
let total_panes = layout.total_terminal_panes();
let mut new_pane_pids = vec![];
for _ in 0..total_panes {
let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(None);
let (pid_primary, pid_secondary): (RawFd, RawFd) =
self.bus.os_input.as_mut().unwrap().spawn_terminal(None);
self.id_to_child_pid.insert(pid_primary, pid_secondary);
new_pane_pids.push(pid_primary);
}
self.send_screen_instructions
self.bus
.to_screen
.as_ref()
.unwrap()
.send(ScreenInstruction::ApplyLayout((
layout,
new_pane_pids.clone(),
@ -204,8 +203,8 @@ impl PtyBus {
for id in new_pane_pids {
let task_handle = stream_terminal_bytes(
id,
self.send_screen_instructions.clone(),
self.os_input.clone(),
self.bus.to_screen.as_ref().unwrap().clone(),
self.bus.os_input.as_ref().unwrap().clone(),
self.debug_to_file,
);
self.task_handles.insert(id, task_handle);
@ -216,13 +215,16 @@ impl PtyBus {
PaneId::Terminal(id) => {
let child_pid = self.id_to_child_pid.remove(&id).unwrap();
let handle = self.task_handles.remove(&id).unwrap();
self.os_input.kill(child_pid).unwrap();
self.bus.os_input.as_mut().unwrap().kill(child_pid).unwrap();
task::block_on(async {
handle.cancel().await;
});
}
PaneId::Plugin(pid) => drop(
self.send_plugin_instructions
self.bus
.to_plugin
.as_ref()
.unwrap()
.send(PluginInstruction::Unload(pid)),
),
}
@ -234,7 +236,7 @@ impl PtyBus {
}
}
impl Drop for PtyBus {
impl Drop for Pty {
fn drop(&mut self) {
let child_ids: Vec<RawFd> = self.id_to_child_pid.keys().copied().collect();
for id in child_ids {

View File

@ -3,15 +3,13 @@
use std::collections::BTreeMap;
use std::os::unix::io::RawFd;
use std::str;
use std::sync::mpsc::Receiver;
use super::{AppInstruction, SenderWithContext};
use super::AppInstruction;
use crate::common::Bus;
use crate::os_input_output::OsApi;
use crate::panes::PositionAndSize;
use crate::pty_bus::{PtyInstruction, VteBytes};
use crate::common::pty::{PtyInstruction, VteBytes};
use crate::tab::Tab;
use crate::{errors::ErrorContext, wasm_vm::PluginInstruction};
use crate::wasm_vm::PluginInstruction;
use crate::{layout::Layout, panes::PaneId};
use zellij_tile::data::{Event, ModeInfo, TabInfo};

0
src/common/utils/bus.rs Normal file
View File

View File

@ -12,7 +12,7 @@ use wasmer_wasi::WasiEnv;
use zellij_tile::data::{Event, EventType, PluginIds};
use super::{
pty_bus::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext,
pty::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext,
};
#[derive(Clone, Debug)]

View File

@ -14,7 +14,7 @@ use crate::utils::{
};
use client::{boundaries, layout, panes, tab};
use common::{
command_is_executing, errors, install, os_input_output, pty_bus, screen, start, utils, wasm_vm,
command_is_executing, errors, install, os_input_output, screen, start, utils, wasm_vm,
ApiCommand,
};
use std::io::Write;