added nesting flag

This commit is contained in:
Valerie Wolfe 2024-02-04 16:07:24 -05:00
parent 2dd4e35600
commit 796d8744c5
4 changed files with 28 additions and 3 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "remux"
version = "0.1.0"
version = "0.1.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -30,8 +30,15 @@ pub fn missing_target() {
exit(4);
}
/// non-terminal environment prevention; code 5
pub fn not_terminal() {
println!("remux: not running from a terminal");
exit(5);
}
/// tried to nest while not in a session; code 6
pub fn not_nesting() {
println!("remux: cannot use nesting flag outside a TMUX session");
exit(6);
}

View file

@ -1,4 +1,7 @@
use std::io::{ stdout, IsTerminal };
use std::{
env::{ set_var, var },
io::{ stdout, IsTerminal }
};
use pico_args::Arguments;
@ -10,17 +13,28 @@ mod util;
use help::help;
fn main() {
// collect args
let mut args = Arguments::from_env();
// consume flags
if args.contains(["-h", "--help"]) {
help(&mut args);
return;
}
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", "");
}
if !stdout().is_terminal() { error::not_terminal(); }
let subcommand = args.subcommand().unwrap();
// invoke subcommand function
match subcommand.as_deref() {
Some("h" | "help")
=> help(&mut args),
@ -44,5 +58,9 @@ fn main() {
_
=> error::no_subcommand(subcommand.unwrap())
}
if nesting {
set_var("TMUX", tmux_var.unwrap());
}
}

View file

@ -22,7 +22,7 @@ pub fn get_sessions() -> Option<Vec<Session>> {
pub fn prevent_nest() {
let tmux = var("TMUX").ok();
if tmux.is_some() && tmux.unwrap() != "" {
println!("Sessions should be nested with care; unset TMUX to allow.");
println!("Sessions should be nested with care; unset TMUX or use the '-n' flag to allow.");
exit(1);
}
}