From 07bd6af1a9864dc2c38551c3b2b3669d20c03153 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 14 Apr 2023 16:38:34 -0400 Subject: [PATCH] various changes --- Cargo.toml | 2 +- src/command.rs | 125 ++++++++++++++++++++++++++++++++++++++++++---- src/error.rs | 22 ++++++++ src/main.rs | 20 +++----- src/midsession.rs | 8 +++ src/tui.rs | 5 ++ 6 files changed, 156 insertions(+), 26 deletions(-) create mode 100644 src/error.rs create mode 100644 src/midsession.rs create mode 100644 src/tui.rs diff --git a/Cargo.toml b/Cargo.toml index 92be1f2..fd88bc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "remux" -version = "0.0.1" +version = "0.0.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/command.rs b/src/command.rs index a36bf34..390546e 100644 --- a/src/command.rs +++ b/src/command.rs @@ -7,9 +7,116 @@ use pico_args::Arguments; use termion::{ color, style }; use tmux_interface::TmuxCommand; +use crate::error; use crate::util; +pub fn help(pargs: &mut Arguments) { + let topic = pargs.subcommand().unwrap(); + + match topic.as_deref() { + None => { // program + println!("remux v{}", env!("CARGO_PKG_VERSION")); + println!("Valerie Wolfe "); + println!("A command wrapper for tmux written in Rust.\n"); + + println!("usage: remux []\n"); + + println!("Commands:"); + println!(" help Show help text for remux or a specific command"); + println!(" attach Attach to an existing tmux session"); + println!(" has Check if a tmux session exists"); + println!(" list Pretty-print all tmux sessions"); + println!(" new Create a new tmux session"); + + println!("\nUse 'remux help ' to see detailed help text for each command."); + }, + + + Some("a") | // attach + Some("attach") => { + println!("remux attach"); + println!("Attach to an existing session.\n"); + + println!("usage: remux attach [window]\n"); + + println!("args:"); + println!(" The session to attach to"); + println!(" [window] Optionally focus a window in the given session"); + }, + + // has + Some("has") => { + println!("remux has"); + println!("Check if the target session exists.\n"); + + println!("usage: remux has [flags] \n"); + + println!("args:"); + println!(" The session to check for\n"); + + println!("flags:"); + println!(" -q, --quiet Display no text; exit code only"); + }, + + Some("l") | // list + Some("ls") | + Some("list") => { + println!("remux list"); + println!("Pretty-print all tmux sessions.\n"); + + println!("usage: remux list"); + }, + + Some("n") | // new + Some("new") => { + println!("remux new"); + println!("Create a new tmux session.\n"); + + println!("usage: remux new [flags] [command]\n"); + + println!("args:"); + println!(" <title> The title of the new session"); + println!(" [command] The shell command to run\n"); + + println!("flags:"); + println!(" -t, --target <dir> Sets the target directory for the new session."); + }, + + // not found + _ => error::no_help(topic.unwrap()) + } +} + pub fn attach(pargs: &mut Arguments) { + let args = pargs.clone().finish(); + let target = args.get(0).unwrap().to_string_lossy(); + let window = args.get(1); + + if window.is_some() { + let target = window.unwrap().to_string_lossy(); + let tmux = TmuxCommand::new(); + tmux + .select_window() + .target_window(target) + .output().ok(); + } + + let tmux = TmuxCommand::new(); + let exists = tmux + .has_session() + .target_session(target.clone()) + .output().unwrap(); + if !exists.success() { error::no_target(target.to_string()); } + + tmux + .attach_session() + .target_session(target) + .output().ok(); +} + +pub fn has(pargs: &mut Arguments) { + let quiet = pargs.contains(["-q", "--quiet"]); + let args = pargs.clone().finish(); let target = args.get(0).unwrap().to_string_lossy(); @@ -18,14 +125,10 @@ pub fn attach(pargs: &mut Arguments) { .has_session() .target_session(target.clone()) .output().unwrap(); - if !exists.success() { - println!("no session \"{target}\" exists!"); - exit(2); - } - tmux - .attach_session() - .target_session(target) - .output().ok(); + + let success = exists.success(); + if !quiet { println!("session \"{target}\" {}.", if success { "exists" } else { "does not exist" }); } + exit( if success { 0 } else { 1 }); } pub fn list() { @@ -54,16 +157,16 @@ pub fn list() { pub fn new(pargs: &mut Arguments) { use pico_args::Error; - let target_dir: Result<String, Error> = pargs.value_from_str("--target"); - let command: Result<String, Error> = pargs.value_from_str("-c"); + let target_dir: Result<String, Error> = pargs.value_from_str(["-t", "--target"]); let args = pargs.clone().finish(); let title = args.get(0).unwrap().to_string_lossy(); + let command = args.get(1); let tmux = TmuxCommand::new(); let mut new = tmux.new_session(); - if command.is_ok() { new.shell_command(command.unwrap()) } else { &mut new } + if command.is_some() { new.shell_command(command.unwrap().to_string_lossy()) } else { &mut new } .group_name(title) .attach() .start_directory(target_dir.unwrap_or(current_dir().unwrap().to_string_lossy().to_string())) diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..81003c7 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,22 @@ +use std::process::exit; + +pub fn no_subcommand(subcommand: String) { + println!("remux: no command match for \"{subcommand}\""); + exit(1); +} + +pub fn no_target(target: String) { + println!("remux: no session \"{target}\" exists"); + exit(2); +} +pub fn no_sessions() { + println!("remux: no sessions running"); + println!("use 'remux n <title>' to create a new session"); + exit(2); +} + +pub fn no_help(topic: String) { + println!("remux: no help for \"{topic}\""); + exit(3); +} + diff --git a/src/main.rs b/src/main.rs index 1cf3369..d69b784 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ -use std::process::exit; use pico_args::Arguments; mod command; +mod error; mod util; fn main() { @@ -14,11 +14,14 @@ fn main() { match subcommand.as_deref() { Some("h") | // help text - Some("help") => help_text(), + Some("help") => command::help(&mut args), Some("a") | // attach Some("attach") => command::attach(&mut args), + Some("has") => command::has(&mut args), + + None | Some("l") | // list Some("ls") | Some("list") => command::list(), @@ -26,18 +29,7 @@ fn main() { Some("n") | // new Some("new") => command::new(&mut args), - None | // none should do something else later - _ => { - println!("no command match for \"{}\"\n", subcommand.unwrap()); - help_text(); - exit(1); - } + _ => error::no_subcommand(subcommand.unwrap()) } } -fn help_text() { - println!("remux v{}", env!("CARGO_PKG_VERSION")); - println!("Valerie Wolfe <sleeplessval@gmail.com>"); - println!("A command wrapper for tmux written in Rust."); -} - diff --git a/src/midsession.rs b/src/midsession.rs new file mode 100644 index 0000000..4348c48 --- /dev/null +++ b/src/midsession.rs @@ -0,0 +1,8 @@ +use std::env::var; + +use tmux_interface::Command; + +pub fn new() { + +} + diff --git a/src/tui.rs b/src/tui.rs new file mode 100644 index 0000000..bb77432 --- /dev/null +++ b/src/tui.rs @@ -0,0 +1,5 @@ + +pub fn main() { + println!("do tui shit here"); +} +