add: set env var's from config and layout (#1154)
Add ability to set `ENVIRONMENT VARIABLES` from the
config and the layout files.
example:
```
env:
ZELLIJ_CONFIG:
DEFAULT
```
or
```
env:
ZELLIJ_LAYOUT_NAME:
BUILD_SESSION
```
If two keys conflict (configuration and layout), then the key
from the layout is used.
fixes: #1059
This commit is contained in:
parent
4f84c36024
commit
6d653e1521
3 changed files with 38 additions and 1 deletions
|
|
@ -133,6 +133,7 @@ pub fn start_client(
|
||||||
.write(clear_client_terminal_attributes.as_bytes())
|
.write(clear_client_terminal_attributes.as_bytes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
envs::set_zellij("0".to_string());
|
envs::set_zellij("0".to_string());
|
||||||
|
config.env.set_vars();
|
||||||
|
|
||||||
let palette = config.themes.clone().map_or_else(
|
let palette = config.themes.clone().map_or_else(
|
||||||
|| os_input.load_palette(),
|
|| os_input.load_palette(),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
/// Uniformly operates ZELLIJ* environment variables
|
/// Uniformly operates ZELLIJ* environment variables
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::env::{set_var, var};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
env::{set_var, var},
|
||||||
|
};
|
||||||
|
|
||||||
pub const ZELLIJ_ENV_KEY: &str = "ZELLIJ";
|
pub const ZELLIJ_ENV_KEY: &str = "ZELLIJ";
|
||||||
pub fn get_zellij() -> Result<String> {
|
pub fn get_zellij() -> Result<String> {
|
||||||
|
|
@ -24,3 +28,26 @@ pub const SOCKET_DIR_ENV_KEY: &str = "ZELLIJ_SOCKET_DIR";
|
||||||
pub fn get_socket_dir() -> Result<String> {
|
pub fn get_socket_dir() -> Result<String> {
|
||||||
Ok(var(SOCKET_DIR_ENV_KEY)?)
|
Ok(var(SOCKET_DIR_ENV_KEY)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Manage ENVIRONMENT VARIABLES from the configuration and the layout files
|
||||||
|
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub struct EnvironmentVariablesFromYaml {
|
||||||
|
env: HashMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EnvironmentVariablesFromYaml {
|
||||||
|
/// Merges two structs, keys from `other` supercede keys from `self`
|
||||||
|
pub fn merge(&self, other: Self) -> Self {
|
||||||
|
let mut env = self.clone();
|
||||||
|
env.env.extend(other.env);
|
||||||
|
env
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set all the ENVIRONMENT VARIABLES, that are configured
|
||||||
|
/// in the configuration and layout files
|
||||||
|
pub fn set_vars(&self) {
|
||||||
|
for (k, v) in &self.env {
|
||||||
|
set_var(k, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ use super::options::Options;
|
||||||
use super::plugins::{PluginsConfig, PluginsConfigError, PluginsConfigFromYaml};
|
use super::plugins::{PluginsConfig, PluginsConfigError, PluginsConfigFromYaml};
|
||||||
use super::theme::ThemesFromYaml;
|
use super::theme::ThemesFromYaml;
|
||||||
use crate::cli::{CliArgs, Command};
|
use crate::cli::{CliArgs, Command};
|
||||||
|
use crate::envs::EnvironmentVariablesFromYaml;
|
||||||
use crate::setup;
|
use crate::setup;
|
||||||
|
|
||||||
const DEFAULT_CONFIG_FILE_NAME: &str = "config.yaml";
|
const DEFAULT_CONFIG_FILE_NAME: &str = "config.yaml";
|
||||||
|
|
@ -26,6 +27,8 @@ pub struct ConfigFromYaml {
|
||||||
pub options: Option<Options>,
|
pub options: Option<Options>,
|
||||||
pub keybinds: Option<KeybindsFromYaml>,
|
pub keybinds: Option<KeybindsFromYaml>,
|
||||||
pub themes: Option<ThemesFromYaml>,
|
pub themes: Option<ThemesFromYaml>,
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub env: Option<EnvironmentVariablesFromYaml>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub plugins: PluginsConfigFromYaml,
|
pub plugins: PluginsConfigFromYaml,
|
||||||
}
|
}
|
||||||
|
|
@ -37,6 +40,7 @@ pub struct Config {
|
||||||
pub options: Options,
|
pub options: Options,
|
||||||
pub themes: Option<ThemesFromYaml>,
|
pub themes: Option<ThemesFromYaml>,
|
||||||
pub plugins: PluginsConfig,
|
pub plugins: PluginsConfig,
|
||||||
|
pub env: EnvironmentVariablesFromYaml,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
|
|
@ -66,6 +70,7 @@ impl Default for Config {
|
||||||
let keybinds = Keybinds::default();
|
let keybinds = Keybinds::default();
|
||||||
let options = Options::default();
|
let options = Options::default();
|
||||||
let themes = None;
|
let themes = None;
|
||||||
|
let env = EnvironmentVariablesFromYaml::default();
|
||||||
let plugins = PluginsConfig::default();
|
let plugins = PluginsConfig::default();
|
||||||
|
|
||||||
Config {
|
Config {
|
||||||
|
|
@ -73,6 +78,7 @@ impl Default for Config {
|
||||||
options,
|
options,
|
||||||
themes,
|
themes,
|
||||||
plugins,
|
plugins,
|
||||||
|
env,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -160,6 +166,7 @@ impl Config {
|
||||||
keybinds: self.keybinds.clone(),
|
keybinds: self.keybinds.clone(),
|
||||||
options: self.options.merge(other.options),
|
options: self.options.merge(other.options),
|
||||||
themes: self.themes.clone(), // TODO
|
themes: self.themes.clone(), // TODO
|
||||||
|
env: self.env.merge(other.env),
|
||||||
plugins: self.plugins.merge(other.plugins),
|
plugins: self.plugins.merge(other.plugins),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -172,12 +179,14 @@ impl TryFrom<ConfigFromYaml> for Config {
|
||||||
let keybinds = Keybinds::get_default_keybinds_with_config(config_from_yaml.keybinds);
|
let keybinds = Keybinds::get_default_keybinds_with_config(config_from_yaml.keybinds);
|
||||||
let options = Options::from_yaml(config_from_yaml.options);
|
let options = Options::from_yaml(config_from_yaml.options);
|
||||||
let themes = config_from_yaml.themes;
|
let themes = config_from_yaml.themes;
|
||||||
|
let env = config_from_yaml.env.unwrap_or_default();
|
||||||
let plugins = PluginsConfig::get_plugins_with_default(config_from_yaml.plugins.try_into()?);
|
let plugins = PluginsConfig::get_plugins_with_default(config_from_yaml.plugins.try_into()?);
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
keybinds,
|
keybinds,
|
||||||
options,
|
options,
|
||||||
plugins,
|
plugins,
|
||||||
themes,
|
themes,
|
||||||
|
env,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue