initial rc implementation (for 'new' and 'source' commands)

This commit is contained in:
Valerie Wolfe 2024-07-12 09:15:47 -04:00
parent cdc68986fa
commit bf67610ccb
4 changed files with 78 additions and 2 deletions

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,19 @@ 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);
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

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