Refactor CommandIsExecuting to have a single variant

This commit is contained in:
Kunal Mohan 2021-04-28 21:13:19 +05:30
parent b7aa3fc21a
commit 42079f8d04
6 changed files with 45 additions and 112 deletions

View file

@ -25,9 +25,7 @@ use crate::server::ServerInstruction;
pub enum ClientInstruction { pub enum ClientInstruction {
Error(String), Error(String),
Render(Option<String>), Render(Option<String>),
DoneClosingPane, UnblockInputThread,
DoneOpeningNewPane,
DoneUpdatingTabs,
Exit, Exit,
} }
@ -157,9 +155,9 @@ pub fn start_client(mut os_input: Box<dyn ClientOsApi>, opts: CliArgs) {
.expect("cannot write to stdout"); .expect("cannot write to stdout");
stdout.flush().expect("could not flush"); stdout.flush().expect("could not flush");
} }
ClientInstruction::DoneClosingPane => command_is_executing.done_closing_pane(), ClientInstruction::UnblockInputThread => {
ClientInstruction::DoneOpeningNewPane => command_is_executing.done_opening_new_pane(), command_is_executing.unblock_input_thread();
ClientInstruction::DoneUpdatingTabs => command_is_executing.done_updating_tabs(), }
} }
} }

View file

@ -3,71 +3,31 @@ use std::sync::{Arc, Condvar, Mutex};
#[derive(Clone)] #[derive(Clone)]
pub struct CommandIsExecuting { pub struct CommandIsExecuting {
opening_new_pane: Arc<(Mutex<bool>, Condvar)>, input_thread: Arc<(Mutex<bool>, Condvar)>,
closing_pane: Arc<(Mutex<bool>, Condvar)>,
updating_tabs: Arc<(Mutex<bool>, Condvar)>,
} }
impl CommandIsExecuting { impl CommandIsExecuting {
pub fn new() -> Self { pub fn new() -> Self {
CommandIsExecuting { CommandIsExecuting {
opening_new_pane: Arc::new((Mutex::new(false), Condvar::new())), input_thread: Arc::new((Mutex::new(false), Condvar::new())),
closing_pane: Arc::new((Mutex::new(false), Condvar::new())),
updating_tabs: Arc::new((Mutex::new(false), Condvar::new())),
} }
} }
pub fn closing_pane(&mut self) { pub fn blocking_input_thread(&mut self) {
let (lock, _cvar) = &*self.closing_pane; let (lock, _cvar) = &*self.input_thread;
let mut closing_pane = lock.lock().unwrap(); let mut input_thread = lock.lock().unwrap();
*closing_pane = true; *input_thread = true;
} }
pub fn done_closing_pane(&mut self) { pub fn unblock_input_thread(&mut self) {
let (lock, cvar) = &*self.closing_pane; let (lock, cvar) = &*self.input_thread;
let mut closing_pane = lock.lock().unwrap(); let mut input_thread = lock.lock().unwrap();
*closing_pane = false; *input_thread = false;
cvar.notify_all(); cvar.notify_all();
} }
pub fn opening_new_pane(&mut self) { pub fn wait_until_input_thread_is_unblocked(&self) {
let (lock, _cvar) = &*self.opening_new_pane; let (lock, cvar) = &*self.input_thread;
let mut opening_new_pane = lock.lock().unwrap(); let mut input_thread = lock.lock().unwrap();
*opening_new_pane = true; while *input_thread {
} input_thread = cvar.wait(input_thread).unwrap();
pub fn done_opening_new_pane(&mut self) {
let (lock, cvar) = &*self.opening_new_pane;
let mut opening_new_pane = lock.lock().unwrap();
*opening_new_pane = false;
cvar.notify_all();
}
pub fn updating_tabs(&mut self) {
let (lock, _cvar) = &*self.updating_tabs;
let mut updating_tabs = lock.lock().unwrap();
*updating_tabs = true;
}
pub fn done_updating_tabs(&self) {
let (lock, cvar) = &*self.updating_tabs;
let mut updating_tabs = lock.lock().unwrap();
*updating_tabs = false;
cvar.notify_one();
}
pub fn wait_until_pane_is_closed(&self) {
let (lock, cvar) = &*self.closing_pane;
let mut closing_pane = lock.lock().unwrap();
while *closing_pane {
closing_pane = cvar.wait(closing_pane).unwrap();
}
}
pub fn wait_until_new_pane_is_opened(&self) {
let (lock, cvar) = &*self.opening_new_pane;
let mut opening_new_pane = lock.lock().unwrap();
while *opening_new_pane {
opening_new_pane = cvar.wait(opening_new_pane).unwrap();
}
}
pub fn wait_until_tabs_are_updated(&self) {
let (lock, cvar) = &*self.updating_tabs;
let mut updating_tabs = lock.lock().unwrap();
while *updating_tabs {
updating_tabs = cvar.wait(updating_tabs).unwrap();
} }
} }
} }

