remux/src/command.rs

160 lines
3.8 KiB
Rust
Raw Normal View History

2023-04-06 13:34:32 -04:00
use std::{
env::current_dir,
process::exit
};
use pico_args::Arguments;
use termion::{ color, style };
use tmux_interface::TmuxCommand;
2023-04-14 16:38:34 -04:00
use crate::error;
2023-04-06 13:34:32 -04:00
use crate::util;
pub fn attach(pargs: &mut Arguments) {
util::prevent_nest();
// get optional flags
let read_only = pargs.contains(["-r", "--readonly"]);
let detach_other = pargs.contains(["-d", "--detach"]);
// get target and error out if not provided
2023-04-06 13:34:32 -04:00
let args = pargs.clone().finish();
if args.len() < 1 { error::missing_target(); }
2023-04-06 13:34:32 -04:00
let target = args.get(0).unwrap().to_string_lossy();
2023-04-14 16:38:34 -04:00
let window = args.get(1);
// focus window if provided
2023-04-14 16:38:34 -04:00
if window.is_some() {
let target = window.unwrap().to_string_lossy();
let tmux = TmuxCommand::new();
tmux
.select_window()
.target_window(target)
.output().ok();
}
2023-04-06 13:34:32 -04:00
// command
2023-04-06 13:34:32 -04:00
let tmux = TmuxCommand::new();
let exists = tmux
.has_session()
.target_session(target.clone())
.output().unwrap();
2023-04-14 16:38:34 -04:00
if !exists.success() { error::no_target(target.to_string()); }
let mut attach = tmux.attach_session();
// handle optional flags
if read_only { attach.read_only(); }
if detach_other { attach.detach_other(); }
attach
.target_session(target)
.output().ok();
}
pub fn detach(pargs: &mut Arguments) {
// get target and error out if not provided
let args = pargs.clone().finish();
if args.len() < 1 { error::missing_target(); }
let target = args.get(0).unwrap().to_string_lossy();
// command
let tmux = TmuxCommand::new();
let exists = tmux
.has_session()
.target_session(target.clone())
.output().unwrap();
if !exists.success() { error::no_target(target.to_string()); }
2023-04-06 13:34:32 -04:00
tmux
.detach_client()
2023-04-06 13:34:32 -04:00
.target_session(target)
.output().ok();
}
2023-04-14 16:38:34 -04:00
pub fn has(pargs: &mut Arguments) {
// get optional flag
2023-04-14 16:38:34 -04:00
let quiet = pargs.contains(["-q", "--quiet"]);
// get target and error out if not provided
2023-04-14 16:38:34 -04:00
let args = pargs.clone().finish();
if args.len() < 1 { error::missing_target(); }
2023-04-14 16:38:34 -04:00
let target = args.get(0).unwrap().to_string_lossy();
// command
2023-04-14 16:38:34 -04:00
let tmux = TmuxCommand::new();
let exists = tmux
.has_session()
.target_session(target.clone())
.output().unwrap();
let success = exists.success();
// handle optional flag
// inverted; print text if NOT quiet
2023-04-14 16:38:34 -04:00
if !quiet { println!("session \"{target}\" {}.", if success { "exists" } else { "does not exist" }); }
// exit codes for scripts to use
2023-04-14 16:38:34 -04:00
exit( if success { 0 } else { 1 });
}
2023-04-06 13:34:32 -04:00
pub fn list() {
// get session list
let sessions = util::get_sessions().unwrap_or(Vec::new());
2023-04-06 13:34:32 -04:00
// handle empty case
if sessions.len() == 0 {
println!("no sessions");
return;
}
2023-04-06 13:34:32 -04:00
// iterate over pretty print
println!("sessions:");
for session in sessions.into_iter() {
2023-04-06 13:34:32 -04:00
let group = session.group.unwrap_or("[untitled]".to_string());
let id = session.id.unwrap();
let attached = session.attached.unwrap_or(0) > 0;
println!(
" {group} ({bold}{blue}{id}{reset}) {bold}{green}{attach_sym}{reset}",
// values
2023-06-10 16:16:28 -04:00
attach_sym = if attached { "󰌹" } else {""},
2023-04-06 13:34:32 -04:00
// formatting
bold = style::Bold,
blue = color::Fg(color::Blue),
green = color::Fg(color::LightGreen),
reset = style::Reset
);
}
}
pub fn new(pargs: &mut Arguments) {
use pico_args::Error;
// show nest message
util::prevent_nest();
// get optional flag
2023-04-14 16:38:34 -04:00
let target_dir: Result<String, Error> = pargs.value_from_str(["-t", "--target"]);
2023-04-06 13:34:32 -04:00
// get target and error out if not provided
2023-04-06 13:34:32 -04:00
let args = pargs.clone().finish();
if args.len() < 1 { error::missing_target(); }
// get target session and optional command
2023-04-06 13:34:32 -04:00
let title = args.get(0).unwrap().to_string_lossy();
2023-04-14 16:38:34 -04:00
let command = args.get(1);
2023-04-06 13:34:32 -04:00
let tmux = TmuxCommand::new();
let mut new = tmux.new_session();
2023-05-01 14:25:53 -04:00
if command.is_some() { new.shell_command(command.unwrap().to_string_lossy()); }
new
2023-04-06 13:34:32 -04:00
.group_name(title)
.attach()
.start_directory(target_dir.unwrap_or(current_dir().unwrap().to_string_lossy().to_string()))
.output().ok();
}