summaryrefslogtreecommitdiffstats
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
parent03f58fc184bc1be47a8da526563e60a21a1fced0 (diff)
fix(envs): Unify operation of Zellij environment variables (#842)
-rw-r--r--Cargo.lock1
-rw-r--r--src/sessions.rs5
-rw-r--r--zellij-client/src/lib.rs11
-rw-r--r--zellij-server/src/lib.rs3
-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
9 files changed, 42 insertions, 14 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5830e7bcf..ec8123b0a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2932,6 +2932,7 @@ dependencies = [
name = "zellij-utils"
version = "0.21.0"
dependencies = [
+ "anyhow",
"async-std",
"backtrace",
"bincode",
diff --git a/src/sessions.rs b/src/sessions.rs
index 31c4df4ef..a13c6c7c1 100644
--- a/src/sessions.rs
+++ b/src/sessions.rs
@@ -3,6 +3,7 @@ use std::time::SystemTime;
use std::{fs, io, process};
use zellij_utils::{
consts::ZELLIJ_SOCK_DIR,
+ envs,
interprocess::local_socket::LocalSocketStream,
ipc::{ClientToServerMsg, IpcSenderWithContext},
};
@@ -66,7 +67,7 @@ fn assert_socket(name: &str) -> bool {
}
pub(crate) fn print_sessions(sessions: Vec<String>) {
- let curr_session = std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into());
+ let curr_session = envs::get_session_name().unwrap_or_else(|_| "".into());
sessions.iter().for_each(|session| {
let suffix = if curr_session == *session {
" (current)"
@@ -78,7 +79,7 @@ pub(crate) fn print_sessions(sessions: Vec<String>) {
}
pub(crate) fn print_sessions_with_index(sessions: Vec<String>) {
- let curr_session = std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into());
+ let curr_session = envs::get_session_name().unwrap_or_else(|_| "".into());
for (i, session) in sessions.iter().enumerate() {
let suffix = if curr_session == *session {
" (current)"
diff --git a/zellij-client/src/lib.rs b/zellij-client/src/lib.rs
index 45842fece..cbad733cb 100644
--- a/zellij-client/src/lib.rs
+++ b/zellij-client/src/lib.rs
@@ -18,7 +18,8 @@ use crate::{
use zellij_tile::data::InputMode;
use zellij_utils::{
channels::{self, ChannelWithContext, SenderWithContext},
- consts::{SESSION_NAME, ZELLIJ_IPC_PIPE},
+ consts::ZELLIJ_IPC_PIPE,
+ envs,
errors::{ClientContext, ContextType, ErrorInstruction},
input::{actions::Action, config::Config, options::Options},
ipc::{ClientAttributes, ClientToServerMsg, ExitReason, ServerToClientMsg},
@@ -119,7 +120,7 @@ pub fn start_client(
.get_stdout_writer()
.write(clear_client_terminal_attributes.as_bytes())
.unwrap();
- std::env::set_var(&"ZELLIJ", "0");
+ envs::set_zellij("0".to_string());
let palette = config.themes.clone().map_or_else(
|| os_input.load_palette(),
@@ -137,14 +138,12 @@ pub fn start_client(
let first_msg = match info {
ClientInfo::Attach(name, config_options) => {
- SESSION_NAME.set(name).unwrap();
- std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap());
+ envs::set_session_name(name);
ClientToServerMsg::AttachClient(client_attributes, config_options)
}
ClientInfo::New(name) => {
- SESSION_NAME.set(name).unwrap();
- std::env::set_var(&"ZELLIJ_SESSION_NAME", SESSION_NAME.get().unwrap());
+ envs::set_session_name(name);
spawn_server(&*ZELLIJ_IPC_PIPE).unwrap();
diff --git a/zellij-server/src/lib.rs b/zellij-server/src/lib.rs
index 570645e9c..38e469a89 100644
--- a/zellij-server/src/lib.rs
+++ b/zellij-server/src/lib.rs
@@ -17,6 +17,7 @@ use std::{
sync::{Arc, Mutex, RwLock},
thread,
};
+use zellij_utils::envs;
use zellij_utils::nix::sys::stat::{umask, Mode};
use zellij_utils::pane_size::Size;
use zellij_utils::zellij_tile;
@@ -186,7 +187,7 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.start()
.expect("could not daemonize the server process");
- std::env::set_var(&"ZELLIJ", "0");
+ envs::set_zellij("0".to_string());
let (to_server, server_receiver): ChannelWithContext<ServerInstruction> = channels::bounded(50);
let to_server = SenderWithContext::new(to_server);
diff --git a/zellij-utils/Cargo.toml b/zellij-utils/Cargo.toml
index f9f697230..2eb8f6b64 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 bcb4ea4aa..f20dadaac 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 000000000..971def6e5
--- /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 613b8d172..ca26f78f6 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 0aa958300..4ccb25f5e 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;