more robust source documentation, and targeted commands now gracefully fail without a target

This commit is contained in:
Valerie Wolfe 2023-06-19 10:42:52 -04:00
parent a15bee37c6
commit b257e11b02
4 changed files with 37 additions and 4 deletions

View file

@ -105,14 +105,17 @@ pub fn help(pargs: &mut Arguments) {
pub fn attach(pargs: &mut Arguments) { pub fn attach(pargs: &mut Arguments) {
util::prevent_nest(); util::prevent_nest();
// get optional flags
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"]);
// 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(); }
let target = args.get(0).unwrap().to_string_lossy(); let target = args.get(0).unwrap().to_string_lossy();
let window = args.get(1); let window = args.get(1);
// 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(); let tmux = TmuxCommand::new();
@ -122,6 +125,7 @@ pub fn attach(pargs: &mut Arguments) {
.output().ok(); .output().ok();
} }
// command
let tmux = TmuxCommand::new(); let tmux = TmuxCommand::new();
let exists = tmux let exists = tmux
.has_session() .has_session()
@ -131,6 +135,7 @@ pub fn attach(pargs: &mut Arguments) {
let mut attach = tmux.attach_session(); let mut attach = tmux.attach_session();
// 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(); }
@ -140,10 +145,12 @@ pub fn attach(pargs: &mut Arguments) {
} }
pub fn detach(pargs: &mut Arguments) { pub fn detach(pargs: &mut Arguments) {
// 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(); }
let target = args.get(0).unwrap().to_string_lossy(); let target = args.get(0).unwrap().to_string_lossy();
// command
let tmux = TmuxCommand::new(); let tmux = TmuxCommand::new();
let exists = tmux let exists = tmux
.has_session() .has_session()
@ -158,11 +165,15 @@ pub fn detach(pargs: &mut Arguments) {
} }
pub fn has(pargs: &mut Arguments) { pub fn has(pargs: &mut Arguments) {
// get optional flag
let quiet = pargs.contains(["-q", "--quiet"]); let quiet = pargs.contains(["-q", "--quiet"]);
// 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(); }
let target = args.get(0).unwrap().to_string_lossy(); let target = args.get(0).unwrap().to_string_lossy();
// command
let tmux = TmuxCommand::new(); let tmux = TmuxCommand::new();
let exists = tmux let exists = tmux
.has_session() .has_session()
@ -171,19 +182,25 @@ pub fn has(pargs: &mut Arguments) {
let success = exists.success(); let success = exists.success();
// handle optional flag
// inverted; print text if NOT quiet
if !quiet { println!("session \"{target}\" {}.", if success { "exists" } else { "does not exist" }); } if !quiet { println!("session \"{target}\" {}.", if success { "exists" } else { "does not exist" }); }
// exit codes for scripts to use
exit( if success { 0 } else { 1 }); exit( if success { 0 } else { 1 });
} }
pub fn list() { pub fn list() {
// get session list
let sessions = util::get_sessions().unwrap_or(Vec::new()); let sessions = util::get_sessions().unwrap_or(Vec::new());
// handle empty case
if sessions.len() == 0 { if sessions.len() == 0 {
println!("no sessions"); println!("no sessions");
return; return;
} }
// iterate over pretty print
println!("sessions:"); println!("sessions:");
for session in sessions.into_iter() { for session in sessions.into_iter() {
let group = session.group.unwrap_or("[untitled]".to_string()); let group = session.group.unwrap_or("[untitled]".to_string());
@ -206,11 +223,17 @@ pub fn list() {
pub fn new(pargs: &mut Arguments) { pub fn new(pargs: &mut Arguments) {
use pico_args::Error; use pico_args::Error;
// show nest message
util::prevent_nest(); util::prevent_nest();
// get optional flag
let target_dir: Result<String, Error> = pargs.value_from_str(["-t", "--target"]); let target_dir: Result<String, Error> = pargs.value_from_str(["-t", "--target"]);
// 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(); }
// get target session and optional command
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);

View file

@ -1,22 +1,32 @@
use std::process::exit; use std::process::exit;
/// no subcommand that matches user input; code 1
pub fn no_subcommand(subcommand: String) { pub fn no_subcommand(subcommand: String) {
println!("remux: no command match for \"{subcommand}\""); println!("remux: no command match for \"{subcommand}\"");
exit(1); exit(1);
} }
/// target session not found; code 2
pub fn no_target(target: String) { pub fn no_target(target: String) {
println!("remux: no session \"{target}\" exists"); println!("remux: no session \"{target}\" exists");
exit(2); exit(2);
} }
/// no sessions exist; code 2
pub fn no_sessions() { pub fn no_sessions() {
println!("remux: no sessions running"); println!("remux: no sessions running");
println!("use 'remux n <title>' to create a new session"); println!("use 'remux n <title>' to create a new session");
exit(2); exit(2);
} }
/// help topic doesn't exist; code 3
pub fn no_help(topic: String) { pub fn no_help(topic: String) {
println!("remux: no help for \"{topic}\""); println!("remux: no help for \"{topic}\"");
exit(3); exit(3);
} }
/// user provided no target; code 4
pub fn missing_target() {
println!("remux: no target provided");
exit(4);
}

View file

@ -10,8 +10,6 @@ fn main() {
let subcommand = args.subcommand().unwrap(); let subcommand = args.subcommand().unwrap();
//let tmuxvar = var("TMUX");
match subcommand.as_deref() { match subcommand.as_deref() {
Some("h" | "help") Some("h" | "help")
=> command::help(&mut args), => command::help(&mut args),

View file

@ -8,6 +8,7 @@ use tmux_interface::{
variables::session::session::SESSION_ALL variables::session::session::SESSION_ALL
}; };
/// 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);
if i_sessions.is_err() { return None; } if i_sessions.is_err() { return None; }
@ -17,6 +18,7 @@ pub fn get_sessions() -> Option<Vec<Session>> {
Some(sessions.unwrap().0) Some(sessions.unwrap().0)
} }
/// show the tmux nest text if env var is not unset
pub fn prevent_nest() { pub fn prevent_nest() {
let tmux = var("TMUX").ok(); let tmux = var("TMUX").ok();
if tmux.is_some() && tmux.unwrap() != "" { if tmux.is_some() && tmux.unwrap() != "" {