From 67ee63548aa48caebf32b081d57cb8184bff39b8 Mon Sep 17 00:00:00 2001 From: Kunal Mohan Date: Fri, 9 Jul 2021 00:25:59 +0530 Subject: [PATCH 1/2] Add on_force_close config option --- zellij-client/src/lib.rs | 4 ++- zellij-utils/assets/config/default.yaml | 7 ++++++ zellij-utils/src/input/actions.rs | 10 ++++++++ zellij-utils/src/input/options.rs | 33 +++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/zellij-client/src/lib.rs b/zellij-client/src/lib.rs index 90732024da..2bfaae2e82 100644 --- a/zellij-client/src/lib.rs +++ b/zellij-client/src/lib.rs @@ -164,6 +164,8 @@ pub fn start_client( }) }); + let on_force_close = config_options.on_force_close.unwrap_or_default(); + let _stdin_thread = thread::Builder::new() .name("stdin_handler".to_string()) .spawn({ @@ -200,7 +202,7 @@ pub fn start_client( Box::new({ let os_api = os_input.clone(); move || { - os_api.send_to_server(ClientToServerMsg::Action(Action::Detach)); + os_api.send_to_server(ClientToServerMsg::Action(on_force_close.into())); } }), ); diff --git a/zellij-utils/assets/config/default.yaml b/zellij-utils/assets/config/default.yaml index 42bc16886d..9b0a57ffa4 100644 --- a/zellij-utils/assets/config/default.yaml +++ b/zellij-utils/assets/config/default.yaml @@ -240,3 +240,10 @@ keybinds: key: [Ctrl: 'q',] - action: [Detach,] key: [Char: 'd',] + +# Choose what to do when zellij receives SIGTERM, SIGINT, SIGQUIT or SIGHUP +# eg. when terminal window with an active zellij session is closed +# Options: +# - Detach (Default) +# - Quit +#on_force_close: Quit diff --git a/zellij-utils/src/input/actions.rs b/zellij-utils/src/input/actions.rs index ecd515a4a6..bfd7d25592 100644 --- a/zellij-utils/src/input/actions.rs +++ b/zellij-utils/src/input/actions.rs @@ -1,6 +1,7 @@ //! Definition of the actions that can be bound to keys. use super::command::RunCommandAction; +use crate::input::options::OnForceClose; use serde::{Deserialize, Serialize}; use zellij_tile::data::InputMode; @@ -81,3 +82,12 @@ pub enum Action { MouseHold(Position), Copy, } + +impl From for Action { + fn from(ofc: OnForceClose) -> Action { + match ofc { + OnForceClose::Quit => Action::Quit, + OnForceClose::Detach => Action::Detach, + } + } +} diff --git a/zellij-utils/src/input/options.rs b/zellij-utils/src/input/options.rs index 2ba2be86e5..c2083f4e10 100644 --- a/zellij-utils/src/input/options.rs +++ b/zellij-utils/src/input/options.rs @@ -2,9 +2,34 @@ use crate::cli::Command; use serde::{Deserialize, Serialize}; use std::path::PathBuf; +use std::str::FromStr; use structopt::StructOpt; use zellij_tile::data::InputMode; +#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Serialize)] +pub enum OnForceClose { + Quit, + Detach, +} + +impl Default for OnForceClose { + fn default() -> Self { + Self::Detach + } +} + +impl FromStr for OnForceClose { + type Err = Box; + + fn from_str(s: &str) -> Result { + match s { + "quit" => Ok(Self::Quit), + "detach" => Ok(Self::Detach), + e => Err(e.to_string().into()), + } + } +} + #[derive(Clone, Default, Debug, PartialEq, Deserialize, Serialize, StructOpt)] /// Options that can be set either through the config file, /// or cli flags @@ -30,6 +55,8 @@ pub struct Options { #[structopt(long)] #[serde(default)] pub disable_mouse_mode: bool, + #[structopt(long)] + pub on_force_close: Option, } impl Options { @@ -77,6 +104,11 @@ impl Options { self.disable_mouse_mode }; + let on_force_close = match other.on_force_close { + None => self.on_force_close, + other => other, + }; + Options { simplified_ui, theme, @@ -84,6 +116,7 @@ impl Options { default_shell, layout_dir, disable_mouse_mode, + on_force_close, } } From 56af1d8640a8ae72b9f82146abb5c9381411b6e4 Mon Sep 17 00:00:00 2001 From: Kunal Mohan Date: Fri, 9 Jul 2021 00:42:06 +0530 Subject: [PATCH 2/2] Add doc comment for the on_force_close option --- zellij-utils/src/input/options.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/zellij-utils/src/input/options.rs b/zellij-utils/src/input/options.rs index c2083f4e10..461f5c341e 100644 --- a/zellij-utils/src/input/options.rs +++ b/zellij-utils/src/input/options.rs @@ -55,6 +55,7 @@ pub struct Options { #[structopt(long)] #[serde(default)] pub disable_mouse_mode: bool, + /// Set behaviour on force close (quit or detach) #[structopt(long)] pub on_force_close: Option, }