Introduce serlializable ClientInstruction and Rename ApiCommand

This commit is contained in:
Kunal Mohan 2021-02-18 10:17:01 +05:30
parent 858e48c6aa
commit 1ee86f9a77
4 changed files with 54 additions and 48 deletions

View file

@ -43,7 +43,7 @@ use wasmer_wasi::{Pipe, WasiState};
use zellij_tile::data::{EventType, InputMode, ModeInfo}; use zellij_tile::data::{EventType, InputMode, ModeInfo};
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ApiCommand { pub enum ServerInstruction {
OpenFile(PathBuf), OpenFile(PathBuf),
SplitHorizontally, SplitHorizontally,
SplitVertically, SplitVertically,
@ -54,6 +54,13 @@ pub enum ApiCommand {
Quit, Quit,
} }
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ClientInstruction {
ToScreen(ScreenInstruction),
ClosePluginPane(u32),
Error(String),
}
// FIXME: It would be good to add some more things to this over time // FIXME: It would be good to add some more things to this over time
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct AppState { pub struct AppState {
@ -142,7 +149,7 @@ impl IpcSenderWithContext {
self.err_ctx = ctx; self.err_ctx = ctx;
} }
pub fn send(&mut self, msg: ApiCommand) -> std::io::Result<()> { pub fn send(&mut self, msg: ServerInstruction) -> std::io::Result<()> {
let command = bincode::serialize(&(self.err_ctx, msg)).unwrap(); let command = bincode::serialize(&(self.err_ctx, msg)).unwrap();
let x = self.sender.write_all(&command); let x = self.sender.write_all(&command);
self.sender.flush(); self.sender.flush();
@ -564,7 +571,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
break; break;
} }
AppInstruction::Error(backtrace) => { AppInstruction::Error(backtrace) => {
let _ = send_server_instructions.send(ApiCommand::Quit); let _ = send_server_instructions.send(ServerInstruction::Quit);
//let _ = pty_thread.join(); //let _ = pty_thread.join();
let _ = send_screen_instructions.send(ScreenInstruction::Quit); let _ = send_screen_instructions.send(ScreenInstruction::Quit);
let _ = screen_thread.join(); let _ = screen_thread.join();
@ -586,12 +593,12 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
send_plugin_instructions.send(instruction).unwrap(); send_plugin_instructions.send(instruction).unwrap();
} }
AppInstruction::ToPty(instruction) => { AppInstruction::ToPty(instruction) => {
let _ = send_server_instructions.send(ApiCommand::ToPty(instruction)); let _ = send_server_instructions.send(ServerInstruction::ToPty(instruction));
} }
} }
} }
let _ = send_server_instructions.send(ApiCommand::Quit); let _ = send_server_instructions.send(ServerInstruction::Quit);
//let _ = pty_thread.join().unwrap(); //let _ = pty_thread.join().unwrap();
let _ = send_screen_instructions.send(ScreenInstruction::Quit); let _ = send_screen_instructions.send(ScreenInstruction::Quit);
screen_thread.join().unwrap(); screen_thread.join().unwrap();

View file

