various changes

This commit is contained in:
Valerie Wolfe 2023-04-14 16:38:34 -04:00
parent 97fdb206c9
commit 07bd6af1a9
6 changed files with 156 additions and 26 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "remux" name = "remux"
version = "0.0.1" version = "0.0.3"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -7,9 +7,116 @@ use pico_args::Arguments;
use termion::{ color, style }; use termion::{ color, style };
use tmux_interface::TmuxCommand; use tmux_interface::TmuxCommand;
use crate::error;
use crate::util; 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 <sleeplessval@gmail.com>");
println!("A command wrapper for tmux written in Rust.\n");
println!("usage: remux <command> [<args>]\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 <command>' 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 <session> [window]\n");
println!("args:");
println!(" <session> 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] <session>\n");
println!("args:");
println!(" <session> 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] <title> [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) { 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 args = pargs.clone().finish();
let target = args.get(0).unwrap().to_string_lossy(); let target = args.get(0).unwrap().to_string_lossy();
@ -18,14 +125,10 @@ pub fn attach(pargs: &mut Arguments) {
.has_session() .has_session()
.target_session(target.clone()) .target_session(target.clone())
.output().unwrap(); .output().unwrap();
if !exists.success() {
println!("no session \"{target}\" exists!"); let success = exists.success();
exit(2); if !quiet { println!("session \"{target}\" {}.", if success { "exists" } else { "does not exist" }); }
} exit( if success { 0 } else { 1 });
tmux
.attach_session()
.target_session(target)
.output().ok();
} }
pub fn list() { pub fn list() {
@ -54,16 +157,16 @@ pub fn list() {
pub fn new(pargs: &mut Arguments) { pub fn new(pargs: &mut Arguments) {
use pico_args::Error; use pico_args::Error;
let target_dir: Result<String, Error> = pargs.value_from_str("--target"); let target_dir: Result<String, Error> = pargs.value_from_str(["-t", "--target"]);
let command: Result<String, Error> = pargs.value_from_str("-c");
let args = pargs.clone().finish(); let args = pargs.clone().finish();
let title = args.get(0).unwrap().to_string_lossy(); let title = args.get(0).unwrap().to_string_lossy();
let command = args.get(1);
let tmux = TmuxCommand::new(); let tmux = TmuxCommand::new();
let mut new = tmux.new_session(); 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) .group_name(title)
.attach() .attach()
.start_directory(target_dir.unwrap_or(current_dir().unwrap().to_string_lossy().to_string())) .start_directory(target_dir.unwrap_or(current_dir().unwrap().to_string_lossy().to_string()))

22
src/error.rs Normal file
View file

@ -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);
}

View file

@ -1,8 +1,8 @@
use std::process::exit;
use pico_args::Arguments; use pico_args::Arguments;
mod command; mod command;
mod error;
mod util; mod util;
fn main() { fn main() {
@ -14,11 +14,14 @@ fn main() {
match subcommand.as_deref() { match subcommand.as_deref() {
Some("h") | // help text Some("h") | // help text
Some("help") => help_text(), Some("help") => command::help(&mut args),
Some("a") | // attach Some("a") | // attach
Some("attach") => command::attach(&mut args), Some("attach") => command::attach(&mut args),
Some("has") => command::has(&mut args),
None |
Some("l") | // list Some("l") | // list
Some("ls") | Some("ls") |
Some("list") => command::list(), Some("list") => command::list(),
@ -26,18 +29,7 @@ fn main() {
Some("n") | // new Some("n") | // new
Some("new") => command::new(&mut args), Some("new") => command::new(&mut args),
None | // none should do something else later _ => error::no_subcommand(subcommand.unwrap())
_ => {
println!("no command match for \"{}\"\n", subcommand.unwrap());
help_text();
exit(1);
}
} }
} }
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.");
}

8
src/midsession.rs Normal file
View file

@ -0,0 +1,8 @@
use std::env::var;
use tmux_interface::Command;
pub fn new() {
}

5
src/tui.rs Normal file
View file

@ -0,0 +1,5 @@
pub fn main() {
println!("do tui shit here");
}