switch command now must run in-session

This commit is contained in:
Valerie Wolfe 2024-03-08 10:04:24 -05:00
parent 5c3fb7df3f
commit b00e15a037
4 changed files with 23 additions and 6 deletions

View file

@ -9,20 +9,23 @@ use tmux_interface::{
use crate::{ error, flag, util };
pub fn switch(pargs: &mut Arguments) {
// refuse to run outside a session
util::session_enforce("switch");
// consume optional flags
let read_only = pargs.contains(flag::READ_ONLY);
let detach_other = pargs.contains(flag::DETACHED);
//TODO: -d flag handling needs to be done manually
let args = pargs.clone().finish();
if args.len() < 1 { error::missing_target(); }
let target = args.get(0).unwrap().to_string_lossy().to_string();
println!("{target}");
let exists = util::session_exists(target.clone());
if !exists { error::no_target(target.clone()); }
let mut switch = commands::SwitchClient::new();
switch = switch.target_session(target);
if read_only { switch.read_only = true; }
Tmux::new()
.add_command(switch)

View file

@ -55,7 +55,7 @@ pub fn attach(pargs: &mut Arguments) {
// build dispatch
let mut tmux = Tmux::new().add_command(attach);
if let Some(select_window) = select_window { tmux = tmux.add_command(select_window); }
tmux.output().ok();
tmux.disable_echo().output().ok();
}
pub fn detach(pargs: &mut Arguments) {
@ -77,7 +77,7 @@ pub fn detach(pargs: &mut Arguments) {
.target_session(target);
Tmux::new()
.add_command(detach)
.output().ok();
.disable_echo().output().ok();
}
pub fn has(pargs: &mut Arguments) {
@ -168,7 +168,6 @@ pub fn new(pargs: &mut Arguments) {
Tmux::new()
.add_command(new)
.output().ok();
.disable_echo().output().ok();
}

View file

@ -37,3 +37,9 @@ pub fn not_nesting() {
exit(6);
}
/// tried to run a session command outside a session; code 7
pub fn not_in_session(cmd: &'static str) {
println!("remux: '{cmd}' must be run from within a session");
exit(7);
}

View file

@ -30,11 +30,20 @@ pub fn prevent_nest() {
}
}
/// enforce a command is being used in-session
pub fn session_enforce(cmd: &'static str) {
let tmux = var("TMUX").unwrap_or("".to_string());
if tmux.is_empty() {
error::not_in_session(cmd);
}
}
/// check whether a target session exists
pub fn session_exists<S: Into<String>>(target: S) -> bool {
let has_session = commands::HasSession::new()
.target_session(target.into());
Tmux::new().add_command(has_session)
.disable_echo()
.status()
.unwrap()
.success()