zellij/zellij-utils/src/envs.rs
Aram Drevekenin 79bf6ab868
feat(config): switch to kdl (#1759)
* chore(config): default kdl keybindings config

* tests

* work

* refactor(config): move stuff around

* work

* tab merge layout

* work

* work

* layouts working

* work

* layout tests

* work

* work

* feat(parsing): kdl layouts without config

* refactor(kdl): move stuff around

* work

* tests(layout): add cases and fix bugs

* work

* fix(kdl): various bugs

* chore(layouts): move all layouts to kdl

* feat(kdl): shared keybidns

* fix(layout): do not count fixed panes toward percentile

* fix(keybinds): missing keybinds and actions

* fix(config): adjust default tips

* refactor(config): move stuff around

* fix(tests): make e2e tests pass

* fix(kdl): add verbose parsing errors

* fix(kdl): focused tab

* fix(layout): corret default_tab_template behavior

* style(code): fix compile warnings

* feat(cli): send actions through the cli

* fix(cli): exit only when action is done

* fix(cli): open embedded pane from floating pane

* fix(cli): send actions to other sessions

* feat(cli): command alias

* feat(converter): convert old config

* feat(converter): convert old layout and theme files

* feat(kdl): pretty errors

* feat(client): convert old YAML files on startup

* fix: various bugs and styling issues

* fix: e2e tests

* fix(screen): propagate errors after merge

* style(clippy): lower clippy level

* fix(tests): own session_name variable

* style(fmt): rustfmt

* fix(cli): various action fixes

* style(fmt): rustfmt

* fix(themes): loading of theme files

* style(fmt): rustfmt

* fix(tests): theme fixtures

* fix(layouts): better errors on unknown nodes

* fix(kdl): clarify valid node terminator error

* fix(e2e): adjust close tab test

* fix(e2e): adjust close tab test again

* style(code): cleanup some comments
2022-10-05 07:44:00 +02:00

71 lines
1.9 KiB
Rust

/// Uniformly operates ZELLIJ* environment variables
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap},
env::{set_var, var},
};
use std::fmt;
pub const ZELLIJ_ENV_KEY: &str = "ZELLIJ";
pub fn get_zellij() -> Result<String> {
Ok(var(ZELLIJ_ENV_KEY)?)
}
pub fn set_zellij(v: String) {
set_var(ZELLIJ_ENV_KEY, v);
}
pub const SESSION_NAME_ENV_KEY: &str = "ZELLIJ_SESSION_NAME";
pub fn get_session_name() -> Result<String> {
Ok(var(SESSION_NAME_ENV_KEY)?)
}
pub fn set_session_name(v: String) {
set_var(SESSION_NAME_ENV_KEY, v);
}
pub fn set_initial_environment_vars() {
set_var("COLORTERM", "24bit");
}
pub const SOCKET_DIR_ENV_KEY: &str = "ZELLIJ_SOCKET_DIR";
pub fn get_socket_dir() -> Result<String> {
Ok(var(SOCKET_DIR_ENV_KEY)?)
}
/// Manage ENVIRONMENT VARIABLES from the configuration and the layout files
#[derive(Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct EnvironmentVariables {
env: HashMap<String, String>,
}
impl fmt::Debug for EnvironmentVariables {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut stable_sorted = BTreeMap::new();
for (env_var_name, env_var_value) in self.env.iter() {
stable_sorted.insert(env_var_name, env_var_value);
}
write!(f, "{:#?}", stable_sorted)
}
}
impl EnvironmentVariables {
/// Merges two structs, keys from `other` supersede keys from `self`
pub fn merge(&self, other: Self) -> Self {
let mut env = self.clone();
env.env.extend(other.env);
env
}
pub fn from_data(data: HashMap<String, String>) -> Self {
EnvironmentVariables { env: data }
}
/// 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);
}
}
}