Compare commits

..

5 commits

4 changed files with 66 additions and 21 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "remux" name = "remux"
version = "0.1.4" version = "0.2.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,5 +1,6 @@
use std::{ use std::{
env::current_dir, env::current_dir,
ffi::OsString,
process::exit process::exit
}; };
@ -17,11 +18,19 @@ 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
target = util::repo_fallback();
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 +42,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 +65,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 +79,18 @@ 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
target = util::repo_fallback();
} 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
@ -127,11 +141,18 @@ pub fn new(pargs: &mut Arguments) {
// get target and error out if not provided // get target and error out if not provided
let args = pargs.clone().finish(); let args = pargs.clone().finish();
if args.len() < 1 { error::missing_target(); }
// get target session and optional command // collect name and command arguments
let title = args.get(0).unwrap().to_string_lossy(); let title: String;
let command = args.get(1); let command: Option<&OsString>;
if args.len() < 1 {
// missing name will attempt to fall back to repository
title = util::repo_fallback();
command = None;
} else {
title = args.get(0).unwrap().to_string_lossy().to_string();
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

@ -1,5 +1,6 @@
use std::{ use std::{
env::var, env::{ current_dir, var },
path::PathBuf,
process::exit process::exit
}; };
@ -8,6 +9,8 @@ use tmux_interface::{
variables::session::session::SESSION_ALL variables::session::session::SESSION_ALL
}; };
use crate::error;
/// return a Vec of all sessions or None /// return a Vec of all sessions or None
pub fn get_sessions() -> Option<Vec<Session>> { pub fn get_sessions() -> Option<Vec<Session>> {
let i_sessions = Sessions::get(SESSION_ALL); let i_sessions = Sessions::get(SESSION_ALL);
@ -28,11 +31,31 @@ 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()
} }
/// attempt to return the repo name or exit
pub fn repo_fallback() -> String {
let repo = repo_root(current_dir().unwrap());
if repo.is_none() { error::missing_target(); }
let target = repo.unwrap().file_name().unwrap().to_string_lossy().to_string();
target
}
/// recursively attempt to find a git root directory
pub fn repo_root(path: PathBuf) -> Option<PathBuf> {
// if .git dir is found, return
if path.join(".git").exists() { return Some(path); }
// otherwise, attempt to traverse
let parent = path.parent();
if let Some(parent) = parent { repo_root(parent.to_path_buf()) }
else { None }
}