@ -15,7 +15,7 @@ use crate::layout::Layout;
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::ApiCommand, common::ServerInstruction,
errors::{ContextType, ErrorContext}, errors::{ContextType, ErrorContext},
panes::PaneId, panes::PaneId,
}; };
@ -96,7 +96,7 @@ impl VteEventSender {
impl vte::Perform for VteEventSender { impl vte::Perform for VteEventSender {
fn print(&mut self, c: char) { fn print(&mut self, c: char) {
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty( .send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id, self.id,
VteEvent::Print(c), VteEvent::Print(c),
))) )))
@ -104,7 +104,7 @@ impl vte::Perform for VteEventSender {
} }
fn execute(&mut self, byte: u8) { fn execute(&mut self, byte: u8) {
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty( .send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id, self.id,
VteEvent::Execute(byte), VteEvent::Execute(byte),
))) )))
@ -115,7 +115,7 @@ impl vte::Perform for VteEventSender {
let params = params.iter().copied().collect(); let params = params.iter().copied().collect();
let intermediates = intermediates.iter().copied().collect(); let intermediates = intermediates.iter().copied().collect();
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty( .send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id, self.id,
VteEvent::Hook(params, intermediates, ignore, c), VteEvent::Hook(params, intermediates, ignore, c),
))) )))
@ -124,7 +124,7 @@ impl vte::Perform for VteEventSender {
fn put(&mut self, byte: u8) { fn put(&mut self, byte: u8) {
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty( .send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id, self.id,
VteEvent::Put(byte), VteEvent::Put(byte),
))) )))
@ -133,7 +133,7 @@ impl vte::Perform for VteEventSender {
fn unhook(&mut self) { fn unhook(&mut self) {
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty( .send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id, self.id,
VteEvent::Unhook, VteEvent::Unhook,
))) )))
@ -143,7 +143,7 @@ impl vte::Perform for VteEventSender {
fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) { fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
let params = params.iter().map(|p| p.to_vec()).collect(); let params = params.iter().map(|p| p.to_vec()).collect();
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty( .send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id, self.id,
VteEvent::OscDispatch(params, bell_terminated), VteEvent::OscDispatch(params, bell_terminated),
))) )))
@ -154,7 +154,7 @@ impl vte::Perform for VteEventSender {
let params = params.iter().copied().collect(); let params = params.iter().copied().collect();
let intermediates = intermediates.iter().copied().collect(); let intermediates = intermediates.iter().copied().collect();
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty( .send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id, self.id,
VteEvent::CsiDispatch(params, intermediates, ignore, c), VteEvent::CsiDispatch(params, intermediates, ignore, c),
))) )))
@ -164,7 +164,7 @@ impl vte::Perform for VteEventSender {
fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) { fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) {
let intermediates = intermediates.iter().copied().collect(); let intermediates = intermediates.iter().copied().collect();
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty( .send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id, self.id,
VteEvent::EscDispatch(intermediates, ignore, byte), VteEvent::EscDispatch(intermediates, ignore, byte),
))) )))
@ -228,7 +228,7 @@ fn stream_terminal_bytes(pid: RawFd, os_input: Box<dyn OsApi>, debug: bool) -> J
if receive_time.elapsed() > max_render_pause { if receive_time.elapsed() > max_render_pause {
pending_render = false; pending_render = false;
send_server_instructions send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Render)) .send(ServerInstruction::ToScreen(ScreenInstruction::Render))
.unwrap(); .unwrap();
last_byte_receive_time = Some(Instant::now()); last_byte_receive_time = Some(Instant::now());
} else { } else {
@ -244,7 +244,7 @@ fn stream_terminal_bytes(pid: RawFd, os_input: Box<dyn OsApi>, debug: bool) -> J
if pending_render { if pending_render {
pending_render = false; pending_render = false;
send_server_instructions send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Render)) .send(ServerInstruction::ToScreen(ScreenInstruction::Render))
.unwrap(); .unwrap();
} }
last_byte_receive_time = None; last_byte_receive_time = None;
@ -252,14 +252,14 @@ fn stream_terminal_bytes(pid: RawFd, os_input: Box<dyn OsApi>, debug: bool) -> J
} }
} }
send_server_instructions send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Render)) .send(ServerInstruction::ToScreen(ScreenInstruction::Render))
.unwrap(); .unwrap();
#[cfg(not(test))] #[cfg(not(test))]
// this is a little hacky, and is because the tests end the file as soon as // this is a little hacky, and is because the tests end the file as soon as
// we read everything, rather than hanging until there is new data // we read everything, rather than hanging until there is new data
// a better solution would be to fix the test fakes, but this will do for now // a better solution would be to fix the test fakes, but this will do for now
send_server_instructions send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::ClosePane( .send(ServerInstruction::ToScreen(ScreenInstruction::ClosePane(
PaneId::Terminal(pid), PaneId::Terminal(pid),
))) )))
.unwrap(); .unwrap();
@ -302,10 +302,9 @@ impl PtyBus {
new_pane_pids.push(pid_primary); new_pane_pids.push(pid_primary);
} }
self.send_server_instructions self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::ApplyLayout(( .send(ServerInstruction::ToScreen(ScreenInstruction::ApplyLayout(
layout_path, (layout_path, new_pane_pids.clone()),
new_pane_pids.clone(), )))
))))
.unwrap(); .unwrap();
for id in new_pane_pids { for id in new_pane_pids {
let task_handle = stream_terminal_bytes(id, self.os_input.clone(), self.debug_to_file); let task_handle = stream_terminal_bytes(id, self.os_input.clone(), self.debug_to_file);
@ -324,7 +323,7 @@ impl PtyBus {
} }
PaneId::Plugin(pid) => self PaneId::Plugin(pid) => self
.send_server_instructions .send_server_instructions
.send(ApiCommand::ClosePluginPane(pid)) .send(ServerInstruction::ClosePluginPane(pid))
.unwrap(), .unwrap(),
} }
} }

