From d3a408ad33a59c2f463124f6a3046e0b8ff0ff4d Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 23 Sep 2024 09:02:40 -0400 Subject: [PATCH] added working directory flag --- Cargo.toml | 2 +- man/remux.1 | 4 +++- src/error.rs | 7 +++++++ src/flag.rs | 23 ++++++++++++++--------- src/state.rs | 10 ++++++++++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 76ca8fe..2e3f496 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "remux" -version = "0.3.6" +version = "0.4.0" edition = "2021" authors = [ "Valerie Wolfe " ] description = "A friendly command shortener for tmux" diff --git a/man/remux.1 b/man/remux.1 index 12564c6..beb109e 100644 --- a/man/remux.1 +++ b/man/remux.1 @@ -1,6 +1,5 @@ .Dd $Mdocdate$ .Dt REMUX 1 -.Os .Sh NAME .Nm remux .Nd a command shortener for @@ -8,6 +7,7 @@ .Sh SYNOPSIS .Nm remux .Op Fl dhnqrtv +.Op Fl D Ar path .Op Ar command .Op args... .Sh DESCRIPTION @@ -35,6 +35,8 @@ Attaches to an existing session. .Bl -tag -width Ds -compact .It Fl d , Fl -detach Detach all other connections to the session. +.It Fl D , Fl -dir Ar path +Sets the working directory for the given command. .It Fl n , Fl -nest Allow nesting (attaching a session from inside another session). .It Fl r , Fl -read-only diff --git a/src/error.rs b/src/error.rs index 11b6f0d..2f2521d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -75,3 +75,10 @@ pub fn not_in_session(cmd: &'static str) -> ! { exit(7); } + +/// failed to set working directory; code 8 +pub fn working_dir_fail(working_dir: &str) -> ! { + eprintln!("remux: failed to set working directory to '{working_dir}'"); + exit(8); +} + diff --git a/src/flag.rs b/src/flag.rs index 6cf4987..98c072f 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -3,13 +3,14 @@ use pico_args::Arguments; type Flag = [&'static str;2]; -pub static DETACH: Flag = ["-d", "--detach"]; -pub static HELP: Flag = ["-h", "--help"]; -pub static NEST: Flag = ["-n", "--nest"]; -pub static QUIET: Flag = ["-q", "--quiet"]; -pub static READ_ONLY: Flag = ["-r", "--read-only"]; -pub static TARGET: Flag = ["-t", "--target"]; -pub static VERSION: Flag = ["-v", "--version"]; +pub static DETACH: Flag = [ "-d", "--detach" ]; +pub static HELP: Flag = [ "-h", "--help" ]; +pub static NEST: Flag = [ "-n", "--nest" ]; +pub static QUIET: Flag = [ "-q", "--quiet" ]; +pub static READ_ONLY: Flag = [ "-r", "--read-only" ]; +pub static TARGET: Flag = [ "-t", "--target" ]; +pub static VERSION: Flag = [ "-v", "--version" ]; +pub static WORKING_DIR: Flag = [ "-D", "--dir" ]; pub struct Flags { pub detached: bool, @@ -17,6 +18,7 @@ pub struct Flags { pub quiet: bool, pub read_only: bool, pub target: Option, + pub working_dir: Option } impl Flags { @@ -27,13 +29,15 @@ impl Flags { let quiet = args.contains(QUIET); let read_only = args.contains(READ_ONLY); let target = args.value_from_str(TARGET).ok(); + let working_dir = args.value_from_str(WORKING_DIR).ok(); Flags { detached, nested, quiet, read_only, - target + target, + working_dir } } @@ -43,7 +47,8 @@ impl Flags { nested: self.nested, quiet: self.quiet, read_only: self.read_only, - target: None + target: self.target.clone(), + working_dir: self.working_dir.clone() } } diff --git a/src/state.rs b/src/state.rs index 93af5f4..ef5d17c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -29,6 +29,9 @@ impl State<'_> { let flags = Flags::from(args); let tmux_var = env::var(TMUX).ok(); let session = tmux_var.is_some(); + + if let Some(ref path) = flags.working_dir { State::set_working_dir(&path); } + let title = if session { message(MSG_SESSION_NAME) } else { None }; let repository = Repository::find(); @@ -62,6 +65,13 @@ impl State<'_> { if !self.session { error::not_in_session(cmd); } } + fn set_working_dir(path: &str) { + let result = env::set_current_dir(path); + if result.is_err() { + error::working_dir_fail(path); + } + } + pub fn target(&mut self) -> Option { self.args.subcommand().unwrap_or(None) } pub fn target_title(&mut self) -> Option { let from_args = self.target();