moved session exist check to a helper function

This commit is contained in:
Valerie Wolfe 2024-02-28 09:35:30 -05:00
parent 5abe1c827c
commit 7aa6b225de
3 changed files with 29 additions and 30 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "remux" name = "remux"
version = "0.1.3b" 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"

View file

@ -26,27 +26,24 @@ pub fn attach(pargs: &mut Arguments) {
// focus window if provided // focus window if provided
if window.is_some() { if window.is_some() {
let target = window.unwrap().to_string_lossy(); let target = window.unwrap().to_string_lossy();
let tmux = TmuxCommand::new(); TmuxCommand::new()
tmux
.select_window() .select_window()
.target_window(target) .target_window(target)
.output().ok(); .output().ok();
} }
// command // make sure the target session exists
let tmux = TmuxCommand::new(); let exists = util::session_exists(target.to_string());
let exists = tmux if !exists { error::no_target(target.to_string()); }
.has_session()
.target_session(target.clone())
.output().unwrap();
if !exists.success() { error::no_target(target.to_string()); }
let mut attach = tmux.attach_session(); // build command
let mut attach = TmuxCommand::new().attach_session();
// handle optional flags // handle optional flags
if read_only { attach.read_only(); } if read_only { attach.read_only(); }
if detach_other { attach.detach_other(); } if detach_other { attach.detach_other(); }
// run command
attach attach
.target_session(target) .target_session(target)
.output().ok(); .output().ok();
@ -58,15 +55,12 @@ pub fn detach(pargs: &mut Arguments) {
if args.len() < 1 { error::missing_target(); } if args.len() < 1 { error::missing_target(); }
let target = args.get(0).unwrap().to_string_lossy(); let target = args.get(0).unwrap().to_string_lossy();
// command // make sure the target session exists
let tmux = TmuxCommand::new(); let exists = util::session_exists(target.to_string());
let exists = tmux if !exists { error::no_target(target.to_string()); }
.has_session()
.target_session(target.clone())
.output().unwrap();
if !exists.success() { error::no_target(target.to_string()); }
tmux // build command and run
TmuxCommand::new()
.detach_client() .detach_client()
.target_session(target) .target_session(target)
.output().ok(); .output().ok();
@ -81,14 +75,8 @@ pub fn has(pargs: &mut Arguments) {
if args.len() < 1 { error::missing_target(); } if args.len() < 1 { error::missing_target(); }
let target = args.get(0).unwrap().to_string_lossy(); let target = args.get(0).unwrap().to_string_lossy();
// command // run command
let tmux = TmuxCommand::new(); let success = util::session_exists(target.to_string());
let exists = tmux
.has_session()
.target_session(target.clone())
.output().unwrap();
let success = exists.success();
// handle optional flag // handle optional flag
// inverted; print text if NOT quiet // 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 title = args.get(0).unwrap().to_string_lossy();
let command = args.get(1); let command = args.get(1);
let tmux = TmuxCommand::new(); // build command
let mut new = tmux.new_session(); let mut new = TmuxCommand::new().new_session();
// handle shell command argument
if command.is_some() { new.shell_command(command.unwrap().to_string_lossy()); } if command.is_some() { new.shell_command(command.unwrap().to_string_lossy()); }
// run command
new new
.group_name(title) .group_name(title)
.attach() .attach()

View file

@ -4,7 +4,7 @@ use std::{
}; };
use tmux_interface::{ use tmux_interface::{
Session, Sessions, Session, Sessions, TmuxCommand,
variables::session::session::SESSION_ALL 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()
}