Merge branch 'env-vars' into dep-upgrade
This commit is contained in:
commit
c0512ab1de
5 changed files with 46 additions and 14 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) {
|
||||||
// don't allow unflagged nests
|
// don't allow unflagged nests
|
||||||
|
@ -20,7 +24,7 @@ pub fn attach(pargs: &mut Arguments) {
|
||||||
|
|
||||||
// consume optional flags
|
// consume optional flags
|
||||||
let read_only = pargs.contains(flag::READ_ONLY);
|
let read_only = pargs.contains(flag::READ_ONLY);
|
||||||
let detach_other = pargs.contains(flag::DETACHED);
|
let detach_other = pargs.contains(flag::DETACH);
|
||||||
|
|
||||||
let args = pargs.clone().finish();
|
let args = pargs.clone().finish();
|
||||||
let target: String;
|
let target: String;
|
||||||
|
@ -119,7 +123,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:");
|
||||||
|
@ -145,9 +149,13 @@ pub fn new(pargs: &mut Arguments) {
|
||||||
// don't allow unflagged nesting
|
// don't allow unflagged nesting
|
||||||
util::prevent_nest();
|
util::prevent_nest();
|
||||||
|
|
||||||
// get optional flag
|
// get optional flags
|
||||||
|
let detached = pargs.contains(flag::DETACH);
|
||||||
let target_dir: Result<String, Error> = pargs.value_from_str(flag::TARGET);
|
let target_dir: Result<String, Error> = pargs.value_from_str(flag::TARGET);
|
||||||
|
|
||||||
|
// get environment variables
|
||||||
|
let window_name = env_var(env::NEW_WINDOW_NAME);
|
||||||
|
|
||||||
// get target or fallback
|
// get target or fallback
|
||||||
let args = pargs.clone().finish();
|
let args = pargs.clone().finish();
|
||||||
let title: String;
|
let title: String;
|
||||||
|
@ -164,10 +172,18 @@ pub fn new(pargs: &mut Arguments) {
|
||||||
let mut new = commands::NewSession::new();
|
let mut new = commands::NewSession::new();
|
||||||
new = new.session_name(title);
|
new = new.session_name(title);
|
||||||
if let Some(command) = command { new.shell_command = Some(command.to_string_lossy()); }
|
if let Some(command) = command { new.shell_command = Some(command.to_string_lossy()); }
|
||||||
|
if detached { new.detached = true; }
|
||||||
if let Ok(target_dir) = target_dir { new = new.start_directory(target_dir); }
|
if let Ok(target_dir) = target_dir { new = new.start_directory(target_dir); }
|
||||||
|
|
||||||
Tmux::new()
|
let mut tmux = Tmux::new().add_command(new);
|
||||||
.add_command(new)
|
|
||||||
.output().ok();
|
// rename window if var not empty
|
||||||
|
if !window_name.is_empty() {
|
||||||
|
let auto_name = commands::RenameWindow::new()
|
||||||
|
.new_name(window_name);
|
||||||
|
tmux = tmux.add_command(auto_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
tmux.output().ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
src/env.rs
Normal file
13
src/env.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
use std::env::var;
|
||||||
|
|
||||||
|
pub type EnvVar = (&'static str, &'static str);
|
||||||
|
|
||||||
|
pub static ATTACH_SYMBOL: EnvVar = ("REMUX_ATTACH_SYMBOL", "*");
|
||||||
|
pub static NEW_WINDOW_NAME: EnvVar = ("REMUX_NEW_WINDOW", "");
|
||||||
|
|
||||||
|
pub fn env_var(envvar: EnvVar) -> String {
|
||||||
|
var(envvar.0).unwrap_or(envvar.1.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tmux() -> bool { !var("TMUX").unwrap_or("".to_string()).is_empty() }
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
type Flag = [&'static str;2];
|
type Flag = [&'static str;2];
|
||||||
|
|
||||||
pub static DETACHED: Flag = ["-d", "--detached"];
|
pub static DETACH: Flag = ["-d", "--detach"];
|
||||||
pub static HELP: Flag = ["-h", "--help"];
|
pub static HELP: Flag = ["-h", "--help"];
|
||||||
pub static NEST: Flag = ["-n", "--nest"];
|
pub static NEST: Flag = ["-n", "--nest"];
|
||||||
pub static QUIET: Flag = ["-q", "--quiet"];
|
pub static QUIET: Flag = ["-q", "--quiet"];
|
||||||
|
|
|
@ -6,6 +6,7 @@ use std::{
|
||||||
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,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::exit
|
process::exit
|
||||||
};
|
};
|
||||||
|
@ -11,7 +11,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>> {
|
||||||
|
@ -23,10 +26,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