Compare commits
No commits in common. "407d77812a80db68d6d3eb86f610bba45d06d8fa" and "7aa6b225de30c9f545813e593e720224d6b1e7b9" have entirely different histories.
407d77812a
...
7aa6b225de
4 changed files with 21 additions and 66 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "remux"
|
name = "remux"
|
||||||
version = "0.2.0"
|
version = "0.1.4"
|
||||||
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"
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
env::current_dir,
|
env::current_dir,
|
||||||
ffi::OsString,
|
|
||||||
process::exit
|
process::exit
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,19 +17,11 @@ 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"]);
|
||||||
|
|
||||||
// collect target and window arguments
|
// get target and error out if not provided
|
||||||
let args = pargs.clone().finish();
|
let args = pargs.clone().finish();
|
||||||
let target: String;
|
if args.len() < 1 { error::missing_target(); }
|
||||||
let window: Option<&OsString>;
|
let target = args.get(0).unwrap().to_string_lossy();
|
||||||
if args.len() < 1 {
|
let window = args.get(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() {
|
||||||
|
@ -42,8 +33,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.clone());
|
let exists = util::session_exists(target.to_string());
|
||||||
if !exists { error::no_target(target.clone()); }
|
if !exists { error::no_target(target.to_string()); }
|
||||||
|
|
||||||
// build command
|
// build command
|
||||||
let mut attach = TmuxCommand::new().attach_session();
|
let mut attach = TmuxCommand::new().attach_session();
|
||||||
|
@ -65,8 +56,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.clone());
|
let exists = util::session_exists(target.to_string());
|
||||||
if !exists { error::no_target(target.clone()); }
|
if !exists { error::no_target(target.to_string()); }
|
||||||
|
|
||||||
// build command and run
|
// build command and run
|
||||||
TmuxCommand::new()
|
TmuxCommand::new()
|
||||||
|
@ -79,18 +70,13 @@ pub fn has(pargs: &mut Arguments) {
|
||||||
// get optional flag
|
// get optional flag
|
||||||
let quiet = pargs.contains(["-q", "--quiet"]);
|
let quiet = pargs.contains(["-q", "--quiet"]);
|
||||||
|
|
||||||
// collect target argument
|
// get target and error out if not provided
|
||||||
let args = pargs.clone().finish();
|
let args = pargs.clone().finish();
|
||||||
let target: String;
|
if args.len() < 1 { error::missing_target(); }
|
||||||
if args.len() < 1 {
|
let target = args.get(0).unwrap().to_string_lossy();
|
||||||
// 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.clone());
|
let success = util::session_exists(target.to_string());
|
||||||
|
|
||||||
// handle optional flag
|
// handle optional flag
|
||||||
// inverted; print text if NOT quiet
|
// inverted; print text if NOT quiet
|
||||||
|
@ -141,18 +127,11 @@ 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(); }
|
||||||
|
|
||||||
// collect name and command arguments
|
// get target session and optional command
|
||||||
let title: String;
|
let title = args.get(0).unwrap().to_string_lossy();
|
||||||
let command: Option<&OsString>;
|
let command = args.get(1);
|
||||||
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();
|
||||||
|
|
|
@ -7,8 +7,7 @@ pub fn no_subcommand(subcommand: String) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// target session not found; code 2
|
/// target session not found; code 2
|
||||||
pub fn no_target<S: Into<String>>(target: S) {
|
pub fn no_target(target: String) {
|
||||||
let target = target.into();
|
|
||||||
println!("remux: no session \"{target}\" exists");
|
println!("remux: no session \"{target}\" exists");
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
|
29
src/util.rs
29
src/util.rs
|
@ -1,6 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
env::{ current_dir, var },
|
env::var,
|
||||||
path::PathBuf,
|
|
||||||
process::exit
|
process::exit
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -9,8 +8,6 @@ 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);
|
||||||
|
@ -31,31 +28,11 @@ pub fn prevent_nest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// check whether a target session exists
|
/// check whether a target session exists
|
||||||
pub fn session_exists<S: Into<String>>(target: S) -> bool {
|
pub fn session_exists(target: String) -> bool {
|
||||||
TmuxCommand::new()
|
TmuxCommand::new()
|
||||||
.has_session()
|
.has_session()
|
||||||
.target_session(target.into())
|
.target_session(target)
|
||||||
.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 }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue