From 362ead14d865b7bd5cc174693e67c5cad700deb5 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Wed, 12 May 2021 09:42:05 +0200 Subject: [PATCH] Slight rework of config loading. Merges the cli and file configuration --- src/cli.rs | 4 ---- src/client/mod.rs | 5 ++++- src/common/input/options.rs | 42 ++++++++++++++++++++++++++++++++++++- src/common/screen.rs | 13 ++++-------- src/server/mod.rs | 8 +++---- 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 6ab33d4554..b95b5770d5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -11,10 +11,6 @@ pub struct CliArgs { #[structopt(long)] pub max_panes: Option, - /// Speficy, if a simplified layout should be used that is compatible with more fonts - #[structopt(long)] - pub simplified: bool, - /// Change where zellij looks for layouts and plugins #[structopt(long)] pub data_dir: Option, diff --git a/src/client/mod.rs b/src/client/mod.rs index 8fcbc07050..15e1df11c5 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -15,6 +15,7 @@ use crate::common::{ errors::{ClientContext, ContextType}, input::config::Config, input::handler::input_loop, + input::options::{ConfigOptions, Options}, os_input_output::ClientOsApi, thread_bus::{SenderType, SenderWithContext, SyncChannelWithContext}, }; @@ -40,12 +41,14 @@ pub fn start_client(mut os_input: Box, opts: CliArgs, config: C let mut command_is_executing = CommandIsExecuting::new(); + let config_options: ConfigOptions = Options::from_cli(&config.options, opts.option.clone()).into(); + let full_screen_ws = os_input.get_terminal_size_using_fd(0); os_input.connect_to_server(); os_input.send_to_server(ServerInstruction::NewClient( full_screen_ws, opts, - config.options.clone(), + config_options, )); os_input.set_raw_mode(0); diff --git a/src/common/input/options.rs b/src/common/input/options.rs index 3ff2fd8e53..6affb71566 100644 --- a/src/common/input/options.rs +++ b/src/common/input/options.rs @@ -1,12 +1,23 @@ +//! Handles cli and configuration options +use crate::cli::ConfigCli; use serde::{Deserialize, Serialize}; use structopt::StructOpt; #[derive(Clone, Default, Debug, PartialEq, Deserialize, Serialize, StructOpt)] /// Options that can be set either through the config file, /// or cli flags +/// intermediate struct pub struct Options { - /// Allow plugins to use a more compatible font type + /// Allow plugins to use a more simplified layout + /// that is compatible with more fonts #[structopt(long)] + pub simplified_ui: Option, +} + +#[derive(Clone, Default, Debug, PartialEq, Deserialize, Serialize)] +/// Merged version of the [`Options`] struct +// TODO: Maybe a good candidate for a macro? +pub struct ConfigOptions { pub simplified_ui: bool, } @@ -18,4 +29,33 @@ impl Options { Options::default() } } + + /// Merges two [`Options`] structs, a `Some` in `other` + /// will supercede a `Some` in `self` + // TODO: Maybe a good candidate for a macro? + pub fn merge(&self, other: Options) -> Options { + let simplified_ui = if let Some(bool) = other.simplified_ui { + Some(bool) + } else { self.simplified_ui }; + + Options { simplified_ui } + } + + pub fn from_cli(&self, other: Option) -> Options { + if let Some(ConfigCli::Options(options)) = other { + Options::merge(&self, options) + } else { + self.to_owned() + } + } +} + +impl From for ConfigOptions { + fn from(options: Options) -> ConfigOptions { + let simplified_ui = options.simplified_ui; + + ConfigOptions { + simplified_ui: simplified_ui.unwrap_or_default(), + } + } } diff --git a/src/common/screen.rs b/src/common/screen.rs index 6f5a4cb378..804730d078 100644 --- a/src/common/screen.rs +++ b/src/common/screen.rs @@ -4,8 +4,7 @@ use std::collections::BTreeMap; use std::os::unix::io::RawFd; use std::str; -use crate::cli::ConfigCli; -use crate::common::input::options::Options; +use crate::common::input::options::ConfigOptions; use crate::common::pty::{PtyInstruction, VteBytes}; use crate::common::thread_bus::Bus; use crate::errors::{ContextType, ScreenContext}; @@ -330,15 +329,11 @@ pub fn screen_thread_main( bus: Bus, max_panes: Option, full_screen_ws: PositionAndSize, - options: Option, - config_options: Options, + config_options: ConfigOptions, ) { let colors = bus.os_input.as_ref().unwrap().load_palette(); - let capabilities = if let Some(ConfigCli::Options(options)) = options { - options.simplified_ui - } else { - config_options.simplified_ui - }; + let capabilities = config_options.simplified_ui; + let mut screen = Screen::new( bus, &full_screen_ws, diff --git a/src/server/mod.rs b/src/server/mod.rs index d987572707..7704b747d4 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -12,7 +12,7 @@ use crate::client::ClientInstruction; use crate::common::thread_bus::{Bus, ThreadSenders}; use crate::common::{ errors::{ContextType, ServerContext}, - input::{actions::Action, options::Options}, + input::{actions::Action, options::ConfigOptions}, os_input_output::{set_permissions, ServerOsApi}, pty::{pty_thread_main, Pty, PtyInstruction}, screen::{screen_thread_main, ScreenInstruction}, @@ -30,7 +30,7 @@ use route::route_thread_main; #[derive(Serialize, Deserialize, Debug, Clone)] pub enum ServerInstruction { TerminalResize(PositionAndSize), - NewClient(PositionAndSize, CliArgs, Options), + NewClient(PositionAndSize, CliArgs, ConfigOptions), Action(Action), Render(Option), UnblockInputThread, @@ -155,7 +155,7 @@ pub fn start_server(os_input: Box) -> thread::JoinHandle<()> { fn init_session( os_input: Box, opts: CliArgs, - config_options: Options, + config_options: ConfigOptions, to_server: SenderWithContext, full_screen_ws: PositionAndSize, ) -> SessionMetaData { @@ -214,14 +214,12 @@ fn init_session( Some(os_input.clone()), ); let max_panes = opts.max_panes; - let options = opts.option; move || { screen_thread_main( screen_bus, max_panes, full_screen_ws, - options, config_options, ); }