moved environment variable code to a new module
This commit is contained in:
parent
c8800a7f53
commit
a07ae193b5
4 changed files with 22 additions and 13 deletions
|
@ -1,6 +1,5 @@
|
||||||
//! globally available tmux commands.
|
//! globally available tmux commands.
|
||||||
use std::{
|
use std::{
|
||||||
env::var,
|
|
||||||
ffi::OsString,
|
ffi::OsString,
|
||||||
process::exit
|
process::exit
|
||||||
};
|
};
|
||||||
|
@ -12,7 +11,12 @@ use tmux_interface::{
|
||||||
commands
|
commands
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{ error, flag, util };
|
use crate::{
|
||||||
|
env::{ self, env_var },
|
||||||
|
error,
|
||||||
|
flag,
|
||||||
|
util
|
||||||
|
};
|
||||||
|
|
||||||
pub fn attach(pargs: &mut Arguments) {
|
pub fn attach(pargs: &mut Arguments) {
|
||||||
// must be run from terminal
|
// must be run from terminal
|
||||||
|
@ -122,7 +126,7 @@ pub fn list() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get attached session symbol
|
// get attached session symbol
|
||||||
let attach_symbol = var("REMUX_ATTACH_SYMBOL").unwrap_or("*".to_string());
|
let attach_symbol = env_var(env::ATTACH_SYMBOL);
|
||||||
|
|
||||||
// pretty print session list
|
// pretty print session list
|
||||||
println!("sessions:");
|
println!("sessions:");
|
||||||
|
|
12
src/env.rs
12
src/env.rs
|
@ -1,10 +1,12 @@
|
||||||
use std::env::{ set_var, var };
|
use std::env::var;
|
||||||
|
|
||||||
pub type ENV_VAR = (&'static str, &'static str);
|
pub type EnvVar = (&'static str, &'static str);
|
||||||
|
|
||||||
pub static ATTACH_SYMBOL: ENV_VAR = ("ATTACH_SYMBOL", "*");
|
pub static ATTACH_SYMBOL: EnvVar = ("ATTACH_SYMBOL", "*");
|
||||||
|
|
||||||
pub fn env_var(var: ENV_VAR) -> String {
|
pub fn env_var(envvar: EnvVar) -> String {
|
||||||
var(var.0).unwrap_or(var.1.to_string())
|
var(envvar.0).unwrap_or(envvar.1.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn tmux() -> bool { !var("TMUX").unwrap_or("".to_string()).is_empty() }
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::env::{ set_var, var };
|
||||||
use pico_args::Arguments;
|
use pico_args::Arguments;
|
||||||
|
|
||||||
mod command;
|
mod command;
|
||||||
|
mod env;
|
||||||
mod error;
|
mod error;
|
||||||
mod flag;
|
mod flag;
|
||||||
mod help;
|
mod help;
|
||||||
|
|
12
src/util.rs
12
src/util.rs
|
@ -1,5 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
env::{ current_dir, var },
|
env::current_dir,
|
||||||
io::{ stdout, IsTerminal },
|
io::{ stdout, IsTerminal },
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::exit
|
process::exit
|
||||||
|
@ -12,7 +12,10 @@ use tmux_interface::{
|
||||||
variables::session::SessionsCtl
|
variables::session::SessionsCtl
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::error;
|
use crate::{
|
||||||
|
env,
|
||||||
|
error
|
||||||
|
};
|
||||||
|
|
||||||
/// return a Vec of all sessions or None
|
/// return a Vec of all sessions or None
|
||||||
pub fn get_sessions() -> Option<Vec<Session>> {
|
pub fn get_sessions() -> Option<Vec<Session>> {
|
||||||
|
@ -24,10 +27,9 @@ pub fn get_sessions() -> Option<Vec<Session>> {
|
||||||
|
|
||||||
/// show the tmux nest text if env var is not unset
|
/// show the tmux nest text if env var is not unset
|
||||||
pub fn prevent_nest() {
|
pub fn prevent_nest() {
|
||||||
let tmux = var("TMUX").ok();
|
if env::tmux() {
|
||||||
if tmux.is_some() && tmux.unwrap() != "" {
|
|
||||||
println!("Sessions should be nested with care; unset TMUX or use the '-n' flag to allow.");
|
println!("Sessions should be nested with care; unset TMUX or use the '-n' flag to allow.");
|
||||||
exit(1);
|
exit(6);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue