58 lines
1.2 KiB
Rust
58 lines
1.2 KiB
Rust
use std::env;
|
|
|
|
use pico_args::Arguments;
|
|
|
|
use crate::{
|
|
env::TMUX,
|
|
error,
|
|
flag::Flags,
|
|
util::session_name
|
|
};
|
|
|
|
pub struct State<'a> {
|
|
pub args: &'a mut Arguments,
|
|
pub flags: Flags,
|
|
|
|
pub session: bool,
|
|
tmux_var: Option<String>,
|
|
pub title: Option<String>
|
|
}
|
|
|
|
impl State<'_> {
|
|
|
|
pub fn new(args: &mut Arguments) -> State {
|
|
let flags = Flags::from(args);
|
|
let tmux_var = env::var(TMUX).ok();
|
|
let session = tmux_var.is_some();
|
|
let title = if session { session_name() } else { None };
|
|
|
|
State {
|
|
args,
|
|
flags,
|
|
session,
|
|
tmux_var,
|
|
title
|
|
}
|
|
}
|
|
|
|
pub fn nest_init(&self) {
|
|
if self.flags.nested {
|
|
if self.session { env::set_var(TMUX, ""); } // nesting & session => ok
|
|
else { error::not_nesting(); } // nesting & !session => error
|
|
} else if self.session { error::prevent_nest(); } // !nesting & session => error
|
|
// !nesting & !session => ok
|
|
}
|
|
|
|
pub fn nest_deinit(&self) {
|
|
if self.flags.nested && self.session {
|
|
env::set_var(TMUX, self.tmux_var.as_ref().unwrap());
|
|
}
|
|
}
|
|
|
|
pub fn session_enforce(&self, cmd: &'static str) {
|
|
if !self.session { error::not_in_session(cmd); }
|
|
}
|
|
|
|
pub fn target(&mut self) -> Option<String> { self.args.subcommand().unwrap_or(None) }
|
|
}
|
|
|