View file

@ -6,7 +6,7 @@ mod server;
use client::{boundaries, layout, panes, tab}; use client::{boundaries, layout, panes, tab};
use common::{ use common::{
command_is_executing, errors, ipc, os_input_output, pty_bus, screen, start, utils, wasm_vm, command_is_executing, errors, ipc, os_input_output, pty_bus, screen, start, utils, wasm_vm,
ApiCommand, IpcSenderWithContext, IpcSenderWithContext, ServerInstruction,
}; };
use directories_next::ProjectDirs; use directories_next::ProjectDirs;
@ -64,13 +64,13 @@ pub fn main() {
'h' => { 'h' => {
let mut send_server_instructions = IpcSenderWithContext::new(); let mut send_server_instructions = IpcSenderWithContext::new();
send_server_instructions send_server_instructions
.send(ApiCommand::SplitHorizontally) .send(ServerInstruction::SplitHorizontally)
.unwrap(); .unwrap();
} }
'v' => { 'v' => {
let mut send_server_instructions = IpcSenderWithContext::new(); let mut send_server_instructions = IpcSenderWithContext::new();
send_server_instructions send_server_instructions
.send(ApiCommand::SplitVertically) .send(ServerInstruction::SplitVertically)
.unwrap(); .unwrap();
} }
_ => {} _ => {}
@ -78,12 +78,12 @@ pub fn main() {
} else if opts.move_focus { } else if opts.move_focus {
let mut send_server_instructions = IpcSenderWithContext::new(); let mut send_server_instructions = IpcSenderWithContext::new();
send_server_instructions send_server_instructions
.send(ApiCommand::MoveFocus) .send(ServerInstruction::MoveFocus)
.unwrap(); .unwrap();
} else if let Some(file_to_open) = opts.open_file { } else if let Some(file_to_open) = opts.open_file {
let mut send_server_instructions = IpcSenderWithContext::new(); let mut send_server_instructions = IpcSenderWithContext::new();
send_server_instructions send_server_instructions
.send(ApiCommand::OpenFile(file_to_open)) .send(ServerInstruction::OpenFile(file_to_open))
.unwrap(); .unwrap();
} else { } else {
let os_input = get_os_input(); let os_input = get_os_input();

View file

@ -1,8 +1,8 @@
use crate::cli::CliArgs; use crate::cli::CliArgs;
use crate::command_is_executing::CommandIsExecuting; use crate::command_is_executing::CommandIsExecuting;
use crate::common::{ use crate::common::{
ApiCommand, AppInstruction, ChannelWithContext, IpcSenderWithContext, SenderType, AppInstruction, ChannelWithContext, IpcSenderWithContext, SenderType, SenderWithContext,
SenderWithContext, ServerInstruction,
}; };
use crate::errors::{ContextType, ErrorContext, PtyContext}; use crate::errors::{ContextType, ErrorContext, PtyContext};
use crate::layout::Layout; use crate::layout::Layout;
@ -67,7 +67,7 @@ pub fn start_server(
let pid = pty_bus.spawn_terminal(file_to_open); let pid = pty_bus.spawn_terminal(file_to_open);
pty_bus pty_bus
.send_server_instructions .send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::NewPane( .send(ServerInstruction::ToScreen(ScreenInstruction::NewPane(
PaneId::Terminal(pid), PaneId::Terminal(pid),
))) )))
.unwrap(); .unwrap();
@ -76,18 +76,18 @@ pub fn start_server(
let pid = pty_bus.spawn_terminal(file_to_open); let pid = pty_bus.spawn_terminal(file_to_open);
pty_bus pty_bus
.send_server_instructions .send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::VerticalSplit( .send(ServerInstruction::ToScreen(
PaneId::Terminal(pid), 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_bus.spawn_terminal(file_to_open);
pty_bus pty_bus
.send_server_instructions .send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::HorizontalSplit( .send(ServerInstruction::ToScreen(
PaneId::Terminal(pid), ScreenInstruction::HorizontalSplit(PaneId::Terminal(pid)),
))) ))
.unwrap(); .unwrap();
} }
PtyInstruction::NewTab => { PtyInstruction::NewTab => {
@ -97,7 +97,7 @@ pub fn start_server(
let pid = pty_bus.spawn_terminal(None); let pid = pty_bus.spawn_terminal(None);
pty_bus pty_bus
.send_server_instructions .send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::NewTab(pid))) .send(ServerInstruction::ToScreen(ScreenInstruction::NewTab(pid)))
.unwrap(); .unwrap();
} }
} }
@ -164,51 +164,51 @@ fn handle_stream(
let bytes = reader let bytes = reader
.read(&mut buffer) .read(&mut buffer)
.expect("failed to parse ipc message"); .expect("failed to parse ipc message");
let (mut err_ctx, decoded): (ErrorContext, ApiCommand) = let (mut err_ctx, decoded): (ErrorContext, ServerInstruction) =
match bincode::deserialize(&buffer[..bytes]) { match bincode::deserialize(&buffer[..bytes]) {
Ok(d) => d, Ok(d) => d,
Err(e) => break, Err(_) => break,
}; };
err_ctx.add_call(ContextType::IPCServer); err_ctx.add_call(ContextType::IPCServer);
send_pty_instructions.update(err_ctx); send_pty_instructions.update(err_ctx);
send_app_instructions.update(err_ctx); send_app_instructions.update(err_ctx);
match decoded { match decoded {
ApiCommand::OpenFile(file_name) => { ServerInstruction::OpenFile(file_name) => {
let path = PathBuf::from(file_name); let path = PathBuf::from(file_name);
send_pty_instructions send_pty_instructions
.send(PtyInstruction::SpawnTerminal(Some(path))) .send(PtyInstruction::SpawnTerminal(Some(path)))
.unwrap(); .unwrap();
} }
ApiCommand::SplitHorizontally => { ServerInstruction::SplitHorizontally => {
send_pty_instructions send_pty_instructions
.send(PtyInstruction::SpawnTerminalHorizontally(None)) .send(PtyInstruction::SpawnTerminalHorizontally(None))
.unwrap(); .unwrap();
} }
ApiCommand::SplitVertically => { ServerInstruction::SplitVertically => {
send_pty_instructions send_pty_instructions
.send(PtyInstruction::SpawnTerminalVertically(None)) .send(PtyInstruction::SpawnTerminalVertically(None))
.unwrap(); .unwrap();
} }
ApiCommand::MoveFocus => { ServerInstruction::MoveFocus => {
send_app_instructions send_app_instructions
.send(AppInstruction::ToScreen(ScreenInstruction::MoveFocus)) .send(AppInstruction::ToScreen(ScreenInstruction::MoveFocus))
.unwrap(); .unwrap();
} }
ApiCommand::ToPty(instruction) => { ServerInstruction::ToPty(instruction) => {
send_pty_instructions.send(instruction).unwrap(); send_pty_instructions.send(instruction).unwrap();
} }
ApiCommand::ToScreen(instruction) => { ServerInstruction::ToScreen(instruction) => {
send_app_instructions send_app_instructions
.send(AppInstruction::ToScreen(instruction)) .send(AppInstruction::ToScreen(instruction))
.unwrap(); .unwrap();
} }
ApiCommand::ClosePluginPane(pid) => { ServerInstruction::ClosePluginPane(pid) => {
send_app_instructions send_app_instructions
.send(AppInstruction::ToPlugin(PluginInstruction::Unload(pid))) .send(AppInstruction::ToPlugin(PluginInstruction::Unload(pid)))
.unwrap(); .unwrap();
} }
ApiCommand::Quit => { ServerInstruction::Quit => {
let _ = send_pty_instructions.send(PtyInstruction::Quit); let _ = send_pty_instructions.send(PtyInstruction::Quit);
break; break;
} }