diff options
| author | Aram Drevekenin <aram@poor.dev> | 2025-07-08 22:50:08 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-08 22:50:08 +0200 |
| commit | da9cf4ffeb1659669b067165ba6718556d665f2b (patch) | |
| tree | ef4404d5b82117baf8249a7bcc7efd0a7af31794 /zellij-utils/src | |
| parent | fix(tabs): allow setting cwd without a layout (#4273) (diff) | |
| download | sl0ppij-da9cf4ffeb1659669b067165ba6718556d665f2b.tar.gz sl0ppij-da9cf4ffeb1659669b067165ba6718556d665f2b.tar.xz sl0ppij-da9cf4ffeb1659669b067165ba6718556d665f2b.zip | |
fix(serialization): don't serialize when only UI elements present and provide post command discovery hook (#4276)
* do not serialize when only UI elements are present
* start work on a post serialization hook
* add post_command_discovery_hook
* fix tests
* style(fmt): rustfmt
* some cleanups
* moar formatting
* docs(changelog): add PR
Diffstat (limited to 'zellij-utils/src')
14 files changed, 76 insertions, 1 deletions
diff --git a/zellij-utils/src/errors.rs b/zellij-utils/src/errors.rs index b4082cd1..854410b6 100644 --- a/zellij-utils/src/errors.rs +++ b/zellij-utils/src/errors.rs @@ -348,7 +348,7 @@ pub enum ScreenContext { UpdateSessionInfos, ReplacePane, NewInPlacePluginPane, - DumpLayoutToHd, + SerializeLayoutForResurrection, RenameSession, DumpLayoutToPlugin, ListClientsMetadata, diff --git a/zellij-utils/src/input/options.rs b/zellij-utils/src/input/options.rs index 3e167022..72b4e63b 100644 --- a/zellij-utils/src/input/options.rs +++ b/zellij-utils/src/input/options.rs @@ -227,6 +227,10 @@ pub struct Options { pub web_server_cert: Option<PathBuf>, pub web_server_key: Option<PathBuf>, pub enforce_https_for_localhost: Option<bool>, + /// A command to run after the discovery of running commands when serializing, for the purpose + /// of manipulating the command (eg. with a regex) before it gets serialized + #[clap(long, value_parser)] + pub post_command_discovery_hook: Option<String>, } #[derive(ArgEnum, Deserialize, Serialize, Debug, Clone, Copy, PartialEq)] @@ -320,6 +324,9 @@ impl Options { let enforce_https_for_localhost = other .enforce_https_for_localhost .or(self.enforce_https_for_localhost); + let post_command_discovery_hook = other + .post_command_discovery_hook + .or(self.post_command_discovery_hook.clone()); Options { simplified_ui, @@ -360,6 +367,7 @@ impl Options { web_server_cert, web_server_key, enforce_https_for_localhost, + post_command_discovery_hook, } } @@ -433,6 +441,9 @@ impl Options { let enforce_https_for_localhost = other .enforce_https_for_localhost .or(self.enforce_https_for_localhost); + let post_command_discovery_hook = other + .post_command_discovery_hook + .or_else(|| self.post_command_discovery_hook.clone()); Options { simplified_ui, @@ -473,6 +484,7 @@ impl Options { web_server_cert, web_server_key, enforce_https_for_localhost, + post_command_discovery_hook, } } @@ -549,6 +561,7 @@ impl From<CliOptions> for Options { web_server_cert: opts.web_server_cert, web_server_key: opts.web_server_key, enforce_https_for_localhost: opts.enforce_https_for_localhost, + post_command_discovery_hook: opts.post_command_discovery_hook, ..Default::default() } } diff --git a/zellij-utils/src/kdl/mod.rs b/zellij-utils/src/kdl/mod.rs index ec6abf72..7ac9af09 100644 --- a/zellij-utils/src/kdl/mod.rs +++ b/zellij-utils/src/kdl/mod.rs @@ -2399,6 +2399,9 @@ impl Options { let enforce_https_for_localhost = kdl_property_first_arg_as_bool_or_error!(kdl_options, "enforce_https_for_localhost") .map(|(v, _)| v); + let post_command_discovery_hook = + kdl_property_first_arg_as_string_or_error!(kdl_options, "post_command_discovery_hook") + .map(|(hook, _entry)| hook.to_string()); Ok(Options { simplified_ui, @@ -2439,6 +2442,7 @@ impl Options { web_server_cert, web_server_key, enforce_https_for_localhost, + post_command_discovery_hook, }) } pub fn from_string(stringified_keybindings: &String) -> Result<Self, ConfigError> { @@ -3562,6 +3566,36 @@ impl Options { None } } + fn post_command_discovery_hook_to_kdl(&self, add_comments: bool) -> Option<KdlNode> { + let comment_text = format!( + "{}\n{}\n{}\n{}\n{}\n{}", + " ", + "// A command to run (will be wrapped with sh -c and provided the RESURRECT_COMMAND env variable) ", + "// after Zellij attempts to discover a command inside a pane when resurrecting sessions, the STDOUT", + "// of this command will be used instead of the discovered RESURRECT_COMMAND", + "// can be useful for removing wrappers around commands", + "// Note: be sure to escape backslashes and similar characters properly", + ); + + let create_node = |node_value: &str| -> KdlNode { + let mut node = KdlNode::new("post_command_discovery_hook"); + node.push(node_value.to_owned()); + node + }; + if let Some(post_command_discovery_hook) = &self.post_command_discovery_hook { + let mut node = create_node(&post_command_discovery_hook); + if add_comments { + node.set_leading(format!("{}\n", comment_text)); + } + Some(node) + } else if add_comments { + let mut node = create_node("echo $RESURRECT_COMMAND | sed <your_regex_here>"); + node.set_leading(format!("{}\n// ", comment_text)); + Some(node) + } else { + None + } + } pub fn to_kdl(&self, add_comments: bool) -> Vec<KdlNode> { let mut nodes = vec![]; if let Some(simplified_ui_node) = self.simplified_ui_to_kdl(add_comments) { @@ -3684,6 +3718,11 @@ impl Options { if let Some(web_server_port) = self.web_server_port_to_kdl(add_comments) { nodes.push(web_server_port); } + if let Some(post_command_discovery_hook) = + self.post_command_discovery_hook_to_kdl(add_comments) + { + nodes.push(post_command_discovery_hook); + } nodes } } diff --git a/zellij-utils/src/kdl/snapshots/zellij_utils__kdl__bare_config_from_default_assets_to_string_with_comments.snap b/zellij-utils/src/kdl/snapshots/zellij_utils__kdl__bare_config_from_default_assets_to_string_with_comments.snap index 7e26e85c..ddf36a40 100644 --- a/zellij-utils/src/kdl/snapshots/zellij_utils__kdl__bare_config_from_default_assets_to_string_with_comments.snap +++ b/zellij-utils/src/kdl/snapshots/zellij_utils__kdl__bare_config_from_default_assets_to_string_with_comments.snap @@ -527,3 +527,10 @@ web_client { // Default: 8082 // (Requires restart) // web_server_port 8082 + +// A command to run (will be wrapped with sh -c and provided the RESURRECT_COMMAND env variable) +// after Zellij attempts to discover a command inside a pane when resurrecting sessions, the STDOUT +// of this command will be used instead of the discovered RESURRECT_COMMAND +// can be useful for removing wrappers around commands +// Note: be sure to escape backslashes and similar characters properly +// post_command_discovery_hook "echo $RESURRECT_COMMAND | sed <your_regex_here>" diff --git a/zellij-utils/src/kdl/snapshots/zellij_utils__kdl__config_options_to_string_with_comments.snap b/zellij-utils/src/kdl/snapshots/zellij_utils__kdl__config_options_to_string_with_comments.snap index 1d9ec280..468a97dd 100644 --- a/zellij-utils/src/kdl/snapshots/zellij_utils__kdl__config_options_to_string_with_comments.snap +++ b/zellij-utils/src/kdl/snapshots/zellij_utils__kdl__config_options_to_string_with_comments.snap @@ -254,3 +254,10 @@ web_sharing "disabled" // Default: 8082 // (Requires restart) // web_server_port 8082 + +// A command to run (will be wrapped with sh -c and provided the RESURRECT_COMMAND env variable) +// after Zellij attempts to discover a command inside a pane when resurrecting sessions, the STDOUT +// of this command will be used instead of the discovered RESURRECT_COMMAND +// can be useful for removing wrappers around commands +// Note: be sure to escape backslashes and similar characters properly +// post_command_discovery_hook "echo $RESURRECT_COMMAND | sed <your_regex_here>" diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__cli_arguments_override_config_options.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__cli_arguments_override_config_options.snap index 0bc15ee0..61969ae2 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__cli_arguments_override_config_options.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__cli_arguments_override_config_options.snap @@ -43,4 +43,5 @@ Options { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, } diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__cli_arguments_override_layout_options.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__cli_arguments_override_layout_options.snap index 37d0e455..5d88e791 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__cli_arguments_override_layout_options.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__cli_arguments_override_layout_options.snap @@ -43,4 +43,5 @@ Options { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, } diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments-3.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments-3.snap index adff6294..e0acdc93 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments-3.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments-3.snap @@ -41,4 +41,5 @@ Options { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, } diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments.snap index 1bceb902..94e77a61 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments.snap @@ -5969,6 +5969,7 @@ Config { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, }, themes: {}, plugins: PluginAliases { diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_env_vars_override_config_env_vars.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_env_vars_override_config_env_vars.snap index 2281e405..e02c7eea 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_env_vars_override_config_env_vars.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_env_vars_override_config_env_vars.snap @@ -5969,6 +5969,7 @@ Config { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, }, themes: {}, plugins: PluginAliases { diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_keybinds_override_config_keybinds.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_keybinds_override_config_keybinds.snap index 7539c152..c19e3eea 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_keybinds_override_config_keybinds.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_keybinds_override_config_keybinds.snap @@ -128,6 +128,7 @@ Config { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, }, themes: {}, plugins: PluginAliases { diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_options_override_config_options.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_options_override_config_options.snap index d9ee9183..aa0fe763 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_options_override_config_options.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_options_override_config_options.snap @@ -43,4 +43,5 @@ Options { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, } diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_themes_override_config_themes.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_themes_override_config_themes.snap index 45f351b0..69b2d6b5 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_themes_override_config_themes.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_themes_override_config_themes.snap @@ -5969,6 +5969,7 @@ Config { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, }, themes: { "other-theme-from-config": Theme { diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_ui_config_overrides_config_ui_config.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_ui_config_overrides_config_ui_config.snap index 2e4e0dff..c4a4345b 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_ui_config_overrides_config_ui_config.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_ui_config_overrides_config_ui_config.snap @@ -5969,6 +5969,7 @@ Config { web_server_cert: None, web_server_key: None, enforce_https_for_localhost: None, + post_command_discovery_hook: None, }, themes: {}, plugins: PluginAliases { |
