more robust source documentation, and targeted commands now gracefully fail without a target
This commit is contained in:
parent
a15bee37c6
commit
b257e11b02
4 changed files with 37 additions and 4 deletions
|
@ -105,14 +105,17 @@ pub fn help(pargs: &mut Arguments) {
|
|||
pub fn attach(pargs: &mut Arguments) {
|
||||
util::prevent_nest();
|
||||
|
||||
// get optional flags
|
||||
let read_only = pargs.contains(["-r", "--readonly"]);
|
||||
let detach_other = pargs.contains(["-d", "--detach"]);
|
||||
|
||||
// get target and error out if not provided
|
||||
let args = pargs.clone().finish();
|
||||
|
||||
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() {
|
||||
let target = window.unwrap().to_string_lossy();
|
||||
let tmux = TmuxCommand::new();
|
||||
|
@ -122,6 +125,7 @@ pub fn attach(pargs: &mut Arguments) {
|
|||
.output().ok();
|
||||
}
|
||||
|
||||
// command
|
||||
let tmux = TmuxCommand::new();
|
||||
let exists = tmux
|
||||
.has_session()
|
||||
|
@ -131,6 +135,7 @@ pub fn attach(pargs: &mut Arguments) {
|
|||
|
||||
let mut attach = tmux.attach_session();
|
||||
|
||||
// handle optional flags
|
||||
if read_only { attach.read_only(); }
|
||||
if detach_other { attach.detach_other(); }
|
||||
|
||||
|
@ -140,10 +145,12 @@ pub fn attach(pargs: &mut Arguments) {
|
|||
}
|
||||
|
||||
pub fn detach(pargs: &mut Arguments) {
|
||||
// get target and error out if not provided
|
||||
let args = pargs.clone().finish();
|
||||
|
||||
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()
|
||||
|
@ -158,11 +165,15 @@ pub fn detach(pargs: &mut Arguments) {
|
|||
}
|
||||
|
||||
pub fn has(pargs: &mut Arguments) {
|
||||
// get optional flag
|
||||
let quiet = pargs.contains(["-q", "--quiet"]);
|
||||
|
||||
// get target and error out if not provided
|
||||
let args = pargs.clone().finish();
|
||||
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()
|
||||
|
@ -171,19 +182,25 @@ pub fn has(pargs: &mut Arguments) {
|
|||
|
||||
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" }); }
|
||||
|
||||
// exit codes for scripts to use
|
||||
exit( if success { 0 } else { 1 });
|
||||
}
|
||||
|
||||
pub fn list() {
|
||||
// get session list
|
||||
let sessions = util::get_sessions().unwrap_or(Vec::new());
|
||||
|
||||
// handle empty case
|
||||
if sessions.len() == 0 {
|
||||
println!("no sessions");
|
||||
return;
|
||||
}
|
||||
|
||||
// iterate over pretty print
|
||||
println!("sessions:");
|
||||
for session in sessions.into_iter() {
|
||||
let group = session.group.unwrap_or("[untitled]".to_string());
|
||||
|
@ -206,11 +223,17 @@ pub fn list() {
|
|||
pub fn new(pargs: &mut Arguments) {
|
||||
use pico_args::Error;
|
||||
|
||||
// show nest message
|
||||
util::prevent_nest();
|
||||
|
||||
// get optional flag
|
||||
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();
|
||||
if args.len() < 1 { error::missing_target(); }
|
||||
|
||||
// get target session and optional command
|
||||
let title = args.get(0).unwrap().to_string_lossy();
|
||||
let command = args.get(1);
|
||||
|
||||
|
|
10
src/error.rs
10
src/error.rs
|
@ -1,22 +1,32 @@
|
|||
use std::process::exit;
|
||||
|
||||
/// no subcommand that matches user input; code 1
|
||||
pub fn no_subcommand(subcommand: String) {
|
||||
println!("remux: no command match for \"{subcommand}\"");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/// target session not found; code 2
|
||||
pub fn no_target(target: String) {
|
||||
println!("remux: no session \"{target}\" exists");
|
||||
exit(2);
|
||||
}
|
||||
/// no sessions exist; code 2
|
||||
pub fn no_sessions() {
|
||||
println!("remux: no sessions running");
|
||||
println!("use 'remux n <title>' to create a new session");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
/// help topic doesn't exist; code 3
|
||||
pub fn no_help(topic: String) {
|
||||
println!("remux: no help for \"{topic}\"");
|
||||
exit(3);
|
||||
}
|
||||
|
||||
/// user provided no target; code 4
|
||||
pub fn missing_target() {
|
||||
println!("remux: no target provided");
|
||||
exit(4);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,6 @@ fn main() {
|
|||
|
||||
let subcommand = args.subcommand().unwrap();
|
||||
|
||||
//let tmuxvar = var("TMUX");
|
||||
|
||||
match subcommand.as_deref() {
|
||||
Some("h" | "help")
|
||||
=> command::help(&mut args),
|
||||
|
|
|
@ -8,6 +8,7 @@ use tmux_interface::{
|
|||
variables::session::session::SESSION_ALL
|
||||
};
|
||||
|
||||
/// return a Vec of all sessions or None
|
||||
pub fn get_sessions() -> Option<Vec<Session>> {
|
||||
let i_sessions = Sessions::get(SESSION_ALL);
|
||||
if i_sessions.is_err() { return None; }
|
||||
|
@ -17,6 +18,7 @@ pub fn get_sessions() -> Option<Vec<Session>> {
|
|||
Some(sessions.unwrap().0)
|
||||
}
|
||||
|
||||
/// show the tmux nest text if env var is not unset
|
||||
pub fn prevent_nest() {
|
||||
let tmux = var("TMUX").ok();
|
||||
if tmux.is_some() && tmux.unwrap() != "" {
|
||||
|
|
Loading…
Reference in a new issue