From b380d1863d5af5e4b120abd4ff23ab090cd4aca9 Mon Sep 17 00:00:00 2001 From: Valerie Wolfe Date: Thu, 6 Jun 2024 09:41:41 -0400 Subject: [PATCH] initial implementation of root command and made some commands allow being thrown through pipes --- src/command/session.rs | 18 ++++++++++++++++++ src/command/share.rs | 4 ++++ src/help.rs | 8 ++++++++ src/main.rs | 10 ++++------ src/util.rs | 6 ++++++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/command/session.rs b/src/command/session.rs index 47c9da9..3bd3584 100644 --- a/src/command/session.rs +++ b/src/command/session.rs @@ -1,4 +1,5 @@ //! commands accessible from within a session +use std::fs::read_to_string; use pico_args::Arguments; use tmux_interface::{ @@ -8,7 +9,10 @@ use tmux_interface::{ use crate::{ error, flag, util }; +const TMP_ROOT: &str = "/tmp/remux_root"; + pub fn switch(pargs: &mut Arguments) { + util::terminal_enforce(); // refuse to run outside a session util::session_enforce("switch"); @@ -32,3 +36,17 @@ pub fn switch(pargs: &mut Arguments) { .output().ok(); } +pub fn root() { + util::session_enforce("root"); + + let exec = commands::Run::new().shell_command("printf '#{session_path}' > ".to_string() + TMP_ROOT); + Tmux::new() + .add_command(exec) + .output().ok(); + + if let Ok(text) = read_to_string(TMP_ROOT) { + println!("{text}"); + std::fs::remove_file(TMP_ROOT).ok(); + } +} + diff --git a/src/command/share.rs b/src/command/share.rs index 028d9ca..81f23a4 100644 --- a/src/command/share.rs +++ b/src/command/share.rs @@ -15,6 +15,8 @@ use tmux_interface::{ use crate::{ error, flag, util }; pub fn attach(pargs: &mut Arguments) { + // must be run from terminal + util::terminal_enforce(); // don't allow unflagged nests util::prevent_nest(); @@ -59,6 +61,7 @@ pub fn attach(pargs: &mut Arguments) { } pub fn detach(pargs: &mut Arguments) { + util::terminal_enforce(); // get target or fallback let args = pargs.clone().finish(); let target: String; @@ -142,6 +145,7 @@ pub fn list() { } pub fn new(pargs: &mut Arguments) { + util::terminal_enforce(); // don't allow unflagged nesting util::prevent_nest(); diff --git a/src/help.rs b/src/help.rs index 1c78b45..f3180ac 100644 --- a/src/help.rs +++ b/src/help.rs @@ -91,6 +91,14 @@ flags: -n, --nest Create the session inside another session. -t, --target Sets the target directory for the new session."), + Some("root") + => +println!("remux root +Print the session path (#{{session_path}}) to standard output. +Must be run from inside a session. + +usage: remux root"), + Some("s" | "switch") => println!("remux switch diff --git a/src/main.rs b/src/main.rs index f610b1e..d4883f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,4 @@ -use std::{ - env::{ set_var, var }, - io::{ stdout, IsTerminal } -}; +use std::env::{ set_var, var }; use pico_args::Arguments; @@ -39,8 +36,6 @@ fn main() { set_var("TMUX", ""); } - if !stdout().is_terminal() { error::not_terminal(); } - let subcommand = args.subcommand().unwrap(); // invoke subcommand function @@ -64,6 +59,9 @@ fn main() { Some("n" | "new") => command::share::new(&mut args), + Some("root") + => command::session::root(), + Some("s" | "switch") => command::session::switch(&mut args), diff --git a/src/util.rs b/src/util.rs index f6bca95..2042764 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,6 @@ use std::{ env::{ current_dir, var }, + io::{ stdout, IsTerminal }, path::PathBuf, process::exit }; @@ -49,6 +50,11 @@ pub fn session_exists>(target: S) -> bool { .success() } +/// enforce a command is being run in a terminal +pub fn terminal_enforce() { + if !stdout().is_terminal() { error::not_terminal(); } +} + /// attempt to return the repo name or exit pub fn repo_fallback() -> String { let repo = repo_root(current_dir().unwrap());