added working directory flag

This commit is contained in:
Valerie Wolfe 2024-09-23 09:02:40 -04:00
parent 8449ae00d6
commit d3a408ad33
5 changed files with 35 additions and 11 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "remux" name = "remux"
version = "0.3.6" version = "0.4.0"
edition = "2021" edition = "2021"
authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ] authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ]
description = "A friendly command shortener for tmux" description = "A friendly command shortener for tmux"

View file

@ -1,6 +1,5 @@
.Dd $Mdocdate$ .Dd $Mdocdate$
.Dt REMUX 1 .Dt REMUX 1
.Os
.Sh NAME .Sh NAME
.Nm remux .Nm remux
.Nd a command shortener for .Nd a command shortener for
@ -8,6 +7,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm remux .Nm remux
.Op Fl dhnqrtv .Op Fl dhnqrtv
.Op Fl D Ar path
.Op Ar command .Op Ar command
.Op args... .Op args...
.Sh DESCRIPTION .Sh DESCRIPTION
@ -35,6 +35,8 @@ Attaches to an existing session.
.Bl -tag -width Ds -compact .Bl -tag -width Ds -compact
.It Fl d , Fl -detach .It Fl d , Fl -detach
Detach all other connections to the session. 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 .It Fl n , Fl -nest
Allow nesting (attaching a session from inside another session). Allow nesting (attaching a session from inside another session).
.It Fl r , Fl -read-only .It Fl r , Fl -read-only

View file

@ -75,3 +75,10 @@ pub fn not_in_session(cmd: &'static str) -> ! {
exit(7); 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);
}

View file

@ -10,6 +10,7 @@ pub static QUIET: Flag = ["-q", "--quiet"];
pub static READ_ONLY: Flag = [ "-r", "--read-only" ]; pub static READ_ONLY: Flag = [ "-r", "--read-only" ];
pub static TARGET: Flag = [ "-t", "--target" ]; pub static TARGET: Flag = [ "-t", "--target" ];
pub static VERSION: Flag = [ "-v", "--version" ]; pub static VERSION: Flag = [ "-v", "--version" ];
pub static WORKING_DIR: Flag = [ "-D", "--dir" ];
pub struct Flags { pub struct Flags {
pub detached: bool, pub detached: bool,
@ -17,6 +18,7 @@ pub struct Flags {
pub quiet: bool, pub quiet: bool,
pub read_only: bool, pub read_only: bool,
pub target: Option<String>, pub target: Option<String>,
pub working_dir: Option<String>
} }
impl Flags { impl Flags {
@ -27,13 +29,15 @@ impl Flags {
let quiet = args.contains(QUIET); let quiet = args.contains(QUIET);
let read_only = args.contains(READ_ONLY); let read_only = args.contains(READ_ONLY);
let target = args.value_from_str(TARGET).ok(); let target = args.value_from_str(TARGET).ok();
let working_dir = args.value_from_str(WORKING_DIR).ok();
Flags { Flags {
detached, detached,
nested, nested,
quiet, quiet,
read_only, read_only,
target target,
working_dir
} }
} }
@ -43,7 +47,8 @@ impl Flags {
nested: self.nested, nested: self.nested,
quiet: self.quiet, quiet: self.quiet,
read_only: self.read_only, read_only: self.read_only,
target: None target: self.target.clone(),
working_dir: self.working_dir.clone()
} }
} }

View file

@ -29,6 +29,9 @@ impl State<'_> {
let flags = Flags::from(args); let flags = Flags::from(args);
let tmux_var = env::var(TMUX).ok(); let tmux_var = env::var(TMUX).ok();
let session = tmux_var.is_some(); 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 title = if session { message(MSG_SESSION_NAME) } else { None };
let repository = Repository::find(); let repository = Repository::find();
@ -62,6 +65,13 @@ impl State<'_> {
if !self.session { error::not_in_session(cmd); } 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<String> { self.args.subcommand().unwrap_or(None) } pub fn target(&mut self) -> Option<String> { self.args.subcommand().unwrap_or(None) }
pub fn target_title(&mut self) -> Option<String> { pub fn target_title(&mut self) -> Option<String> {
let from_args = self.target(); let from_args = self.target();