added repo fallback to all relevant commands

This commit is contained in:
Valerie Wolfe 2024-02-28 17:49:08 -05:00
parent 90f3b9a999
commit 2ede502884
3 changed files with 39 additions and 16 deletions

View file

@ -1,5 +1,6 @@
use std::{ use std::{
env::current_dir, env::current_dir,
ffi::OsString,
process::exit process::exit
}; };
@ -17,11 +18,22 @@ pub fn attach(pargs: &mut Arguments) {
let read_only = pargs.contains(["-r", "--readonly"]); let read_only = pargs.contains(["-r", "--readonly"]);
let detach_other = pargs.contains(["-d", "--detach"]); let detach_other = pargs.contains(["-d", "--detach"]);
// get target and error out if not provided // collect target and window arguments
let args = pargs.clone().finish(); let args = pargs.clone().finish();
if args.len() < 1 { error::missing_target(); } let target: String;
let target = args.get(0).unwrap().to_string_lossy(); let window: Option<&OsString>;
let window = args.get(1); if args.len() < 1 {
// missing name will attempt to fall back to repository
let repo = util::repo_root(current_dir().unwrap());
if repo.is_none() { error::missing_target(); }
target = repo.unwrap().file_name().unwrap().to_string_lossy().to_string();
if !util::session_exists(target.clone()) { error::missing_target(); }
window = None;
} else {
target = args.get(0).unwrap().to_string_lossy().to_string();
window = args.get(1);
}
// focus window if provided // focus window if provided
if window.is_some() { if window.is_some() {
@ -33,8 +45,8 @@ pub fn attach(pargs: &mut Arguments) {
} }
// make sure the target session exists // make sure the target session exists
let exists = util::session_exists(target.to_string()); let exists = util::session_exists(target.clone());
if !exists { error::no_target(target.to_string()); } if !exists { error::no_target(target.clone()); }
// build command // build command
let mut attach = TmuxCommand::new().attach_session(); let mut attach = TmuxCommand::new().attach_session();
@ -56,8 +68,8 @@ pub fn detach(pargs: &mut Arguments) {
let target = args.get(0).unwrap().to_string_lossy(); let target = args.get(0).unwrap().to_string_lossy();
// make sure the target session exists // make sure the target session exists
let exists = util::session_exists(target.to_string()); let exists = util::session_exists(target.clone());
if !exists { error::no_target(target.to_string()); } if !exists { error::no_target(target.clone()); }
// build command and run // build command and run
TmuxCommand::new() TmuxCommand::new()
@ -70,13 +82,21 @@ pub fn has(pargs: &mut Arguments) {
// get optional flag // get optional flag
let quiet = pargs.contains(["-q", "--quiet"]); let quiet = pargs.contains(["-q", "--quiet"]);
// get target and error out if not provided // collect target argument
let args = pargs.clone().finish(); let args = pargs.clone().finish();
if args.len() < 1 { error::missing_target(); } let target: String;
let target = args.get(0).unwrap().to_string_lossy(); if args.len() < 1 {
// missing name will attempt to fall back to repository
let repo = util::repo_root(current_dir().unwrap());
if repo.is_none() { error::missing_target(); }
target = repo.unwrap().file_name().unwrap().to_string_lossy().to_string();
} else {
target = args.get(0).unwrap().to_string_lossy().to_string();
}
// run command // run command
let success = util::session_exists(target.to_string()); let success = util::session_exists(target.clone());
// handle optional flag // handle optional flag
// inverted; print text if NOT quiet // inverted; print text if NOT quiet
@ -130,16 +150,18 @@ pub fn new(pargs: &mut Arguments) {
// collect name and command arguments // collect name and command arguments
let title: String; let title: String;
let command: Option<&OsString>;
if args.len() < 1 { if args.len() < 1 {
// missing name will attempt to fall back to repository // missing name will attempt to fall back to repository
let repo = util::repo_root(current_dir().unwrap()); let repo = util::repo_root(current_dir().unwrap());
if repo.is_none() { error::missing_target(); } if repo.is_none() { error::missing_target(); }
title = repo.unwrap().file_name().unwrap().to_string_lossy().to_string(); title = repo.unwrap().file_name().unwrap().to_string_lossy().to_string();
command = None;
} else { } else {
title = args.get(0).unwrap().to_string_lossy().to_string(); title = args.get(0).unwrap().to_string_lossy().to_string();
command = args.get(1);
} }
let command = args.get(1);
// build command // build command
let mut new = TmuxCommand::new().new_session(); let mut new = TmuxCommand::new().new_session();

View file

@ -7,7 +7,8 @@ pub fn no_subcommand(subcommand: String) {
} }
/// target session not found; code 2 /// target session not found; code 2
pub fn no_target(target: String) { pub fn no_target<S: Into<String>>(target: S) {
let target = target.into();
println!("remux: no session \"{target}\" exists"); println!("remux: no session \"{target}\" exists");
exit(2); exit(2);
} }

View file

@ -29,10 +29,10 @@ pub fn prevent_nest() {
} }
/// check whether a target session exists /// check whether a target session exists
pub fn session_exists(target: String) -> bool { pub fn session_exists<S: Into<String>>(target: S) -> bool {
TmuxCommand::new() TmuxCommand::new()
.has_session() .has_session()
.target_session(target) .target_session(target.into())
.output().unwrap() .output().unwrap()
.success() .success()
} }