View file

@ -323,9 +323,7 @@ impl From<&PluginInstruction> for PluginContext {
pub enum ClientContext { pub enum ClientContext {
Exit, Exit,
Error, Error,
DoneClosingPane, UnblockInputThread,
DoneOpeningNewPane,
DoneUpdatingTabs,
Render, Render,
} }
@ -335,9 +333,7 @@ impl From<&ClientInstruction> for ClientContext {
ClientInstruction::Exit => ClientContext::Exit, ClientInstruction::Exit => ClientContext::Exit,
ClientInstruction::Error(_) => ClientContext::Error, ClientInstruction::Error(_) => ClientContext::Error,
ClientInstruction::Render(_) => ClientContext::Render, ClientInstruction::Render(_) => ClientContext::Render,
ClientInstruction::DoneClosingPane => ClientContext::DoneClosingPane, ClientInstruction::UnblockInputThread => ClientContext::UnblockInputThread,
ClientInstruction::DoneOpeningNewPane => ClientContext::DoneOpeningNewPane,
ClientInstruction::DoneUpdatingTabs => ClientContext::DoneUpdatingTabs,
} }
} }
} }
@ -353,9 +349,7 @@ pub enum ServerContext {
Action, Action,
Render, Render,
TerminalResize, TerminalResize,
DoneClosingPane, UnblockInputThread,
DoneOpeningNewPane,
DoneUpdatingTabs,
ClientExit, ClientExit,
} }
@ -370,9 +364,7 @@ impl From<&ServerInstruction> for ServerContext {
ServerInstruction::Action(_) => ServerContext::Action, ServerInstruction::Action(_) => ServerContext::Action,
ServerInstruction::TerminalResize(_) => ServerContext::TerminalResize, ServerInstruction::TerminalResize(_) => ServerContext::TerminalResize,
ServerInstruction::Render(_) => ServerContext::Render, ServerInstruction::Render(_) => ServerContext::Render,
ServerInstruction::DoneClosingPane => ServerContext::DoneClosingPane, ServerInstruction::UnblockInputThread => ServerContext::UnblockInputThread,
ServerInstruction::DoneOpeningNewPane => ServerContext::DoneOpeningNewPane,
ServerInstruction::DoneUpdatingTabs => ServerContext::DoneUpdatingTabs,
ServerInstruction::ClientExit => ServerContext::ClientExit, ServerInstruction::ClientExit => ServerContext::ClientExit,
} }
} }

View file

@ -114,27 +114,18 @@ impl InputHandler {
self.os_input self.os_input
.send_to_server(ServerInstruction::Action(action.clone())); .send_to_server(ServerInstruction::Action(action.clone()));
} }
Action::NewPane(_) => { Action::CloseFocus
self.command_is_executing.opening_new_pane(); | Action::NewPane(_)
self.os_input | Action::NewTab
.send_to_server(ServerInstruction::Action(action));
self.command_is_executing.wait_until_new_pane_is_opened();
}
Action::CloseFocus => {
self.command_is_executing.closing_pane();
self.os_input
.send_to_server(ServerInstruction::Action(action));
self.command_is_executing.wait_until_pane_is_closed();
}
Action::NewTab
| Action::GoToNextTab | Action::GoToNextTab
| Action::GoToPreviousTab | Action::GoToPreviousTab
| Action::CloseTab | Action::CloseTab
| Action::GoToTab(_) => { | Action::GoToTab(_) => {
self.command_is_executing.updating_tabs(); self.command_is_executing.blocking_input_thread();
self.os_input self.os_input
.send_to_server(ServerInstruction::Action(action)); .send_to_server(ServerInstruction::Action(action));
self.command_is_executing.wait_until_tabs_are_updated(); self.command_is_executing
.wait_until_input_thread_is_unblocked();
} }
_ => self _ => self
.os_input .os_input

View file

