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}; use crate::panes::{PaneId, PositionAndSize};

View file

@ -8,7 +8,7 @@ use crate::panes::grid::Grid;
use crate::panes::terminal_character::{ use crate::panes::terminal_character::{
CharacterStyles, TerminalCharacter, EMPTY_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)] #[derive(PartialEq, Eq, Ord, PartialOrd, Hash, Clone, Copy, Debug)]
pub enum PaneId { pub enum PaneId {

View file

@ -7,7 +7,7 @@ use crate::common::{input::handler::parse_keys, AppInstruction, SenderWithContex
use crate::layout::Layout; use crate::layout::Layout;
use crate::os_input_output::OsApi; use crate::os_input_output::OsApi;
use crate::panes::{PaneId, PositionAndSize, TerminalPane}; 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::utils::shared::adjust_to_size;
use crate::wasm_vm::PluginInstruction; use crate::wasm_vm::PluginInstruction;
use crate::{boundaries::Boundaries, panes::PluginPane}; use crate::{boundaries::Boundaries, panes::PluginPane};

View file

@ -2,7 +2,7 @@
//! the instructions that are sent between threads. //! the instructions that are sent between threads.
use super::{AppInstruction, ASYNCOPENCALLS, OPENCALLS}; use super::{AppInstruction, ASYNCOPENCALLS, OPENCALLS};
use crate::pty_bus::PtyInstruction; use crate::common::pty::PtyInstruction;
use crate::screen::ScreenInstruction; use crate::screen::ScreenInstruction;
use std::fmt::{Display, Error, Formatter}; 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::common::{AppInstruction, SenderWithContext, OPENCALLS};
use crate::errors::ContextType; use crate::errors::ContextType;
use crate::os_input_output::OsApi; use crate::os_input_output::OsApi;
use crate::pty_bus::PtyInstruction; use crate::common::pty::PtyInstruction;
use crate::screen::ScreenInstruction; use crate::screen::ScreenInstruction;
use crate::wasm_vm::PluginInstruction; use crate::wasm_vm::PluginInstruction;
use crate::CommandIsExecuting; use crate::CommandIsExecuting;

View file

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

View file

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

View file

@ -3,15 +3,13 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::os::unix::io::RawFd; use std::os::unix::io::RawFd;
use std::str; use std::str;
use std::sync::mpsc::Receiver;
use super::{AppInstruction, SenderWithContext}; use super::AppInstruction;
use crate::common::Bus; use crate::common::Bus;
use crate::os_input_output::OsApi;
use crate::panes::PositionAndSize; use crate::panes::PositionAndSize;
use crate::pty_bus::{PtyInstruction, VteBytes}; use crate::common::pty::{PtyInstruction, VteBytes};
use crate::tab::Tab; use crate::tab::Tab;
use crate::{errors::ErrorContext, wasm_vm::PluginInstruction}; use crate::wasm_vm::PluginInstruction;
use crate::{layout::Layout, panes::PaneId}; use crate::{layout::Layout, panes::PaneId};
use zellij_tile::data::{Event, ModeInfo, TabInfo}; 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 zellij_tile::data::{Event, EventType, PluginIds};
use super::{ use super::{
pty_bus::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext, pty::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext,
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

View file

@ -14,7 +14,7 @@ use crate::utils::{
}; };
use client::{boundaries, layout, panes, tab}; use client::{boundaries, layout, panes, tab};
use common::{ 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, ApiCommand,
}; };
use std::io::Write; use std::io::Write;