2024-02-04 16:07:24 -05:00
|
|
|
use std::{
|
|
|
|
env::{ set_var, var },
|
|
|
|
io::{ stdout, IsTerminal }
|
|
|
|
};
|
2023-04-06 13:34:32 -04:00
|
|
|
|
|
|
|
use pico_args::Arguments;
|
|
|
|
|
|
|
|
mod command;
|
2023-04-14 16:38:34 -04:00
|
|
|
mod error;
|
2024-01-23 16:45:58 -05:00
|
|
|
mod help;
|
2023-04-06 13:34:32 -04:00
|
|
|
mod util;
|
|
|
|
|
2024-02-04 16:11:32 -05:00
|
|
|
use help::{ help, version };
|
|
|
|
|
|
|
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
2024-01-23 16:45:58 -05:00
|
|
|
|
2023-04-06 13:34:32 -04:00
|
|
|
fn main() {
|
2024-02-04 16:07:24 -05:00
|
|
|
// collect args
|
2023-04-06 13:34:32 -04:00
|
|
|
let mut args = Arguments::from_env();
|
|
|
|
|
2024-02-04 16:07:24 -05:00
|
|
|
// consume flags
|
2023-06-19 12:55:45 -04:00
|
|
|
if args.contains(["-h", "--help"]) {
|
2024-01-23 16:45:58 -05:00
|
|
|
help(&mut args);
|
2023-06-19 12:55:45 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-04 16:11:32 -05:00
|
|
|
if args.contains(["-v", "--version"]) {
|
|
|
|
version();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-04 16:07:24 -05:00
|
|
|
let nesting = args.contains(["-n", "--nest"]);
|
|
|
|
let tmux_var = var("TMUX").ok();
|
|
|
|
if nesting {
|
|
|
|
if tmux_var.is_none() {
|
|
|
|
error::not_nesting();
|
|
|
|
}
|
|
|
|
set_var("TMUX", "");
|
|
|
|
}
|
2023-06-19 12:55:45 -04:00
|
|
|
|
2023-08-15 21:30:10 -04:00
|
|
|
if !stdout().is_terminal() { error::not_terminal(); }
|
|
|
|
|
2023-04-06 13:34:32 -04:00
|
|
|
let subcommand = args.subcommand().unwrap();
|
|
|
|
|
2024-02-04 16:07:24 -05:00
|
|
|
// invoke subcommand function
|
2023-04-06 13:34:32 -04:00
|
|
|
match subcommand.as_deref() {
|
2023-04-20 15:31:22 -04:00
|
|
|
Some("h" | "help")
|
2024-01-23 16:45:58 -05:00
|
|
|
=> help(&mut args),
|
2023-04-06 13:34:32 -04:00
|
|
|
|
2023-04-20 15:31:22 -04:00
|
|
|
Some("a" | "attach")
|
|
|
|
=> command::attach(&mut args),
|
2023-04-06 13:34:32 -04:00
|
|
|
|
2023-05-01 10:50:47 -04:00
|
|
|
Some("d" | "detach")
|
|
|
|
=> command::detach(&mut args),
|
|
|
|
|
2023-04-20 15:31:22 -04:00
|
|
|
Some("has")
|
|
|
|
=> command::has(&mut args),
|
2023-04-14 16:38:34 -04:00
|
|
|
|
2023-04-20 15:31:22 -04:00
|
|
|
None |
|
|
|
|
Some("l" | "ls" | "list")
|
|
|
|
=> command::list(),
|
2023-04-06 13:34:32 -04:00
|
|
|
|
2023-04-20 15:31:22 -04:00
|
|
|
Some("n" | "new")
|
|
|
|
=> command::new(&mut args),
|
2023-04-06 13:34:32 -04:00
|
|
|
|
2023-04-20 15:31:22 -04:00
|
|
|
_
|
|
|
|
=> error::no_subcommand(subcommand.unwrap())
|
2023-04-06 13:34:32 -04:00
|
|
|
}
|
2024-02-04 16:07:24 -05:00
|
|
|
|
2024-02-04 16:14:34 -05:00
|
|
|
// re-set TMUX var if we unset it for nest mode
|
2024-02-04 16:07:24 -05:00
|
|
|
if nesting {
|
|
|
|
set_var("TMUX", tmux_var.unwrap());
|
|
|
|
}
|
2023-04-06 13:34:32 -04:00
|
|
|
}
|
|
|
|
|