diff --git a/src/command/session.rs b/src/command/session.rs index 0ececee..2c75ccb 100644 --- a/src/command/session.rs +++ b/src/command/session.rs @@ -1,5 +1,6 @@ //! commands accessible from within a session +use termion::{ color, style }; use tmux_interface::{ Tmux, commands @@ -7,6 +8,7 @@ use tmux_interface::{ use crate::{ error, + script, state::State, 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) { util::terminal_enforce(); // refuse to run outside a session diff --git a/src/command/share.rs b/src/command/share.rs index edae1b3..556d178 100644 --- a/src/command/share.rs +++ b/src/command/share.rs @@ -12,6 +12,7 @@ use crate::{ env::{ self, env_var }, error, flag, + script, state::State, util::{ self, NULL } }; @@ -181,11 +182,19 @@ pub fn new(state: &mut State) { 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() { let auto_name = commands::RenameWindow::new() - .new_name(window_name); + .new_name(&window_name); tmux = tmux.add_command(auto_name); + + 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(); diff --git a/src/main.rs b/src/main.rs index 9ff9e28..ac6cef6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod env; mod error; mod flag; mod help; +mod script; mod state; mod util; @@ -64,6 +65,9 @@ fn main() { Some("t" | "title" | "which") => command::session::title(state), + Some("x" | "source") + => command::session::source(&mut state), + _ => error::no_subcommand(target.unwrap()) } diff --git a/src/script.rs b/src/script.rs new file mode 100644 index 0000000..4cc8204 --- /dev/null +++ b/src/script.rs @@ -0,0 +1,40 @@ +use std::{ + collections::HashMap, + env, + fs::read_dir, + path::{ Path, PathBuf } +}; + +use crate::util::find; + +pub type Scripts<'a> = HashMap; + +/// get a hashmap of scripts +pub fn list<'a>() -> Option> { + let global: Option = + 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()); + } + } + } + } +} +