Compare commits

...

3 commits
main ... rc

6 changed files with 85 additions and 5 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "remux" name = "remux"
version = "0.3.5" version = "0.3.6"
edition = "2021" edition = "2021"
authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ] authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ]
description = "A friendly command shortener for tmux" description = "A friendly command shortener for tmux"

View file

@ -1,5 +1,6 @@
//! commands accessible from within a session //! commands accessible from within a session
use termion::{ color, style };
use tmux_interface::{ use tmux_interface::{
Tmux, Tmux,
commands commands
@ -7,6 +8,7 @@ use tmux_interface::{
use crate::{ use crate::{
error, error,
script,
state::State, state::State,
util::{ self, NULL } util::{ self, NULL }
}; };
@ -26,6 +28,27 @@ pub fn path(state: &mut State) {
} }
} }
pub fn source(state: &mut State) {
if let Some(scripts) = script::list() {
if let Some(name) = state.target() {
if let Some(path) = scripts.get(&name) {
let rc = commands::SourceFile::new()
.path(path);
Tmux::new().add_command(rc).output().ok();
}
} else {
for (name, path) in scripts.into_iter() {
println!(
"{bold}{name}{reset} ({yellow}{path:?}{reset})",
bold = style::Bold,
yellow = color::Fg(color::LightYellow),
reset = style::Reset
);
}
}
}
}
pub fn switch(state: &mut State) { pub fn switch(state: &mut State) {
util::terminal_enforce(); util::terminal_enforce();
// refuse to run outside a session // refuse to run outside a session

View file

@ -12,6 +12,7 @@ use crate::{
env::{ self, env_var }, env::{ self, env_var },
error, error,
flag, flag,
script,
state::State, state::State,
util::{ self, NULL } util::{ self, NULL }
}; };
@ -181,11 +182,22 @@ pub fn new(state: &mut State) {
let mut tmux = Tmux::new().add_command(new); let mut tmux = Tmux::new().add_command(new);
// rename window if var not empty // rename window & check for script if var not empty
if !window_name.is_empty() { if !window_name.is_empty() {
let auto_name = commands::RenameWindow::new() let auto_name = commands::RenameWindow::new()
.new_name(window_name); .new_name(&window_name);
tmux = tmux.add_command(auto_name); tmux = tmux.add_command(auto_name);
let rc_var = env_var(env::NEW_RC);
if !(rc_var.is_empty() || rc_var == "0") {
if let Some(mut scripts) = script::list() {
if let Some(path) = scripts.remove(&window_name) {
let rc = commands::SourceFile::new()
.path(path);
tmux = tmux.add_command(rc);
}
}
}
} }
tmux.stderr(NULL).output().ok(); tmux.stderr(NULL).output().ok();

View file

@ -2,9 +2,10 @@ use std::env::var;
pub type EnvVar = (&'static str, &'static str); pub type EnvVar = (&'static str, &'static str);
pub static ATTACH_SYMBOL: EnvVar = ("REMUX_ATTACH_SYMBOL", "*"); pub static ATTACH_SYMBOL: EnvVar = ("REMUX_ATTACH_SYMBOL", "*");
pub static CURRENT_SYMBOL: EnvVar = ("REMUX_CURRENT_SYMBOL", ">"); pub static CURRENT_SYMBOL: EnvVar = ("REMUX_CURRENT_SYMBOL", ">");
pub static NEW_WINDOW_NAME: EnvVar = ("REMUX_NEW_WINDOW", ""); pub static NEW_WINDOW_NAME: EnvVar = ("REMUX_NEW_WINDOW", "");
pub static NEW_RC: EnvVar = ("REMUX_NEW_RC", "1");
pub static TMUX: &str = "TMUX"; pub static TMUX: &str = "TMUX";

View file

@ -6,6 +6,7 @@ mod env;
mod error; mod error;
mod flag; mod flag;
mod help; mod help;
mod script;
mod state; mod state;
mod util; mod util;
@ -64,6 +65,9 @@ fn main() {
Some("t" | "title" | "which") Some("t" | "title" | "which")
=> command::session::title(state), => command::session::title(state),
Some("x" | "source")
=> command::session::source(&mut state),
_ _
=> error::no_subcommand(target.unwrap()) => error::no_subcommand(target.unwrap())
} }

40
src/script.rs Normal file
View file

@ -0,0 +1,40 @@
use std::{
collections::HashMap,
env,
fs::read_dir,
path::{ Path, PathBuf }
};
use crate::util::find;
pub type Scripts<'a> = HashMap<String, String>;
/// get a hashmap of scripts
pub fn list<'a>() -> Option<Scripts<'a>> {
let global: Option<PathBuf> =
if let Ok(home) = env::var("HOME") { Some(Path::new(&(home + "/.config/remux")).to_path_buf()) }
else { None };
let local = find(".remux", env::current_dir().unwrap());
if global.is_none() && local.is_none() { return None; }
let mut output = Scripts::new();
if let Some(global) = global { read(&global, &mut output); }
if let Some(local) = local { read(&local, &mut output); }
Some(output)
}
/// populate the given hashmap with scripts from the given path
fn read(path: &PathBuf, map: &mut Scripts) {
if let Ok(children) = read_dir(path) {
for child in children {
if let Ok(child) = child {
let path = child.path();
if let Some(name) = path.file_stem() {
map.insert(name.to_string_lossy().into(), path.to_string_lossy().into());
}
}
}
}
}