Skip to content

Commit

Permalink
feat: allow RunCommand to block calling terminal until command has ex…
Browse files Browse the repository at this point in the history
…ited
  • Loading branch information
blefevre committed Feb 20, 2024
1 parent 27bffbf commit 491615d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,19 @@ fn attach_with_cli_client(
) {
let os_input = get_os_input(zellij_client::os_input_output::get_cli_client_os_input);
let get_current_dir = || std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
let blocking = match cli_action {
zellij_utils::cli::CliAction::NewPane { blocking, .. } => blocking,
_ => false,
};

match Action::actions_from_cli(cli_action, Box::new(get_current_dir), config) {
Ok(actions) => {
zellij_client::cli_client::start_cli_client(Box::new(os_input), session_name, actions);
zellij_client::cli_client::start_cli_client(
Box::new(os_input),
session_name,
actions,
blocking,
);
std::process::exit(0);
},
Err(e) => {
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn main() {
in_place,
name,
close_on_exit,
blocking,
start_suspended,
x,
y,
Expand All @@ -46,6 +47,7 @@ fn main() {
in_place,
name,
close_on_exit,
blocking,
start_suspended,
configuration: None,
skip_plugin_cache,
Expand Down Expand Up @@ -79,6 +81,7 @@ fn main() {
in_place,
name: None,
close_on_exit: false,
blocking: false,
start_suspended: false,
configuration,
skip_plugin_cache,
Expand Down
10 changes: 9 additions & 1 deletion zellij-client/src/cli_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn start_cli_client(
mut os_input: Box<dyn ClientOsApi>,
session_name: &str,
actions: Vec<Action>,
blocking: bool,
) {
let zellij_ipc_pipe: PathBuf = {
let mut sock_dir = zellij_utils::consts::ZELLIJ_SOCK_DIR.clone();
Expand Down Expand Up @@ -64,7 +65,7 @@ pub fn start_cli_client(
);
},
action => {
single_message_client(&mut os_input, action, pane_id);
single_message_client(&mut os_input, action, pane_id, blocking);
},
}
}
Expand Down Expand Up @@ -186,12 +187,19 @@ fn single_message_client(
os_input: &mut Box<dyn ClientOsApi>,
action: Action,
pane_id: Option<u32>,
blocking: bool,
) {
let msg = ClientToServerMsg::Action(action, pane_id, None);
os_input.send_to_server(msg);
loop {
match os_input.recv_from_server() {
Some((ServerToClientMsg::UnblockInputThread, _)) => {
if !blocking {
os_input.send_to_server(ClientToServerMsg::ClientExited);
process::exit(0);
}
},
Some((ServerToClientMsg::RunCommandComplete, _)) => {
os_input.send_to_server(ClientToServerMsg::ClientExited);
process::exit(0);
},
Expand Down
3 changes: 3 additions & 0 deletions zellij-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(crate) enum ClientInstruction {
Error(String),
Render(String),
UnblockInputThread,
RunCommandComplete,
Exit(ExitReason),
SwitchToMode(InputMode),
Connected,
Expand All @@ -59,6 +60,7 @@ impl From<ServerToClientMsg> for ClientInstruction {
ServerToClientMsg::Exit(e) => ClientInstruction::Exit(e),
ServerToClientMsg::Render(buffer) => ClientInstruction::Render(buffer),
ServerToClientMsg::UnblockInputThread => ClientInstruction::UnblockInputThread,
ServerToClientMsg::RunCommandComplete => ClientInstruction::RunCommandComplete,
ServerToClientMsg::SwitchToMode(input_mode) => {
ClientInstruction::SwitchToMode(input_mode)
},
Expand Down Expand Up @@ -86,6 +88,7 @@ impl From<&ClientInstruction> for ClientContext {
ClientInstruction::Error(_) => ClientContext::Error,
ClientInstruction::Render(_) => ClientContext::Render,
ClientInstruction::UnblockInputThread => ClientContext::UnblockInputThread,
ClientInstruction::RunCommandComplete => ClientContext::RunCommandComplete,
ClientInstruction::SwitchToMode(_) => ClientContext::SwitchToMode,
ClientInstruction::Connected => ClientContext::Connected,
ClientInstruction::ActiveClients(_) => ClientContext::ActiveClients,
Expand Down
12 changes: 12 additions & 0 deletions zellij-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub enum ServerInstruction {
),
Render(Option<HashMap<ClientId, String>>),
UnblockInputThread,
RunCommandComplete,
ClientExit(ClientId),
RemoveClient(ClientId),
Error(String),
Expand Down Expand Up @@ -102,6 +103,7 @@ impl From<&ServerInstruction> for ServerContext {
ServerInstruction::NewClient(..) => ServerContext::NewClient,
ServerInstruction::Render(..) => ServerContext::Render,
ServerInstruction::UnblockInputThread => ServerContext::UnblockInputThread,
ServerInstruction::RunCommandComplete => ServerContext::RunCommandComplete,
ServerInstruction::ClientExit(..) => ServerContext::ClientExit,
ServerInstruction::RemoveClient(..) => ServerContext::RemoveClient,
ServerInstruction::Error(_) => ServerContext::Error,
Expand Down Expand Up @@ -571,6 +573,16 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
},
}
},
ServerInstruction::RunCommandComplete => {
for client_id in session_state.read().unwrap().clients.keys() {
send_to_client!(
*client_id,
os_input,
ServerToClientMsg::RunCommandComplete,
session_state
);
}
},
ServerInstruction::ClientExit(client_id) => {
let _ =
os_input.send_to_client(client_id, ServerToClientMsg::Exit(ExitReason::Normal));
Expand Down
1 change: 1 addition & 0 deletions zellij-server/src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ impl Pty {
let quit_cb = Box::new({
let senders = self.bus.senders.clone();
move |pane_id, exit_status, command| {
let _ = senders.send_to_server(ServerInstruction::RunCommandComplete);
if hold_on_close {
let _ = senders.send_to_screen(ScreenInstruction::HoldPane(
pane_id,
Expand Down
3 changes: 3 additions & 0 deletions zellij-server/src/unit/screen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,7 @@ pub fn send_cli_new_pane_action_with_default_parameters() {
in_place: false,
name: None,
close_on_exit: false,
blocking: false,
start_suspended: false,
configuration: None,
skip_plugin_cache: false,
Expand Down Expand Up @@ -2151,6 +2152,7 @@ pub fn send_cli_new_pane_action_with_split_direction() {
in_place: false,
name: None,
close_on_exit: false,
blocking: false,
start_suspended: false,
configuration: None,
skip_plugin_cache: false,
Expand Down Expand Up @@ -2194,6 +2196,7 @@ pub fn send_cli_new_pane_action_with_command_and_cwd() {
in_place: false,
name: None,
close_on_exit: false,
blocking: false,
start_suspended: false,
configuration: None,
skip_plugin_cache: false,
Expand Down
16 changes: 16 additions & 0 deletions zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ pub enum Sessions {
#[clap(short, long, value_parser, default_value("false"), takes_value(false))]
close_on_exit: bool,

/// Block the calling terminal until the run command exits
#[clap(short, long, value_parser, default_value("false"), takes_value(false))]
blocking: bool,

/// Start the command suspended, only running after you first presses ENTER
#[clap(short, long, value_parser, default_value("false"), takes_value(false))]
start_suspended: bool,
Expand Down Expand Up @@ -483,6 +487,18 @@ pub enum CliAction {
requires("command")
)]
close_on_exit: bool,

/// Block the calling terminal until the run command exits
#[clap(
short,
long,
value_parser,
default_value("false"),
takes_value(false),
requires("command")
)]
blocking: bool,

/// Start the command suspended, only running it after the you first press ENTER
#[clap(
short,
Expand Down
2 changes: 2 additions & 0 deletions zellij-utils/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ pub enum ClientContext {
Exit,
Error,
UnblockInputThread,
RunCommandComplete,
Render,
ServerError,
SwitchToMode,
Expand All @@ -430,6 +431,7 @@ pub enum ServerContext {
NewClient,
Render,
UnblockInputThread,
RunCommandComplete,
ClientExit,
RemoveClient,
Error,
Expand Down
1 change: 1 addition & 0 deletions zellij-utils/src/input/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ impl Action {
in_place,
name,
close_on_exit,
blocking: _,
start_suspended,
configuration,
skip_plugin_cache,
Expand Down
1 change: 1 addition & 0 deletions zellij-utils/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum ClientToServerMsg {
pub enum ServerToClientMsg {
Render(String),
UnblockInputThread,
RunCommandComplete,
Exit(ExitReason),
SwitchToMode(InputMode),
Connected,
Expand Down

0 comments on commit 491615d

Please sign in to comment.