Don't commit to master like an idiot... :(

This commit is contained in:
Kyle Sutherland-Cash 2021-05-01 09:32:30 -07:00
parent 2eadcb86a5
commit 69d34c3e09
11 changed files with 178 additions and 209 deletions

View file

@ -1,4 +1,4 @@
use crate::{common::SenderWithContext, common::pty::VteBytes, tab::Pane, wasm_vm::PluginInstruction}; use crate::{common::SenderWithContext, pty_bus::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::common::pty::VteBytes; use crate::pty_bus::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::common::pty::{PtyInstruction, VteBytes}; use crate::pty_bus::{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::common::pty::PtyInstruction; use crate::pty_bus::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::common::pty::PtyInstruction; use crate::pty_bus::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; pub mod pty_bus;
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::{Pty, PtyInstruction}; use pty_bus::{PtyBus, 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;
@ -118,35 +118,6 @@ pub enum AppInstruction {
Error(String), Error(String),
} }
pub struct Bus<T> {
receiver: Option<mpsc::Receiver<(T, ErrorContext)>>,
to_screen: Option<SenderWithContext<ScreenInstruction>>,
to_pty: Option<SenderWithContext<PtyInstruction>>,
to_plugin: Option<SenderWithContext<PluginInstruction>>,
to_app: Option<SenderWithContext<AppInstruction>>,
os_input: Option<Box<dyn OsApi>>,
}
impl<T> Bus<T> {
fn new(
receiver: Option<mpsc::Receiver<(T, ErrorContext)>>,
to_screen: Option<&SenderWithContext<ScreenInstruction>>,
to_pty: Option<&SenderWithContext<PtyInstruction>>,
to_plugin: Option<&SenderWithContext<PluginInstruction>>,
to_app: Option<&SenderWithContext<AppInstruction>>,
os_input: Option<&Box<dyn OsApi>>,
) -> Self {
Bus {
receiver,
to_screen: to_screen.cloned(),
to_pty: to_pty.cloned(),
to_plugin: to_plugin.cloned(),
to_app: to_app.cloned(),
os_input: os_input.cloned(),
}
}
}
/// Start Zellij with the specified [`OsApi`] and command-line arguments. /// Start Zellij with the specified [`OsApi`] and command-line arguments.
// FIXME this should definitely be modularized and split into different functions. // FIXME this should definitely be modularized and split into different functions.
pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) { pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
@ -172,17 +143,34 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let full_screen_ws = os_input.get_terminal_size_using_fd(0); let full_screen_ws = os_input.get_terminal_size_using_fd(0);
os_input.set_raw_mode(0); os_input.set_raw_mode(0);
let (to_screen, from_screen): ChannelWithContext<ScreenInstruction> = mpsc::channel(); let (send_screen_instructions, receive_screen_instructions): ChannelWithContext<
let to_screen = SenderWithContext::new(SenderType::Sender(to_screen)); ScreenInstruction,
> = mpsc::channel();
let send_screen_instructions =
SenderWithContext::new(SenderType::Sender(send_screen_instructions));
let (to_pty, from_pty): ChannelWithContext<PtyInstruction> = mpsc::channel(); let (send_pty_instructions, receive_pty_instructions): ChannelWithContext<PtyInstruction> =
let to_pty = SenderWithContext::new(SenderType::Sender(to_pty)); mpsc::channel();
let send_pty_instructions = SenderWithContext::new(SenderType::Sender(send_pty_instructions));
let (to_plugin, from_plugin): ChannelWithContext<PluginInstruction> = mpsc::channel(); let (send_plugin_instructions, receive_plugin_instructions): ChannelWithContext<
let to_plugin = SenderWithContext::new(SenderType::Sender(to_plugin)); PluginInstruction,
> = mpsc::channel();
let send_plugin_instructions =
SenderWithContext::new(SenderType::Sender(send_plugin_instructions));
let (to_app, from_app): SyncChannelWithContext<AppInstruction> = mpsc::sync_channel(0); let (send_app_instructions, receive_app_instructions): SyncChannelWithContext<AppInstruction> =
let to_app = SenderWithContext::new(SenderType::SyncSender(to_app)); mpsc::sync_channel(0);
let send_app_instructions =
SenderWithContext::new(SenderType::SyncSender(send_app_instructions));
let mut pty_bus = PtyBus::new(
receive_pty_instructions,
send_screen_instructions.clone(),
send_plugin_instructions.clone(),
os_input.clone(),
opts.debug,
);
// Determine and initialize the data directory // Determine and initialize the data directory
let project_dirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); let project_dirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap();
@ -204,85 +192,62 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
#[cfg(not(test))] #[cfg(not(test))]
std::panic::set_hook({ std::panic::set_hook({
use crate::errors::handle_panic; use crate::errors::handle_panic;
let to_app = to_app.clone(); let send_app_instructions = send_app_instructions.clone();
Box::new(move |info| { Box::new(move |info| {
handle_panic(info, &to_app); handle_panic(info, &send_app_instructions);
}) })
}); });
let pty_thread = thread::Builder::new() let pty_thread = thread::Builder::new()
.name("pty".to_string()) .name("pty".to_string())
.spawn({ .spawn({
let mut pty = Pty::new(
Bus::new(
Some(from_pty),
Some(&to_screen),
None,
Some(&to_plugin),
None,
Some(&os_input),
),
opts.debug,
);
let mut command_is_executing = command_is_executing.clone(); let mut command_is_executing = command_is_executing.clone();
to_pty.send(PtyInstruction::NewTab).unwrap(); send_pty_instructions.send(PtyInstruction::NewTab).unwrap();
move || loop { move || loop {
let (event, mut err_ctx) = pty let (event, mut err_ctx) = pty_bus
.bus .receive_pty_instructions
.receiver
.as_ref()
.unwrap()
.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.spawn_terminal(file_to_open); let pid = pty_bus.spawn_terminal(file_to_open);
pty.bus pty_bus
.to_screen .send_screen_instructions
.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.spawn_terminal(file_to_open); let pid = pty_bus.spawn_terminal(file_to_open);
pty.bus pty_bus
.to_screen .send_screen_instructions
.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.spawn_terminal(file_to_open); let pid = pty_bus.spawn_terminal(file_to_open);
pty.bus pty_bus
.to_screen .send_screen_instructions
.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.spawn_terminals_for_layout(layout); pty_bus.spawn_terminals_for_layout(layout);
} else { } else {
let pid = pty.spawn_terminal(None); let pid = pty_bus.spawn_terminal(None);
pty.bus pty_bus
.to_screen .send_screen_instructions
.as_ref()
.unwrap()
.send(ScreenInstruction::NewTab(pid)) .send(ScreenInstruction::NewTab(pid))
.unwrap(); .unwrap();
} }
} }
PtyInstruction::ClosePane(id) => { PtyInstruction::ClosePane(id) => {
pty.close_pane(id); pty_bus.close_pane(id);
command_is_executing.done_closing_pane(); command_is_executing.done_closing_pane();
} }
PtyInstruction::CloseTab(ids) => { PtyInstruction::CloseTab(ids) => {
pty.close_tab(ids); pty_bus.close_tab(ids);
command_is_executing.done_closing_pane(); command_is_executing.done_closing_pane();
} }
PtyInstruction::Quit => { PtyInstruction::Quit => {
@ -297,25 +262,26 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
.name("screen".to_string()) .name("screen".to_string())
.spawn({ .spawn({
let mut command_is_executing = command_is_executing.clone(); let mut command_is_executing = command_is_executing.clone();
let screen_bus = Bus::new( let os_input = os_input.clone();
Some(from_screen), let send_pty_instructions = send_pty_instructions.clone();
None, let send_plugin_instructions = send_plugin_instructions.clone();
Some(&to_pty), let send_app_instructions = send_app_instructions.clone();
Some(&to_plugin),
Some(&to_app),
Some(&os_input),
);
let max_panes = opts.max_panes; let max_panes = opts.max_panes;
move || { move || {
let mut screen = let mut screen = Screen::new(
Screen::new(screen_bus, &full_screen_ws, max_panes, ModeInfo::default()); receive_screen_instructions,
send_pty_instructions,
send_plugin_instructions,
send_app_instructions,
&full_screen_ws,
os_input,
max_panes,
ModeInfo::default(),
);
loop { loop {
let (event, mut err_ctx) = screen let (event, mut err_ctx) = screen
.bus
.receiver .receiver
.as_ref()
.unwrap()
.recv() .recv()
.expect("failed to receive event on channel"); .expect("failed to receive event on channel");
err_ctx.add_call(ContextType::Screen(ScreenContext::from(&event))); err_ctx.add_call(ContextType::Screen(ScreenContext::from(&event)));
@ -498,23 +464,16 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let wasm_thread = thread::Builder::new() let wasm_thread = thread::Builder::new()
.name("wasm".to_string()) .name("wasm".to_string())
.spawn({ .spawn({
let plugin_bus = Bus::new( let send_pty_instructions = send_pty_instructions.clone();
Some(from_plugin), let send_screen_instructions = send_screen_instructions.clone();
Some(&to_screen), let send_app_instructions = send_app_instructions.clone();
Some(&to_pty), let send_plugin_instructions = send_plugin_instructions.clone();
Some(&to_plugin),
Some(&to_app),
None,
);
let store = Store::default(); let store = Store::default();
let mut plugin_id = 0; let mut plugin_id = 0;
let mut plugin_map = HashMap::new(); let mut plugin_map = HashMap::new();
move || loop { move || loop {
let (event, mut err_ctx) = plugin_bus let (event, mut err_ctx) = receive_plugin_instructions
.receiver
.as_ref()
.unwrap()
.recv() .recv()
.expect("failed to receive event on channel"); .expect("failed to receive event on channel");
err_ctx.add_call(ContextType::Plugin(PluginContext::from(&event))); err_ctx.add_call(ContextType::Plugin(PluginContext::from(&event)));
@ -550,10 +509,10 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let plugin_env = PluginEnv { let plugin_env = PluginEnv {
plugin_id, plugin_id,
to_pty: plugin_bus.to_pty.as_ref().unwrap().clone(), send_pty_instructions: send_pty_instructions.clone(),
to_screen: plugin_bus.to_screen.as_ref().unwrap().clone(), send_screen_instructions: send_screen_instructions.clone(),
to_app: plugin_bus.to_app.as_ref().unwrap().clone(), send_app_instructions: send_app_instructions.clone(),
to_plugin: plugin_bus.to_plugin.as_ref().unwrap().clone(), send_plugin_instructions: send_plugin_instructions.clone(),
wasi_env, wasi_env,
subscriptions: Arc::new(Mutex::new(HashSet::new())), subscriptions: Arc::new(Mutex::new(HashSet::new())),
}; };
@ -581,13 +540,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
update.call(&[]).unwrap(); update.call(&[]).unwrap();
} }
} }
drop( drop(send_screen_instructions.send(ScreenInstruction::Render));
plugin_bus
.to_screen
.as_ref()
.unwrap()
.send(ScreenInstruction::Render),
);
} }
PluginInstruction::Render(buf_tx, pid, rows, cols) => { PluginInstruction::Render(buf_tx, pid, rows, cols) => {
let (instance, plugin_env) = plugin_map.get(&pid).unwrap(); let (instance, plugin_env) = plugin_map.get(&pid).unwrap();
@ -611,10 +564,10 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
.name("signal_listener".to_string()) .name("signal_listener".to_string())
.spawn({ .spawn({
let os_input = os_input.clone(); let os_input = os_input.clone();
let to_screen = to_screen.clone(); let send_screen_instructions = send_screen_instructions.clone();
move || { move || {
os_input.receive_sigwinch(Box::new(move || { os_input.receive_sigwinch(Box::new(move || {
let _ = to_screen.send(ScreenInstruction::TerminalResize); let _ = send_screen_instructions.send(ScreenInstruction::TerminalResize);
})); }));
} }
}) })
@ -628,8 +581,8 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
.name("ipc_server".to_string()) .name("ipc_server".to_string())
.spawn({ .spawn({
use std::io::Read; use std::io::Read;
let to_pty = to_pty.clone(); let send_pty_instructions = send_pty_instructions.clone();
let to_screen = to_screen.clone(); let send_screen_instructions = send_screen_instructions.clone();
move || { move || {
std::fs::remove_file(ZELLIJ_IPC_PIPE).ok(); std::fs::remove_file(ZELLIJ_IPC_PIPE).ok();
let listener = std::os::unix::net::UnixListener::bind(ZELLIJ_IPC_PIPE) let listener = std::os::unix::net::UnixListener::bind(ZELLIJ_IPC_PIPE)
@ -649,22 +602,24 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
match &decoded { match &decoded {
ApiCommand::OpenFile(file_name) => { ApiCommand::OpenFile(file_name) => {
let path = PathBuf::from(file_name); let path = PathBuf::from(file_name);
to_pty send_pty_instructions
.send(PtyInstruction::SpawnTerminal(Some(path))) .send(PtyInstruction::SpawnTerminal(Some(path)))
.unwrap(); .unwrap();
} }
ApiCommand::SplitHorizontally => { ApiCommand::SplitHorizontally => {
to_pty send_pty_instructions
.send(PtyInstruction::SpawnTerminalHorizontally(None)) .send(PtyInstruction::SpawnTerminalHorizontally(None))
.unwrap(); .unwrap();
} }
ApiCommand::SplitVertically => { ApiCommand::SplitVertically => {
to_pty send_pty_instructions
.send(PtyInstruction::SpawnTerminalVertically(None)) .send(PtyInstruction::SpawnTerminalVertically(None))
.unwrap(); .unwrap();
} }
ApiCommand::MoveFocus => { ApiCommand::MoveFocus => {
to_screen.send(ScreenInstruction::FocusNextPane).unwrap(); send_screen_instructions
.send(ScreenInstruction::FocusNextPane)
.unwrap();
} }
} }
} }
@ -680,9 +635,9 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let _stdin_thread = thread::Builder::new() let _stdin_thread = thread::Builder::new()
.name("stdin_handler".to_string()) .name("stdin_handler".to_string())
.spawn({ .spawn({
let to_screen = to_screen.clone(); let send_screen_instructions = send_screen_instructions.clone();
let to_pty = to_pty.clone(); let send_pty_instructions = send_pty_instructions.clone();
let to_plugin = to_plugin.clone(); let send_plugin_instructions = send_plugin_instructions.clone();
let os_input = os_input.clone(); let os_input = os_input.clone();
let config = config; let config = config;
move || { move || {
@ -690,17 +645,17 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
os_input, os_input,
config, config,
command_is_executing, command_is_executing,
to_screen, send_screen_instructions,
to_pty, send_pty_instructions,
to_plugin, send_plugin_instructions,
to_app, send_app_instructions,
) )
} }
}); });
#[warn(clippy::never_loop)] #[warn(clippy::never_loop)]
loop { loop {
let (app_instruction, mut err_ctx) = from_app let (app_instruction, mut err_ctx) = receive_app_instructions
.recv() .recv()
.expect("failed to receive app instruction on channel"); .expect("failed to receive app instruction on channel");
@ -710,11 +665,11 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
break; break;
} }
AppInstruction::Error(backtrace) => { AppInstruction::Error(backtrace) => {
let _ = to_screen.send(ScreenInstruction::Quit); let _ = send_screen_instructions.send(ScreenInstruction::Quit);
let _ = screen_thread.join(); let _ = screen_thread.join();
let _ = to_pty.send(PtyInstruction::Quit); let _ = send_pty_instructions.send(PtyInstruction::Quit);
let _ = pty_thread.join(); let _ = pty_thread.join();
let _ = to_plugin.send(PluginInstruction::Quit); let _ = send_plugin_instructions.send(PluginInstruction::Quit);
let _ = wasm_thread.join(); let _ = wasm_thread.join();
os_input.unset_raw_mode(0); os_input.unset_raw_mode(0);
let goto_start_of_last_line = format!("\u{1b}[{};{}H", full_screen_ws.rows, 1); let goto_start_of_last_line = format!("\u{1b}[{};{}H", full_screen_ws.rows, 1);
@ -732,11 +687,11 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
} }
} }
let _ = to_pty.send(PtyInstruction::Quit); let _ = send_pty_instructions.send(PtyInstruction::Quit);
pty_thread.join().unwrap(); pty_thread.join().unwrap();
let _ = to_screen.send(ScreenInstruction::Quit); let _ = send_screen_instructions.send(ScreenInstruction::Quit);
screen_thread.join().unwrap(); screen_thread.join().unwrap();
let _ = to_plugin.send(PluginInstruction::Quit); let _ = send_plugin_instructions.send(PluginInstruction::Quit);
wasm_thread.join().unwrap(); wasm_thread.join().unwrap();
// cleanup(); // cleanup();

View file

@ -4,6 +4,7 @@ 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;
@ -11,8 +12,7 @@ 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::{
common::Bus, errors::{get_current_ctx, ContextType, ErrorContext},
errors::{get_current_ctx, ContextType},
panes::PaneId, panes::PaneId,
}; };
use crate::{layout::Layout, wasm_vm::PluginInstruction}; use crate::{layout::Layout, wasm_vm::PluginInstruction};
@ -77,9 +77,12 @@ pub enum PtyInstruction {
Quit, Quit,
} }
pub struct Pty { pub struct PtyBus {
pub bus: Bus<PtyInstruction>, pub send_screen_instructions: SenderWithContext<ScreenInstruction>,
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<()>>,
} }
@ -153,29 +156,31 @@ fn stream_terminal_bytes(
}) })
} }
impl Pty { impl PtyBus {
pub fn new( pub fn new(
bus: Bus<PtyInstruction>, receive_pty_instructions: Receiver<(PtyInstruction, ErrorContext)>,
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 {
Pty { PtyBus {
bus, send_screen_instructions,
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) = self let (pid_primary, pid_secondary): (RawFd, RawFd) =
.bus self.os_input.spawn_terminal(file_to_open);
.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.bus.to_screen.as_ref().unwrap().clone(), self.send_screen_instructions.clone(),
self.bus.os_input.as_ref().unwrap().clone(), self.os_input.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);
@ -186,15 +191,11 @@ impl Pty {
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) = let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(None);
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.bus self.send_screen_instructions
.to_screen
.as_ref()
.unwrap()
.send(ScreenInstruction::ApplyLayout(( .send(ScreenInstruction::ApplyLayout((
layout, layout,
new_pane_pids.clone(), new_pane_pids.clone(),
@ -203,8 +204,8 @@ impl Pty {
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.bus.to_screen.as_ref().unwrap().clone(), self.send_screen_instructions.clone(),
self.bus.os_input.as_ref().unwrap().clone(), self.os_input.clone(),
self.debug_to_file, self.debug_to_file,
); );
self.task_handles.insert(id, task_handle); self.task_handles.insert(id, task_handle);
@ -215,16 +216,13 @@ impl Pty {
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.bus.os_input.as_mut().unwrap().kill(child_pid).unwrap(); self.os_input.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.bus self.send_plugin_instructions
.to_plugin
.as_ref()
.unwrap()
.send(PluginInstruction::Unload(pid)), .send(PluginInstruction::Unload(pid)),
), ),
} }
@ -236,7 +234,7 @@ impl Pty {
} }
} }
impl Drop for Pty { impl Drop for PtyBus {
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,13 +3,14 @@
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; use super::{AppInstruction, SenderWithContext};
use crate::common::Bus; use crate::os_input_output::OsApi;
use crate::panes::PositionAndSize; use crate::panes::PositionAndSize;
use crate::common::pty::{PtyInstruction, VteBytes}; use crate::pty_bus::{PtyInstruction, VteBytes};
use crate::tab::Tab; use crate::tab::Tab;
use crate::wasm_vm::PluginInstruction; use crate::{errors::ErrorContext, 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};
@ -61,33 +62,51 @@ pub enum ScreenInstruction {
/// A [`Screen`] holds multiple [`Tab`]s, each one holding multiple [`panes`](crate::client::panes). /// A [`Screen`] holds multiple [`Tab`]s, each one holding multiple [`panes`](crate::client::panes).
/// It only directly controls which tab is active, delegating the rest to the individual `Tab`. /// It only directly controls which tab is active, delegating the rest to the individual `Tab`.
pub struct Screen { pub struct Screen {
/// A Bus for sending and receiving messages with the other threads. /// A [`ScreenInstruction`] and [`ErrorContext`] receiver.
pub bus: Bus<ScreenInstruction>, pub receiver: Receiver<(ScreenInstruction, ErrorContext)>,
/// An optional maximal amount of panes allowed per [`Tab`] in this [`Screen`] instance. /// An optional maximal amount of panes allowed per [`Tab`] in this [`Screen`] instance.
max_panes: Option<usize>, max_panes: Option<usize>,
/// A map between this [`Screen`]'s tabs and their ID/key. /// A map between this [`Screen`]'s tabs and their ID/key.
tabs: BTreeMap<usize, Tab>, tabs: BTreeMap<usize, Tab>,
/// A [`PtyInstruction`] and [`ErrorContext`] sender.
pub send_pty_instructions: SenderWithContext<PtyInstruction>,
/// A [`PluginInstruction`] and [`ErrorContext`] sender.
pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
/// An [`AppInstruction`] and [`ErrorContext`] sender.
pub send_app_instructions: SenderWithContext<AppInstruction>,
/// The full size of this [`Screen`]. /// The full size of this [`Screen`].
full_screen_ws: PositionAndSize, full_screen_ws: PositionAndSize,
/// The index of this [`Screen`]'s active [`Tab`]. /// The index of this [`Screen`]'s active [`Tab`].
active_tab_index: Option<usize>, active_tab_index: Option<usize>,
/// The [`OsApi`] this [`Screen`] uses.
os_api: Box<dyn OsApi>,
mode_info: ModeInfo, mode_info: ModeInfo,
} }
impl Screen { impl Screen {
// FIXME: This lint needs actual fixing! Maybe by bundling the Senders
/// Creates and returns a new [`Screen`]. /// Creates and returns a new [`Screen`].
#[allow(clippy::too_many_arguments)]
pub fn new( pub fn new(
bus: Bus<ScreenInstruction>, receive_screen_instructions: Receiver<(ScreenInstruction, ErrorContext)>,
send_pty_instructions: SenderWithContext<PtyInstruction>,
send_plugin_instructions: SenderWithContext<PluginInstruction>,
send_app_instructions: SenderWithContext<AppInstruction>,
full_screen_ws: &PositionAndSize, full_screen_ws: &PositionAndSize,
os_api: Box<dyn OsApi>,
max_panes: Option<usize>, max_panes: Option<usize>,
mode_info: ModeInfo, mode_info: ModeInfo,
) -> Self { ) -> Self {
Screen { Screen {
bus, receiver: receive_screen_instructions,
max_panes, max_panes,
send_pty_instructions,
send_plugin_instructions,
send_app_instructions,
full_screen_ws: *full_screen_ws, full_screen_ws: *full_screen_ws,
active_tab_index: None, active_tab_index: None,
tabs: BTreeMap::new(), tabs: BTreeMap::new(),
os_api,
mode_info, mode_info,
} }
} }
@ -102,10 +121,10 @@ impl Screen {
position, position,
String::new(), String::new(),
&self.full_screen_ws, &self.full_screen_ws,
self.bus.os_input.as_ref().unwrap().clone(), self.os_api.clone(),
self.bus.to_pty.as_ref().unwrap().clone(), self.send_pty_instructions.clone(),
self.bus.to_plugin.as_ref().unwrap().clone(), self.send_plugin_instructions.clone(),
self.bus.to_app.as_ref().unwrap().clone(), self.send_app_instructions.clone(),
self.max_panes, self.max_panes,
Some(PaneId::Terminal(pane_id)), Some(PaneId::Terminal(pane_id)),
self.mode_info.clone(), self.mode_info.clone(),
@ -188,14 +207,11 @@ impl Screen {
// because this might be happening when the app is closing, at which point the pty thread // because this might be happening when the app is closing, at which point the pty thread
// has already closed and this would result in an error // has already closed and this would result in an error
let _ = self let _ = self
.bus .send_pty_instructions
.to_pty
.as_ref()
.unwrap()
.send(PtyInstruction::CloseTab(pane_ids)); .send(PtyInstruction::CloseTab(pane_ids));
if self.tabs.is_empty() { if self.tabs.is_empty() {
self.active_tab_index = None; self.active_tab_index = None;
self.bus.to_app.as_ref().unwrap() self.send_app_instructions
.send(AppInstruction::Exit) .send(AppInstruction::Exit)
.unwrap(); .unwrap();
} else { } else {
@ -209,7 +225,7 @@ impl Screen {
} }
pub fn resize_to_screen(&mut self) { pub fn resize_to_screen(&mut self) {
let new_screen_size = self.bus.os_input.as_ref().unwrap().get_terminal_size_using_fd(0); let new_screen_size = self.os_api.get_terminal_size_using_fd(0);
self.full_screen_ws = new_screen_size; self.full_screen_ws = new_screen_size;
for (_, tab) in self.tabs.iter_mut() { for (_, tab) in self.tabs.iter_mut() {
tab.resize_whole_tab(new_screen_size); tab.resize_whole_tab(new_screen_size);
@ -260,10 +276,10 @@ impl Screen {
position, position,
String::new(), String::new(),
&self.full_screen_ws, &self.full_screen_ws,
self.bus.os_input.as_ref().unwrap().clone(), self.os_api.clone(),
self.bus.to_pty.as_ref().unwrap().clone(), self.send_pty_instructions.clone(),
self.bus.to_plugin.as_ref().unwrap().clone(), self.send_plugin_instructions.clone(),
self.bus.to_app.as_ref().unwrap().clone(), self.send_app_instructions.clone(),
self.max_panes, self.max_panes,
None, None,
self.mode_info.clone(), self.mode_info.clone(),
@ -285,7 +301,7 @@ impl Screen {
is_sync_panes_active: tab.is_sync_panes_active(), is_sync_panes_active: tab.is_sync_panes_active(),
}); });
} }
self.bus.to_plugin.as_ref().unwrap() self.send_plugin_instructions
.send(PluginInstruction::Update(None, Event::TabUpdate(tab_data))) .send(PluginInstruction::Update(None, Event::TabUpdate(tab_data)))
.unwrap(); .unwrap();
} }

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::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext, pty_bus::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext,
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -28,10 +28,10 @@ pub enum PluginInstruction {
pub struct PluginEnv { pub struct PluginEnv {
pub plugin_id: u32, pub plugin_id: u32,
// FIXME: This should be a big bundle of all of the channels // FIXME: This should be a big bundle of all of the channels
pub to_screen: SenderWithContext<ScreenInstruction>, pub send_screen_instructions: SenderWithContext<ScreenInstruction>,
pub to_app: SenderWithContext<AppInstruction>, pub send_app_instructions: SenderWithContext<AppInstruction>,
pub to_pty: SenderWithContext<PtyInstruction>, pub send_pty_instructions: SenderWithContext<PtyInstruction>,
pub to_plugin: SenderWithContext<PluginInstruction>, pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
pub wasi_env: WasiEnv, pub wasi_env: WasiEnv,
pub subscriptions: Arc<Mutex<HashSet<EventType>>>, pub subscriptions: Arc<Mutex<HashSet<EventType>>>,
} }
@ -77,7 +77,7 @@ fn host_unsubscribe(plugin_env: &PluginEnv) {
fn host_set_selectable(plugin_env: &PluginEnv, selectable: i32) { fn host_set_selectable(plugin_env: &PluginEnv, selectable: i32) {
let selectable = selectable != 0; let selectable = selectable != 0;
plugin_env plugin_env
.to_screen .send_screen_instructions
.send(ScreenInstruction::SetSelectable( .send(ScreenInstruction::SetSelectable(
PaneId::Plugin(plugin_env.plugin_id), PaneId::Plugin(plugin_env.plugin_id),
selectable, selectable,
@ -88,7 +88,7 @@ fn host_set_selectable(plugin_env: &PluginEnv, selectable: i32) {
fn host_set_max_height(plugin_env: &PluginEnv, max_height: i32) { fn host_set_max_height(plugin_env: &PluginEnv, max_height: i32) {
let max_height = max_height as usize; let max_height = max_height as usize;
plugin_env plugin_env
.to_screen .send_screen_instructions
.send(ScreenInstruction::SetMaxHeight( .send(ScreenInstruction::SetMaxHeight(
PaneId::Plugin(plugin_env.plugin_id), PaneId::Plugin(plugin_env.plugin_id),
max_height, max_height,
@ -99,7 +99,7 @@ fn host_set_max_height(plugin_env: &PluginEnv, max_height: i32) {
fn host_set_invisible_borders(plugin_env: &PluginEnv, invisible_borders: i32) { fn host_set_invisible_borders(plugin_env: &PluginEnv, invisible_borders: i32) {
let invisible_borders = invisible_borders != 0; let invisible_borders = invisible_borders != 0;
plugin_env plugin_env
.to_screen .send_screen_instructions
.send(ScreenInstruction::SetInvisibleBorders( .send(ScreenInstruction::SetInvisibleBorders(
PaneId::Plugin(plugin_env.plugin_id), PaneId::Plugin(plugin_env.plugin_id),
invisible_borders, invisible_borders,
@ -118,7 +118,7 @@ fn host_get_plugin_ids(plugin_env: &PluginEnv) {
fn host_open_file(plugin_env: &PluginEnv) { fn host_open_file(plugin_env: &PluginEnv) {
let path: PathBuf = wasi_read_object(&plugin_env.wasi_env); let path: PathBuf = wasi_read_object(&plugin_env.wasi_env);
plugin_env plugin_env
.to_pty .send_pty_instructions
.send(PtyInstruction::SpawnTerminal(Some(path))) .send(PtyInstruction::SpawnTerminal(Some(path)))
.unwrap(); .unwrap();
} }
@ -133,7 +133,7 @@ fn host_set_timeout(plugin_env: &PluginEnv, secs: f64) {
// timers as we'd like. // timers as we'd like.
// //
// But that's a lot of code, and this is a few lines: // But that's a lot of code, and this is a few lines:
let send_plugin_instructions = plugin_env.to_plugin.clone(); let send_plugin_instructions = plugin_env.send_plugin_instructions.clone();
let update_target = Some(plugin_env.plugin_id); let update_target = Some(plugin_env.plugin_id);
thread::spawn(move || { thread::spawn(move || {
let start_time = Instant::now(); let start_time = Instant::now();

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, screen, start, utils, wasm_vm, command_is_executing, errors, install, os_input_output, pty_bus, screen, start, utils, wasm_vm,
ApiCommand, ApiCommand,
}; };
use std::io::Write; use std::io::Write;