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]
|
||||
name = "remux"
|
||||
version = "0.2.0"
|
||||
version = "0.1.4"
|
||||
edition = "2021"
|
||||
authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ]
|
||||
description = "A friendly command shortener for tmux"
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use std::{
|
||||
env::current_dir,
|
||||
ffi::OsString,
|
||||
process::exit
|
||||
};
|
||||
|
||||
|
@ -18,19 +17,11 @@ pub fn attach(pargs: &mut Arguments) {
|
|||
let read_only = pargs.contains(["-r", "--readonly"]);
|
||||
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 target: String;
|
||||
let window: Option<&OsString>;
|
||||
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);
|
||||
}
|
||||
if args.len() < 1 { error::missing_target(); }
|
||||
let target = args.get(0).unwrap().to_string_lossy();
|
||||
let window = args.get(1);
|
||||
|
||||
// focus window if provided
|
||||
if window.is_some() {
|
||||
|
@ -42,8 +33,8 @@ pub fn attach(pargs: &mut Arguments) {
|
|||
}
|
||||
|
||||
// make sure the target session exists
|
||||
let exists = util::session_exists(target.clone());
|
||||
if !exists { error::no_target(target.clone()); }
|
||||
let exists = util::session_exists(target.to_string());
|
||||
if !exists { error::no_target(target.to_string()); }
|
||||
|
||||
// build command
|
||||
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();
|
||||
|
||||
// make sure the target session exists
|
||||
let exists = util::session_exists(target.clone());
|
||||
if !exists { error::no_target(target.clone()); }
|
||||
let exists = util::session_exists(target.to_string());
|
||||
if !exists { error::no_target(target.to_string()); }
|
||||
|
||||
// build command and run
|
||||
TmuxCommand::new()
|
||||
|
@ -79,18 +70,13 @@ pub fn has(pargs: &mut Arguments) {
|
|||
// get optional flag
|
||||
let quiet = pargs.contains(["-q", "--quiet"]);
|
||||
|
||||
// collect target argument
|
||||
// get target and error out if not provided
|
||||
let args = pargs.clone().finish();
|
||||
let target: String;
|
||||
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();
|
||||
}
|
||||
if args.len() < 1 { error::missing_target(); }
|
||||
let target = args.get(0).unwrap().to_string_lossy();
|
||||
|
||||
// run command
|
||||
let success = util::session_exists(target.clone());
|
||||
let success = util::session_exists(target.to_string());
|
||||
|
||||
// handle optional flag
|
||||
// inverted; print text if NOT quiet
|
||||
|
@ -141,18 +127,11 @@ pub fn new(pargs: &mut Arguments) {
|
|||
|
||||
// get target and error out if not provided
|
||||
let args = pargs.clone().finish();
|
||||
if args.len() < 1 { error::missing_target(); }
|
||||
|
||||
// collect name and command arguments
|
||||
let title: String;
|
||||
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);
|
||||
}
|
||||
// get target session and optional command
|
||||
let title = args.get(0).unwrap().to_string_lossy();
|
||||
let command = args.get(1);
|
||||
|
||||
// build command
|
||||
let mut new = TmuxCommand::new().new_session();
|
||||
|
|
|
@ -7,8 +7,7 @@ pub fn no_subcommand(subcommand: String) {
|
|||
}
|
||||
|
||||
/// target session not found; code 2
|
||||
pub fn no_target<S: Into<String>>(target: S) {
|
||||
let target = target.into();
|
||||
pub fn no_target(target: String) {
|
||||
println!("remux: no session \"{target}\" exists");
|
||||
exit(2);
|
||||
}
|
||||
|
|
29
src/util.rs
29
src/util.rs
|
@ -1,6 +1,5 @@
|
|||
use std::{
|
||||
env::{ current_dir, var },
|
||||
path::PathBuf,
|
||||
env::var,
|
||||
process::exit
|
||||
};
|
||||
|
||||
|
@ -9,8 +8,6 @@ use tmux_interface::{
|
|||
variables::session::session::SESSION_ALL
|
||||
};
|
||||
|
||||
use crate::error;
|
||||
|
||||
/// return a Vec of all sessions or None
|
||||
pub fn get_sessions() -> Option<Vec<Session>> {
|
||||
let i_sessions = Sessions::get(SESSION_ALL);
|
||||
|
@ -31,31 +28,11 @@ pub fn prevent_nest() {
|
|||
}
|
||||
|
||||
/// 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()
|
||||
.has_session()
|
||||
.target_session(target.into())
|
||||
.target_session(target)
|
||||
.output().unwrap()
|
||||
.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