Compare commits

...

3 commits

Author SHA1 Message Date
4615777cae added help topic for switch 2024-03-08 10:23:02 -05:00
b00e15a037 switch command now must run in-session 2024-03-08 10:04:24 -05:00
5c3fb7df3f created switch command 2024-03-07 17:10:45 -05:00
7 changed files with 71 additions and 5 deletions

View file

@ -1,4 +1,4 @@
pub mod share; pub mod share;
//pub mod session; pub mod session;

34
src/command/session.rs Normal file
View file

@ -0,0 +1,34 @@
//! commands accessible from within a session
use pico_args::Arguments;
use tmux_interface::{
Tmux,
commands
};
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);
//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();
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)
.output().ok();
}

View file

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

View file

@ -37,3 +37,9 @@ pub fn not_nesting() {
exit(6); 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

@ -91,6 +91,21 @@ flags:
-n, --nest Create the session inside another session. -n, --nest Create the session inside another session.
-t, --target <dir> Sets the target directory for the new session."), -t, --target <dir> Sets the target directory for the new session."),
Some("s" | "switch")
=>
println!("remux switch
Switch to a different tmux session.
Must be run from inside a session.
usage: remux switch [flags] <title>
remux s [flags] <title>
args:
<title> The title of the session to switch to.
flags:
-r, --read-only Attach the target session as read-only."),
// not found // not found
_ => error::no_help(topic.unwrap()) _ => error::no_help(topic.unwrap())
} }

View file

@ -64,6 +64,9 @@ fn main() {
Some("n" | "new") Some("n" | "new")
=> command::share::new(&mut args), => command::share::new(&mut args),
Some("s" | "switch")
=> command::session::switch(&mut args),
_ _
=> error::no_subcommand(subcommand.unwrap()) => error::no_subcommand(subcommand.unwrap())
} }

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 /// check whether a target session exists
pub fn session_exists<S: Into<String>>(target: S) -> bool { pub fn session_exists<S: Into<String>>(target: S) -> bool {
let has_session = commands::HasSession::new() let has_session = commands::HasSession::new()
.target_session(target.into()); .target_session(target.into());
Tmux::new().add_command(has_session) Tmux::new().add_command(has_session)
.disable_echo()
.status() .status()
.unwrap() .unwrap()
.success() .success()