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};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ApiCommand {
pub enum ServerInstruction {
OpenFile(PathBuf),
SplitHorizontally,
SplitVertically,
@ -54,6 +54,13 @@ pub enum ApiCommand {
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
#[derive(Debug, Clone, Default)]
pub struct AppState {
@ -142,7 +149,7 @@ impl IpcSenderWithContext {
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 x = self.sender.write_all(&command);
self.sender.flush();
@ -564,7 +571,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
break;
}
AppInstruction::Error(backtrace) => {
let _ = send_server_instructions.send(ApiCommand::Quit);
let _ = send_server_instructions.send(ServerInstruction::Quit);
//let _ = pty_thread.join();
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
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();
}
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 _ = send_screen_instructions.send(ScreenInstruction::Quit);
screen_thread.join().unwrap();

View file

@ -15,7 +15,7 @@ use crate::layout::Layout;
use crate::os_input_output::OsApi;
use crate::utils::logging::debug_to_file;
use crate::{
common::ApiCommand,
common::ServerInstruction,
errors::{ContextType, ErrorContext},
panes::PaneId,
};
@ -96,7 +96,7 @@ impl VteEventSender {
impl vte::Perform for VteEventSender {
fn print(&mut self, c: char) {
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty(
.send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id,
VteEvent::Print(c),
)))
@ -104,7 +104,7 @@ impl vte::Perform for VteEventSender {
}
fn execute(&mut self, byte: u8) {
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty(
.send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id,
VteEvent::Execute(byte),
)))
@ -115,7 +115,7 @@ impl vte::Perform for VteEventSender {
let params = params.iter().copied().collect();
let intermediates = intermediates.iter().copied().collect();
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty(
.send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id,
VteEvent::Hook(params, intermediates, ignore, c),
)))
@ -124,7 +124,7 @@ impl vte::Perform for VteEventSender {
fn put(&mut self, byte: u8) {
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty(
.send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id,
VteEvent::Put(byte),
)))
@ -133,7 +133,7 @@ impl vte::Perform for VteEventSender {
fn unhook(&mut self) {
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty(
.send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id,
VteEvent::Unhook,
)))
@ -143,7 +143,7 @@ impl vte::Perform for VteEventSender {
fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
let params = params.iter().map(|p| p.to_vec()).collect();
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty(
.send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id,
VteEvent::OscDispatch(params, bell_terminated),
)))
@ -154,7 +154,7 @@ impl vte::Perform for VteEventSender {
let params = params.iter().copied().collect();
let intermediates = intermediates.iter().copied().collect();
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty(
.send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id,
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) {
let intermediates = intermediates.iter().copied().collect();
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Pty(
.send(ServerInstruction::ToScreen(ScreenInstruction::Pty(
self.id,
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 {
pending_render = false;
send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Render))
.send(ServerInstruction::ToScreen(ScreenInstruction::Render))
.unwrap();
last_byte_receive_time = Some(Instant::now());
} else {
@ -244,7 +244,7 @@ fn stream_terminal_bytes(pid: RawFd, os_input: Box<dyn OsApi>, debug: bool) -> J
if pending_render {
pending_render = false;
send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::Render))
.send(ServerInstruction::ToScreen(ScreenInstruction::Render))
.unwrap();
}
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(ApiCommand::ToScreen(ScreenInstruction::Render))
.send(ServerInstruction::ToScreen(ScreenInstruction::Render))
.unwrap();
#[cfg(not(test))]
// 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
// a better solution would be to fix the test fakes, but this will do for now
send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::ClosePane(
.send(ServerInstruction::ToScreen(ScreenInstruction::ClosePane(
PaneId::Terminal(pid),
)))
.unwrap();
@ -302,10 +302,9 @@ impl PtyBus {
new_pane_pids.push(pid_primary);
}
self.send_server_instructions
.send(ApiCommand::ToScreen(ScreenInstruction::ApplyLayout((
layout_path,
new_pane_pids.clone(),
))))
.send(ServerInstruction::ToScreen(ScreenInstruction::ApplyLayout(
(layout_path, new_pane_pids.clone()),
)))
.unwrap();
for id in new_pane_pids {
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
.send_server_instructions
.send(ApiCommand::ClosePluginPane(pid))
.send(ServerInstruction::ClosePluginPane(pid))
.unwrap(),
}
}

View file

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

View file

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