aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authora-kenji <aks.kenji@protonmail.com>2021-03-17 16:56:02 +0100
committera-kenji <aks.kenji@protonmail.com>2021-03-17 16:56:02 +0100
commite64192b42e584663ac6a64a73cff1921f098d945 (patch)
treea25e629b1f168550ebf801d224348e9b94364582 /src
parentRemove logging to log.txt (diff)
downloadsl0ppij-e64192b42e584663ac6a64a73cff1921f098d945.tar.gz
sl0ppij-e64192b42e584663ac6a64a73cff1921f098d945.tar.xz
sl0ppij-e64192b42e584663ac6a64a73cff1921f098d945.zip
Add a clean config option
Add a clean config option, which makes zellij not look for a default configuration file.
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs18
-rw-r--r--src/common/input/config.rs21
-rw-r--r--src/common/mod.rs3
3 files changed, 30 insertions, 12 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 67a92196..740879f0 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,7 +1,7 @@
use std::path::PathBuf;
use structopt::StructOpt;
-#[derive(StructOpt, Debug, Default)]
+#[derive(StructOpt, Debug)]
#[structopt(name = "zellij")]
pub struct CliArgs {
/// Send "split (direction h == horizontal / v == vertical)" to active zellij session
@@ -24,10 +24,20 @@ pub struct CliArgs {
#[structopt(short, long)]
pub layout: Option<PathBuf>,
- /// Path to the configuration yaml file
- #[structopt(short, long)]
- pub config: Option<PathBuf>,
+ #[structopt(subcommand)]
+ pub config: Option<ConfigCli>,
#[structopt(short, long)]
pub debug: bool,
}
+
+#[derive(Debug, StructOpt)]
+pub enum ConfigCli {
+ /// Path to the configuration yaml file
+ Config {
+ path: Option<PathBuf>,
+ #[structopt(long)]
+ /// Disables loading of configuration file at default location
+ clean: bool,
+ },
+}
diff --git a/src/common/input/config.rs b/src/common/input/config.rs
index d9d1f784..668b4106 100644
--- a/src/common/input/config.rs
+++ b/src/common/input/config.rs
@@ -6,10 +6,13 @@ use std::io::{self, Read};
use std::path::{Path, PathBuf};
use super::keybinds::{Keybinds, KeybindsFromYaml};
+use crate::cli::ConfigCli;
use directories_next::ProjectDirs;
use serde::Deserialize;
+type ConfigResult = Result<Config, ConfigError>;
+
/// Intermediate deserialisation config struct
#[derive(Debug, Deserialize)]
pub struct ConfigFromYaml {
@@ -41,7 +44,7 @@ impl Default for Config {
impl Config {
/// Uses defaults, but lets config override them.
- pub fn from_yaml(yaml_config: &str) -> Result<Config, ConfigError> {
+ pub fn from_yaml(yaml_config: &str) -> ConfigResult {
let config_from_yaml: ConfigFromYaml = serde_yaml::from_str(&yaml_config)?;
let keybinds = Keybinds::get_default_keybinds_with_config(config_from_yaml.keybinds);
Ok(Config { keybinds })
@@ -49,7 +52,7 @@ impl Config {
/// Deserializes from given path.
#[allow(unused_must_use)]
- pub fn new(path: &Path) -> Result<Config, ConfigError> {
+ pub fn new(path: &Path) -> ConfigResult {
match File::open(path) {
Ok(mut file) => {
let mut yaml_config = String::new();
@@ -57,15 +60,13 @@ impl Config {
.map_err(|e| ConfigError::IoPath(e, path.to_path_buf()))?;
Ok(Config::from_yaml(&yaml_config)?)
}
- Err(_) => {
- Ok(Config::default())
- }
+ Err(_) => Ok(Config::default()),
}
}
/// Deserializes the config from an optional path, or a platform specific path,
/// merges the default configuration - options take precedence.
- pub fn from_option_or_default(option: Option<PathBuf>) -> Result<Config, ConfigError> {
+ fn from_option_or_default(option: Option<PathBuf>) -> ConfigResult {
if let Some(config_path) = option {
Ok(Config::new(&config_path)?)
} else {
@@ -75,6 +76,14 @@ impl Config {
Ok(Config::new(&config_path)?)
}
}
+
+ pub fn from_cli_config(cli_config: Option<ConfigCli>) -> ConfigResult {
+ match cli_config {
+ Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()),
+ Some(ConfigCli::Config { path, .. }) => Ok(Config::from_option_or_default(path)?),
+ None => Ok(Config::default()),
+ }
+ }
}
impl Display for ConfigError {
diff --git a/src/common/mod.rs b/src/common/mod.rs
index 02e75f98..777b615f 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -169,8 +169,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let mut app_state = AppState::default();
- #[allow(unused_must_use)]
- let config = Config::from_option_or_default(opts.config)
+ let config = Config::from_cli_config(opts.config)
.map_err(|e| {
eprintln!("There was an error in the config file:\n{}", e);
std::process::exit(1);