aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-utils
diff options
context:
space:
mode:
authorKen Matsui <26405363+ken-matsui@users.noreply.github.com>2021-11-10 17:02:17 +0900
committerGitHub <noreply@github.com>2021-11-10 09:02:17 +0100
commit6d60d83e5808d3ccc5073f9f92368f38b8f3ae41 (patch)
tree89d7c43cbdf7e71c6e6c11a20bf96379904e39a0 /zellij-utils
parentdocs(changelog): use key-value in `docker-compose` (diff)
downloadsl0ppij-6d60d83e5808d3ccc5073f9f92368f38b8f3ae41.tar.gz
sl0ppij-6d60d83e5808d3ccc5073f9f92368f38b8f3ae41.tar.xz
sl0ppij-6d60d83e5808d3ccc5073f9f92368f38b8f3ae41.zip
fix(envs): Unify operation of Zellij environment variables (#842)
Diffstat (limited to 'zellij-utils')
-rw-r--r--zellij-utils/Cargo.toml1
-rw-r--r--zellij-utils/src/consts.rs7
-rw-r--r--zellij-utils/src/envs.rs24
-rw-r--r--zellij-utils/src/input/mod.rs3
-rw-r--r--zellij-utils/src/lib.rs1
5 files changed, 31 insertions, 5 deletions
diff --git a/zellij-utils/Cargo.toml b/zellij-utils/Cargo.toml
index f9f69723..2eb8f6b6 100644
--- a/zellij-utils/Cargo.toml
+++ b/zellij-utils/Cargo.toml
@@ -9,6 +9,7 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+anyhow = "1.0.45"
backtrace = "0.3.55"
bincode = "1.3.1"
colored = "2.0.0"
diff --git a/zellij-utils/src/consts.rs b/zellij-utils/src/consts.rs
index bcb4ea4a..f20dadaa 100644
--- a/zellij-utils/src/consts.rs
+++ b/zellij-utils/src/consts.rs
@@ -1,10 +1,10 @@
//! Zellij program-wide constants.
+use crate::envs;
use crate::shared::set_permissions;
use directories_next::ProjectDirs;
use lazy_static::lazy_static;
use nix::unistd::Uid;
-use once_cell::sync::OnceCell;
use std::path::PathBuf;
use std::{env, fs};
@@ -26,11 +26,10 @@ const fn system_default_data_dir() -> &'static str {
lazy_static! {
static ref UID: Uid = Uid::current();
- pub static ref SESSION_NAME: OnceCell<String> = OnceCell::new();
pub static ref ZELLIJ_PROJ_DIR: ProjectDirs =
ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap();
pub static ref ZELLIJ_SOCK_DIR: PathBuf = {
- let mut ipc_dir = env::var("ZELLIJ_SOCKET_DIR").map_or_else(
+ let mut ipc_dir = envs::get_socket_dir().map_or_else(
|_| {
ZELLIJ_PROJ_DIR
.runtime_dir()
@@ -45,7 +44,7 @@ lazy_static! {
let mut sock_dir = ZELLIJ_SOCK_DIR.clone();
fs::create_dir_all(&sock_dir).unwrap();
set_permissions(&sock_dir).unwrap();
- sock_dir.push(SESSION_NAME.get().unwrap());
+ sock_dir.push(envs::get_session_name().unwrap());
sock_dir
};
pub static ref ZELLIJ_TMP_DIR: PathBuf =
diff --git a/zellij-utils/src/envs.rs b/zellij-utils/src/envs.rs
new file mode 100644
index 00000000..971def6e
--- /dev/null
+++ b/zellij-utils/src/envs.rs
@@ -0,0 +1,24 @@
+/// Uniformly operates ZELLIJ* environment variables
+use anyhow::Result;
+use std::env::{set_var, var};
+
+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 const SOCKET_DIR_ENV_KEY: &str = "ZELLIJ_SOCKET_DIR";
+pub fn get_socket_dir() -> Result<String> {
+ Ok(var(SOCKET_DIR_ENV_KEY)?)
+}
diff --git a/zellij-utils/src/input/mod.rs b/zellij-utils/src/input/mod.rs
index 613b8d17..ca26f78f 100644
--- a/zellij-utils/src/input/mod.rs
+++ b/zellij-utils/src/input/mod.rs
@@ -10,6 +10,7 @@ pub mod options;
pub mod plugins;
pub mod theme;
+use crate::envs;
use termion::input::TermRead;
use zellij_tile::data::{InputMode, Key, ModeInfo, Palette, PluginCapabilities};
@@ -57,7 +58,7 @@ pub fn get_mode_info(
InputMode::Session => vec![("d".to_string(), "Detach".to_string())],
};
- let session_name = std::env::var("ZELLIJ_SESSION_NAME").ok();
+ let session_name = envs::get_session_name().ok();
ModeInfo {
mode,
diff --git a/zellij-utils/src/lib.rs b/zellij-utils/src/lib.rs
index 0aa95830..4ccb25f5 100644
--- a/zellij-utils/src/lib.rs
+++ b/zellij-utils/src/lib.rs
@@ -1,6 +1,7 @@
pub mod channels;
pub mod cli;
pub mod consts;
+pub mod envs;
pub mod errors;
pub mod input;
pub mod ipc;