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

View file

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

View file

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

View file

@ -114,27 +114,18 @@ impl InputHandler {
self.os_input
.send_to_server(ServerInstruction::Action(action.clone()));
}
Action::NewPane(_) => {
self.command_is_executing.opening_new_pane();
self.os_input
.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::CloseFocus
| Action::NewPane(_)
| Action::NewTab
| Action::GoToNextTab
| Action::GoToPreviousTab
| Action::CloseTab
| Action::GoToTab(_) => {
self.command_is_executing.updating_tabs();
self.command_is_executing.blocking_input_thread();
self.os_input
.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
.os_input

View file

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

View file

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