From 97fdb206c9224581f68011d9234750a575f89c7e Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 6 Apr 2023 13:34:32 -0400 Subject: [PATCH] initial commit --- .gitignore | 2 ++ Cargo.toml | 13 +++++++++ README.md | 24 +++++++++++++++++ src/command.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 43 ++++++++++++++++++++++++++++++ src/util.rs | 15 +++++++++++ 6 files changed, 169 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/command.rs create mode 100644 src/main.rs create mode 100644 src/util.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b72444 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/Cargo.lock +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..92be1f2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "remux" +version = "0.0.1" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +pico-args = { version = "0.5.0", features = [ "combined-flags", "eq-separator" ] } +ratatui = { version = "0.20.1", features = [ "termion" ] } +termion = "2.0.1" +tmux_interface = "0.2.1" + diff --git a/README.md b/README.md new file mode 100644 index 0000000..68dc395 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ + +# ReMux: a friendlier tmux wrapper + +The main point of this project is shortening commonly-used `tmux` +commands. +Attaching, pretty lists, and creating new sessions with group names +are currently implemented: + +```bash + +# new session +tmux new-session -t foo +remux n foo + +# lists +tmux ls +remux l + +# attach +tmux a -t foo +remux a foo + +``` + diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..a36bf34 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,72 @@ +use std::{ + env::current_dir, + process::exit +}; + +use pico_args::Arguments; +use termion::{ color, style }; +use tmux_interface::TmuxCommand; + +use crate::util; + +pub fn attach(pargs: &mut Arguments) { + let args = pargs.clone().finish(); + let target = args.get(0).unwrap().to_string_lossy(); + + let tmux = TmuxCommand::new(); + let exists = tmux + .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(); +} + +pub fn list() { + let sessions = util::get_sessions(); + + println!("sessions:"); + + for session in sessions.unwrap().into_iter() { + 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 + attach_sym = if attached { "" } else {""}, + // 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; + + let target_dir: Result = pargs.value_from_str("--target"); + let command: Result = pargs.value_from_str("-c"); + + let args = pargs.clone().finish(); + let title = args.get(0).unwrap().to_string_lossy(); + + let tmux = TmuxCommand::new(); + let mut new = tmux.new_session(); + + if command.is_ok() { new.shell_command(command.unwrap()) } else { &mut new } + .group_name(title) + .attach() + .start_directory(target_dir.unwrap_or(current_dir().unwrap().to_string_lossy().to_string())) + .output().ok(); +} + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1cf3369 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,43 @@ +use std::process::exit; + +use pico_args::Arguments; + +mod command; +mod util; + +fn main() { + let mut args = Arguments::from_env(); + + let subcommand = args.subcommand().unwrap(); + + //let tmuxvar = var("TMUX"); + + match subcommand.as_deref() { + Some("h") | // help text + Some("help") => help_text(), + + Some("a") | // attach + Some("attach") => command::attach(&mut args), + + Some("l") | // list + Some("ls") | + Some("list") => command::list(), + + 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); + } + } +} + +fn help_text() { + println!("remux v{}", env!("CARGO_PKG_VERSION")); + println!("Valerie Wolfe "); + println!("A command wrapper for tmux written in Rust."); +} + diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..f72c654 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,15 @@ + +use tmux_interface::{ + Session, Sessions, + variables::session::session::SESSION_ALL +}; + +pub fn get_sessions() -> Option> { + let i_sessions = Sessions::get(SESSION_ALL); + if i_sessions.is_err() { return None; } + let sessions = i_sessions.ok(); + if sessions.is_none() { return None; } + + Some(sessions.unwrap().0) +} +