From e0ea051bf4edaea098a3d0391dceed28c5cb9b29 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 28 Feb 2024 17:55:33 -0500 Subject: [PATCH] moved repo fallback into a helper function --- src/command.rs | 15 +++------------ src/util.rs | 13 ++++++++++++- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/command.rs b/src/command.rs index 1ad7925..984f857 100644 --- a/src/command.rs +++ b/src/command.rs @@ -24,10 +24,7 @@ pub fn attach(pargs: &mut Arguments) { let window: Option<&OsString>; if args.len() < 1 { // missing name will attempt to fall back to repository - let repo = util::repo_root(current_dir().unwrap()); - if repo.is_none() { error::missing_target(); } - - target = repo.unwrap().file_name().unwrap().to_string_lossy().to_string(); + target = util::repo_fallback(); if !util::session_exists(target.clone()) { error::missing_target(); } window = None; } else { @@ -87,10 +84,7 @@ pub fn has(pargs: &mut Arguments) { let target: String; if args.len() < 1 { // missing name will attempt to fall back to repository - let repo = util::repo_root(current_dir().unwrap()); - if repo.is_none() { error::missing_target(); } - - target = repo.unwrap().file_name().unwrap().to_string_lossy().to_string(); + target = util::repo_fallback(); } else { target = args.get(0).unwrap().to_string_lossy().to_string(); } @@ -153,10 +147,7 @@ pub fn new(pargs: &mut Arguments) { let command: Option<&OsString>; if args.len() < 1 { // missing name will attempt to fall back to repository - let repo = util::repo_root(current_dir().unwrap()); - if repo.is_none() { error::missing_target(); } - - title = repo.unwrap().file_name().unwrap().to_string_lossy().to_string(); + title = util::repo_fallback(); command = None; } else { title = args.get(0).unwrap().to_string_lossy().to_string(); diff --git a/src/util.rs b/src/util.rs index 1ade5f6..e5ced2f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,5 @@ use std::{ - env::var, + env::{ current_dir, var }, path::PathBuf, process::exit }; @@ -9,6 +9,8 @@ use tmux_interface::{ variables::session::session::SESSION_ALL }; +use crate::error; + /// return a Vec of all sessions or None pub fn get_sessions() -> Option> { let i_sessions = Sessions::get(SESSION_ALL); @@ -37,6 +39,15 @@ pub fn session_exists>(target: S) -> bool { .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 { // if .git dir is found, return