initial commit

This commit is contained in:
Valerie Wolfe 2023-04-06 13:34:32 -04:00
commit 97fdb206c9
6 changed files with 169 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/Cargo.lock
/target

13
Cargo.toml Normal file
View file

@ -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"

24
README.md Normal file
View file

@ -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
```

72
src/command.rs Normal file
View file

@ -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<String, Error> = pargs.value_from_str("--target");
let command: Result<String, Error> = 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();
}

43
src/main.rs Normal file
View file

@ -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 <sleeplessval@gmail.com>");
println!("A command wrapper for tmux written in Rust.");
}

15
src/util.rs Normal file
View file

@ -0,0 +1,15 @@
use tmux_interface::{
Session, Sessions,
variables::session::session::SESSION_ALL
};
pub fn get_sessions() -> Option<Vec<Session>> {
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)
}