diff options
author | a-kenji <aks.kenji@protonmail.com> | 2021-11-10 13:31:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-10 13:31:37 +0100 |
commit | abbe3b237af1308a6108d513c75b41ad0a889e3a (patch) | |
tree | 6c114a5b5e1ebc5de8fbebb6b77e91bad3862f9d /zellij-utils/src | |
parent | 4a3e8689ded45c0494a3e406c3801e7bc2e6aca0 (diff) |
add(feature): toggle boolean options with cli flag (#855)
add the ability to toggle boolean options with a cli flag:
example:
if the pane frames are turned off in the config file,
then passing in the `--no-pane-frames` flag will toggle the
pane frames on
Diffstat (limited to 'zellij-utils/src')
-rw-r--r-- | zellij-utils/src/cli.rs | 10 | ||||
-rw-r--r-- | zellij-utils/src/input/options.rs | 100 |
2 files changed, 101 insertions, 9 deletions
diff --git a/zellij-utils/src/cli.rs b/zellij-utils/src/cli.rs index 78615d929..2c1aeba37 100644 --- a/zellij-utils/src/cli.rs +++ b/zellij-utils/src/cli.rs @@ -1,6 +1,8 @@ -use crate::consts::{ZELLIJ_CONFIG_DIR_ENV, ZELLIJ_CONFIG_FILE_ENV}; -use crate::input::options::Options; use crate::setup::Setup; +use crate::{ + consts::{ZELLIJ_CONFIG_DIR_ENV, ZELLIJ_CONFIG_FILE_ENV}, + input::options::CliOptions, +}; use serde::{Deserialize, Serialize}; use std::path::PathBuf; use structopt::StructOpt; @@ -51,7 +53,7 @@ pub struct CliArgs { pub enum Command { /// Change the behaviour of zellij #[structopt(name = "options")] - Options(Options), + Options(CliOptions), /// Setup zellij and check its configuration #[structopt(name = "setup")] @@ -66,7 +68,7 @@ pub enum Command { pub enum SessionCommand { /// Change the behaviour of zellij #[structopt(name = "options")] - Options(Options), + Options(CliOptions), } #[derive(Debug, StructOpt, Clone, Serialize, Deserialize)] diff --git a/zellij-utils/src/input/options.rs b/zellij-utils/src/input/options.rs index 94994e1e3..c23267fb6 100644 --- a/zellij-utils/src/input/options.rs +++ b/zellij-utils/src/input/options.rs @@ -35,12 +35,14 @@ impl FromStr for OnForceClose { #[derive(Clone, Default, Debug, PartialEq, Deserialize, Serialize, StructOpt)] /// Options that can be set either through the config file, /// or cli flags - cli flags should take precedence over the config file +/// TODO: In order to correctly parse boolean flags, this is currently split +/// into Options and CliOptions, this could be a good canditate for a macro pub struct Options { /// Allow plugins to use a more simplified layout /// that is compatible with more fonts #[structopt(long)] #[serde(default)] - pub simplified_ui: bool, + pub simplified_ui: Option<bool>, /// Set the default theme #[structopt(long)] pub theme: Option<String>, @@ -57,10 +59,10 @@ pub struct Options { #[structopt(long)] #[serde(default)] /// Disable handling of mouse events - pub disable_mouse_mode: bool, + pub disable_mouse_mode: Option<bool>, #[structopt(long)] #[serde(default)] - pub no_pane_frames: bool, + pub no_pane_frames: Option<bool>, /// Set behaviour on force close (quit or detach) #[structopt(long)] pub on_force_close: Option<OnForceClose>, @@ -79,7 +81,41 @@ impl Options { /// will supercede a `Some` in `self` // TODO: Maybe a good candidate for a macro? pub fn merge(&self, other: Options) -> Options { - let merge_bool = |opt_other, opt_self| if opt_other { true } else { opt_self }; + let disable_mouse_mode = other.disable_mouse_mode.or(self.disable_mouse_mode); + let no_pane_frames = other.no_pane_frames.or(self.no_pane_frames); + let simplified_ui = other.simplified_ui.or(self.simplified_ui); + let default_mode = other.default_mode.or(self.default_mode); + let default_shell = other.default_shell.or_else(|| self.default_shell.clone()); + let layout_dir = other.layout_dir.or_else(|| self.layout_dir.clone()); + let theme = other.theme.or_else(|| self.theme.clone()); + let on_force_close = other.on_force_close.or(self.on_force_close); + + Options { + simplified_ui, + theme, + default_mode, + default_shell, + layout_dir, + disable_mouse_mode, + no_pane_frames, + on_force_close, + } + } + + /// Merges two [`Options`] structs, + /// - `Some` in `other` will supercede a `Some` in `self` + /// - `Some(bool)` in `other` will toggle a `Some(bool)` in `self` + // TODO: Maybe a good candidate for a macro? + pub fn merge_from_cli(&self, other: Options) -> Options { + let merge_bool = |opt_other: Option<bool>, opt_self: Option<bool>| { + if opt_other.is_some() ^ opt_self.is_some() { + opt_other.or(opt_self) + } else if opt_other.is_some() && opt_self.is_some() { + Some(opt_other.unwrap() ^ opt_self.unwrap()) + } else { + None + } + }; let simplified_ui = merge_bool(other.simplified_ui, self.simplified_ui); let disable_mouse_mode = merge_bool(other.disable_mouse_mode, self.disable_mouse_mode); @@ -105,9 +141,63 @@ impl Options { pub fn from_cli(&self, other: Option<Command>) -> Options { if let Some(Command::Options(options)) = other { - Options::merge(self, options) + Options::merge_from_cli(self, options.into()) } else { self.to_owned() } } } + +#[derive(Clone, Default, Debug, PartialEq, StructOpt, Serialize, Deserialize)] +/// Options that can be set through cli flags +/// boolean flags end up toggling boolean options in `Options` +pub struct CliOptions { + /// Allow plugins to use a more simplified layout + /// that is compatible with more fonts + #[structopt(long)] + pub simplified_ui: bool, + /// Set the default theme + #[structopt(long)] + pub theme: Option<String>, + /// Set the default mode + #[structopt(long)] + pub default_mode: Option<InputMode>, + /// Set the default shell + #[structopt(long, parse(from_os_str))] + pub default_shell: Option<PathBuf>, + /// Set the layout_dir, defaults to + /// subdirectory of config dir + #[structopt(long, parse(from_os_str))] + pub layout_dir: Option<PathBuf>, + #[structopt(long)] + /// Disable handling of mouse events + pub disable_mouse_mode: bool, + #[structopt(long)] + pub no_pane_frames: bool, + /// Set behaviour on force close (quit or detach) + #[structopt(long)] + pub on_force_close: Option<OnForceClose>, +} + +impl From<CliOptions> for Options { + fn from(cli_options: CliOptions) -> Self { + let handle_bool = |bool| { + if bool { + Some(true) + } else { + None + } + }; + + Self { + simplified_ui: handle_bool(cli_options.simplified_ui), + theme: cli_options.theme, + default_mode: cli_options.default_mode, + default_shell: cli_options.default_shell, + layout_dir: cli_options.layout_dir, + disable_mouse_mode: handle_bool(cli_options.disable_mouse_mode), + no_pane_frames: handle_bool(cli_options.no_pane_frames), + on_force_close: cli_options.on_force_close, + } + } +} |