From 7aa6b225de30c9f545813e593e720224d6b1e7b9 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 28 Feb 2024 09:35:30 -0500 Subject: [PATCH] moved session exist check to a helper function --- Cargo.toml | 2 +- src/command.rs | 46 ++++++++++++++++++---------------------------- src/util.rs | 11 ++++++++++- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f532f3e..b79a50d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "remux" -version = "0.1.3b" +version = "0.1.4" edition = "2021" authors = [ "Valerie Wolfe " ] description = "A friendly command shortener for tmux" diff --git a/src/command.rs b/src/command.rs index 8be8018..4dcad7f 100644 --- a/src/command.rs +++ b/src/command.rs @@ -26,27 +26,24 @@ pub fn attach(pargs: &mut Arguments) { // focus window if provided if window.is_some() { let target = window.unwrap().to_string_lossy(); - let tmux = TmuxCommand::new(); - tmux + TmuxCommand::new() .select_window() .target_window(target) .output().ok(); } - // command - let tmux = TmuxCommand::new(); - let exists = tmux - .has_session() - .target_session(target.clone()) - .output().unwrap(); - if !exists.success() { error::no_target(target.to_string()); } + // make sure the target session exists + let exists = util::session_exists(target.to_string()); + if !exists { error::no_target(target.to_string()); } - let mut attach = tmux.attach_session(); + // build command + let mut attach = TmuxCommand::new().attach_session(); // handle optional flags if read_only { attach.read_only(); } if detach_other { attach.detach_other(); } + // run command attach .target_session(target) .output().ok(); @@ -58,15 +55,12 @@ pub fn detach(pargs: &mut Arguments) { if args.len() < 1 { error::missing_target(); } let target = args.get(0).unwrap().to_string_lossy(); - // command - let tmux = TmuxCommand::new(); - let exists = tmux - .has_session() - .target_session(target.clone()) - .output().unwrap(); - if !exists.success() { error::no_target(target.to_string()); } + // make sure the target session exists + let exists = util::session_exists(target.to_string()); + if !exists { error::no_target(target.to_string()); } - tmux + // build command and run + TmuxCommand::new() .detach_client() .target_session(target) .output().ok(); @@ -81,14 +75,8 @@ pub fn has(pargs: &mut Arguments) { if args.len() < 1 { error::missing_target(); } let target = args.get(0).unwrap().to_string_lossy(); - // command - let tmux = TmuxCommand::new(); - let exists = tmux - .has_session() - .target_session(target.clone()) - .output().unwrap(); - - let success = exists.success(); + // run command + let success = util::session_exists(target.to_string()); // handle optional flag // inverted; print text if NOT quiet @@ -145,11 +133,13 @@ pub fn new(pargs: &mut Arguments) { let title = args.get(0).unwrap().to_string_lossy(); let command = args.get(1); - let tmux = TmuxCommand::new(); - let mut new = tmux.new_session(); + // build command + let mut new = TmuxCommand::new().new_session(); + // handle shell command argument if command.is_some() { new.shell_command(command.unwrap().to_string_lossy()); } + // run command new .group_name(title) .attach() diff --git a/src/util.rs b/src/util.rs index fa84c30..ce3c63a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,7 +4,7 @@ use std::{ }; use tmux_interface::{ - Session, Sessions, + Session, Sessions, TmuxCommand, variables::session::session::SESSION_ALL }; @@ -27,3 +27,12 @@ pub fn prevent_nest() { } } +/// check whether a target session exists +pub fn session_exists(target: String) -> bool { + TmuxCommand::new() + .has_session() + .target_session(target) + .output().unwrap() + .success() +} +