From aa4c26ba4c4a0914217a217927008e3af1a6816b Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 8 Mar 2024 13:58:48 -0500 Subject: [PATCH 01/10] renamed detach flag and added detach support to 'new' --- src/command/share.rs | 4 +++- src/flag.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/command/share.rs b/src/command/share.rs index 1bf96ab..3523afa 100644 --- a/src/command/share.rs +++ b/src/command/share.rs @@ -20,7 +20,7 @@ pub fn attach(pargs: &mut Arguments) { // consume optional flags 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 target: String; @@ -146,6 +146,7 @@ pub fn new(pargs: &mut Arguments) { util::prevent_nest(); // get optional flag + let detached = pargs.contains(flag::DETACH); let target_dir: Result = pargs.value_from_str(flag::TARGET); // get target or fallback @@ -164,6 +165,7 @@ pub fn new(pargs: &mut Arguments) { let mut new = commands::NewSession::new(); new = new.group_name(title); 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); } Tmux::new() diff --git a/src/flag.rs b/src/flag.rs index 92056f3..4c7066d 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -1,7 +1,7 @@ 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 NEST: Flag = ["-n", "--nest"]; pub static QUIET: Flag = ["-q", "--quiet"]; From 71b33ca2748eb3477060b3e43f9f92b44786e041 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 8 Mar 2024 14:02:23 -0500 Subject: [PATCH 02/10] created env module --- src/env.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/env.rs diff --git a/src/env.rs b/src/env.rs new file mode 100644 index 0000000..4c828bc --- /dev/null +++ b/src/env.rs @@ -0,0 +1,10 @@ +use std::env::{ set_var, var }; + +pub type ENV_VAR = (&'static str, &'static str); + +pub static ATTACH_SYMBOL: ENV_VAR = ("ATTACH_SYMBOL", "*"); + +pub fn env_var(var: ENV_VAR) -> String { + var(var.0).unwrap_or(var.1.to_string()) +} + From 708c2334e210495706982a597bff4b6a5746f34b Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 8 Mar 2024 21:27:41 -0500 Subject: [PATCH 03/10] moved environment variable code to a new module --- src/command/share.rs | 10 +++++++--- src/env.rs | 12 +++++++----- src/main.rs | 1 + src/util.rs | 12 +++++++----- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/command/share.rs b/src/command/share.rs index 3523afa..14e0734 100644 --- a/src/command/share.rs +++ b/src/command/share.rs @@ -1,6 +1,5 @@ //! globally available tmux commands. use std::{ - env::var, ffi::OsString, process::exit }; @@ -12,7 +11,12 @@ use tmux_interface::{ commands }; -use crate::{ error, flag, util }; +use crate::{ + env::{ self, env_var }, + error, + flag, + util +}; pub fn attach(pargs: &mut Arguments) { // don't allow unflagged nests @@ -119,7 +123,7 @@ pub fn list() { } // 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 println!("sessions:"); diff --git a/src/env.rs b/src/env.rs index 4c828bc..b5fd791 100644 --- a/src/env.rs +++ b/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 { - var(var.0).unwrap_or(var.1.to_string()) +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() } + diff --git a/src/main.rs b/src/main.rs index 4276941..3ede333 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use std::{ use pico_args::Arguments; mod command; +mod env; mod error; mod flag; mod help; diff --git a/src/util.rs b/src/util.rs index e8d8831..e26608a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,5 @@ use std::{ - env::{ current_dir, var }, + env::current_dir, path::PathBuf, process::exit }; @@ -11,7 +11,10 @@ use tmux_interface::{ variables::session::SessionsCtl }; -use crate::error; +use crate::{ + env, + error +}; /// return a Vec of all sessions or None pub fn get_sessions() -> Option> { @@ -23,10 +26,9 @@ pub fn get_sessions() -> Option> { /// show the tmux nest text if env var is not unset pub fn prevent_nest() { - let tmux = var("TMUX").ok(); - if tmux.is_some() && tmux.unwrap() != "" { + if env::tmux() { println!("Sessions should be nested with care; unset TMUX or use the '-n' flag to allow."); - exit(1); + exit(6); } } From 802daeba37eb571d17dd83b5ac00a45cb67eff0e Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 8 Mar 2024 22:54:47 -0500 Subject: [PATCH 04/10] removed incompatible disable_echo from session exist check --- src/util.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index e8d8831..c99302b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -35,7 +35,6 @@ pub fn session_exists>(target: S) -> bool { let has_session = commands::HasSession::new() .target_session(target.into()); Tmux::new().add_command(has_session) - .disable_echo() .status() .unwrap() .success() From 0a17e66b4b98c0170bcd1e86cf9ced4423578c78 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 14 Mar 2024 14:24:07 -0400 Subject: [PATCH 05/10] wrote help topic for env vars --- src/help.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/help.rs b/src/help.rs index 0e7b14c..3c24a7e 100644 --- a/src/help.rs +++ b/src/help.rs @@ -25,7 +25,10 @@ commands: Use 'remux help ' to see detailed help text for each command."), - Some("a" | "attach") + +// COMMAND HELP + + Some("a" | "attach") => println!("remux attach Attach to an existing session. @@ -91,7 +94,19 @@ flags: -n, --nest Create the session inside another session. -t, --target Sets the target directory for the new session."), - // not found + +// TOPIC HELP + + Some("env" | "vars") + => +println!("remux environment variables + +REMUX_ATTACH_SYMBOL + Changes the symbol displayed for attached sessions displayed + by the 'list' command. + Default: '*'"), + +// not found _ => error::no_help(topic.unwrap()) } } From 54f3fbb3318ea4459d5c583e2a088d7a8b9c5fec Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 14 Mar 2024 14:24:30 -0400 Subject: [PATCH 06/10] corrected name of ATTACH_SYMBOL env var --- src/env.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/env.rs b/src/env.rs index b5fd791..d0f79a4 100644 --- a/src/env.rs +++ b/src/env.rs @@ -2,7 +2,7 @@ use std::env::var; pub type EnvVar = (&'static str, &'static str); -pub static ATTACH_SYMBOL: EnvVar = ("ATTACH_SYMBOL", "*"); +pub static ATTACH_SYMBOL: EnvVar = ("REMUX_ATTACH_SYMBOL", "*"); pub fn env_var(envvar: EnvVar) -> String { var(envvar.0).unwrap_or(envvar.1.to_string()) From fd076f7feee84f33936b502f93d54debf78d7c90 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 14 Mar 2024 15:00:58 -0400 Subject: [PATCH 07/10] updated help text to include topics --- src/help.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/help.rs b/src/help.rs index 3c24a7e..478aaa4 100644 --- a/src/help.rs +++ b/src/help.rs @@ -16,14 +16,17 @@ A command wrapper for tmux written in Rust. usage: remux [] commands: - help Show help text for remux or a specific command + help Show help text for remux, a command, or a help topic. attach Attach to an existing tmux session detach Detach clients from a tmux session has Check if a tmux session exists list Pretty-print all tmux sessions new Create a new tmux session -Use 'remux help ' to see detailed help text for each command."), +Use 'remux help ' to see detailed help text for each command. + +help topics: + env Environment variables"), // COMMAND HELP From 50f129c793cdf7c4f6e4c3c5a032e562e3624291 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 14 Mar 2024 16:26:40 -0400 Subject: [PATCH 08/10] added REMUX_WINDOW_NAME var for add command --- src/command/share.rs | 6 +++++- src/env.rs | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/command/share.rs b/src/command/share.rs index 14e0734..8d04e43 100644 --- a/src/command/share.rs +++ b/src/command/share.rs @@ -149,10 +149,13 @@ pub fn new(pargs: &mut Arguments) { // don't allow unflagged nesting util::prevent_nest(); - // get optional flag + // get optional flags let detached = pargs.contains(flag::DETACH); let target_dir: Result = pargs.value_from_str(flag::TARGET); + // get environment variables + let window_name = env_var(env::NEW_WINDOW_NAME); + // get target or fallback let args = pargs.clone().finish(); let title: String; @@ -171,6 +174,7 @@ pub fn new(pargs: &mut Arguments) { 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 !window_name.is_empty() { new.window_name = Some(window_name.into()); } Tmux::new() .add_command(new) diff --git a/src/env.rs b/src/env.rs index d0f79a4..a5b4450 100644 --- a/src/env.rs +++ b/src/env.rs @@ -2,7 +2,8 @@ use std::env::var; pub type EnvVar = (&'static str, &'static str); -pub static ATTACH_SYMBOL: EnvVar = ("REMUX_ATTACH_SYMBOL", "*"); +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()) From 087a2fe66e68a5cef009424dbfaa3fa009caceb4 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 14 Mar 2024 16:31:06 -0400 Subject: [PATCH 09/10] added help text for REMUX_NEW_WINDOW env var --- src/help.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/help.rs b/src/help.rs index 478aaa4..7e120d8 100644 --- a/src/help.rs +++ b/src/help.rs @@ -107,7 +107,12 @@ println!("remux environment variables REMUX_ATTACH_SYMBOL Changes the symbol displayed for attached sessions displayed by the 'list' command. - Default: '*'"), + Default: '*' + +REMUX_NEW_WINDOW + Provides a default window name when creating a session with + the 'new' command, if not empty. + Default: ''"), // not found _ => error::no_help(topic.unwrap()) From 58ff8fde2a7d42d6dbad47ab7abc3cdc54a96671 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 14 Mar 2024 16:51:40 -0400 Subject: [PATCH 10/10] default window name now sets correctly --- src/command/share.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/command/share.rs b/src/command/share.rs index 8d04e43..a98fd7d 100644 --- a/src/command/share.rs +++ b/src/command/share.rs @@ -174,10 +174,16 @@ pub fn new(pargs: &mut Arguments) { 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 !window_name.is_empty() { new.window_name = Some(window_name.into()); } - Tmux::new() - .add_command(new) - .output().ok(); + let mut tmux = Tmux::new().add_command(new); + + // 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(); }