@ -43,9 +43,7 @@ pub enum ServerInstruction {
NewClient(PositionAndSize), NewClient(PositionAndSize),
Action(Action), Action(Action),
Render(Option<String>), Render(Option<String>),
DoneClosingPane, UnblockInputThread,
DoneOpeningNewPane,
DoneUpdatingTabs,
ClientExit, ClientExit,
} }
@ -132,14 +130,8 @@ pub fn start_server(os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread::Jo
.send(PtyInstruction::NewTab) .send(PtyInstruction::NewTab)
.unwrap(); .unwrap();
} }
ServerInstruction::DoneClosingPane => { ServerInstruction::UnblockInputThread => {
os_input.send_to_client(ClientInstruction::DoneClosingPane); os_input.send_to_client(ClientInstruction::UnblockInputThread);
}
ServerInstruction::DoneOpeningNewPane => {
os_input.send_to_client(ClientInstruction::DoneOpeningNewPane);
}
ServerInstruction::DoneUpdatingTabs => {
os_input.send_to_client(ClientInstruction::DoneUpdatingTabs);
} }
ServerInstruction::ClientExit => { ServerInstruction::ClientExit => {
*sessions.write().unwrap() = None; *sessions.write().unwrap() = None;
@ -313,13 +305,13 @@ fn init_session(
PtyInstruction::ClosePane(id) => { PtyInstruction::ClosePane(id) => {
pty_bus.close_pane(id); pty_bus.close_pane(id);
send_server_instructions send_server_instructions
.send(ServerInstruction::DoneClosingPane) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
PtyInstruction::CloseTab(ids) => { PtyInstruction::CloseTab(ids) => {
pty_bus.close_tab(ids); pty_bus.close_tab(ids);
send_server_instructions send_server_instructions
.send(ServerInstruction::DoneClosingPane) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
PtyInstruction::Exit => { PtyInstruction::Exit => {
@ -382,21 +374,21 @@ fn init_session(
screen.get_active_tab_mut().unwrap().new_pane(pid); screen.get_active_tab_mut().unwrap().new_pane(pid);
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneOpeningNewPane) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::HorizontalSplit(pid) => { ScreenInstruction::HorizontalSplit(pid) => {
screen.get_active_tab_mut().unwrap().horizontal_split(pid); screen.get_active_tab_mut().unwrap().horizontal_split(pid);
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneOpeningNewPane) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::VerticalSplit(pid) => { ScreenInstruction::VerticalSplit(pid) => {
screen.get_active_tab_mut().unwrap().vertical_split(pid); screen.get_active_tab_mut().unwrap().vertical_split(pid);
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneOpeningNewPane) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::WriteCharacter(bytes) => { ScreenInstruction::WriteCharacter(bytes) => {
@ -493,49 +485,49 @@ fn init_session(
screen.new_tab(pane_id); screen.new_tab(pane_id);
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneUpdatingTabs) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::SwitchTabNext => { ScreenInstruction::SwitchTabNext => {
screen.switch_tab_next(); screen.switch_tab_next();
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneUpdatingTabs) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::SwitchTabPrev => { ScreenInstruction::SwitchTabPrev => {
screen.switch_tab_prev(); screen.switch_tab_prev();
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneUpdatingTabs) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::CloseTab => { ScreenInstruction::CloseTab => {
screen.close_tab(); screen.close_tab();
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneUpdatingTabs) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::ApplyLayout(layout, new_pane_pids) => { ScreenInstruction::ApplyLayout(layout, new_pane_pids) => {
screen.apply_layout(Layout::new(layout), new_pane_pids); screen.apply_layout(Layout::new(layout), new_pane_pids);
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneUpdatingTabs) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::GoToTab(tab_index) => { ScreenInstruction::GoToTab(tab_index) => {
screen.go_to_tab(tab_index as usize); screen.go_to_tab(tab_index as usize);
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneUpdatingTabs) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::UpdateTabName(c) => { ScreenInstruction::UpdateTabName(c) => {
screen.update_active_tab_name(c); screen.update_active_tab_name(c);
screen screen
.send_server_instructions .send_server_instructions
.send(ServerInstruction::DoneUpdatingTabs) .send(ServerInstruction::UnblockInputThread)
.unwrap(); .unwrap();
} }
ScreenInstruction::ChangeMode(mode_info) => { ScreenInstruction::ChangeMode(mode_info) => {

View file

@ -280,5 +280,5 @@ impl ServerOsApi for FakeInputOutput {
self.client_sender.send(msg).unwrap(); self.client_sender.send(msg).unwrap();
} }
fn add_client_sender(&mut self) {} fn add_client_sender(&mut self) {}
fn update_receiver(&mut self, stream: LocalSocketStream) {} fn update_receiver(&mut self, _stream: LocalSocketStream) {}
} }