Skip to content

Commit

Permalink
Replace homedir with shellexpand
Browse files Browse the repository at this point in the history
This also gives more flexibility in the commands used (any shell
variable now valid) in cmd files.
  • Loading branch information
Ganneff committed Jul 29, 2023
1 parent e052543 commit be30abd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 29 deletions.
58 changes: 56 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lazy_static = "^1.4"
log = "^0.4"
quit = "^2.0"
rand = "^0.8"
shellexpand = { version = "3.1.0", features = ["full"] }
shlex = "1.1.0"
thiserror = "^1.0"
tmux_interface = { version = "^0.3.1", features = ["tmux_2_8", "cmd_alias"], default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Need to adapt docs from the old [README.org](old/README.org) to here.
- [X] k - kill session
- [X] -n - Open sessions to same hosts as existing session instead of
just attaching to that existing session
- [X] Support same environment variables as shell tm
- [X] Support same environment variables as shell tm (supports any now)
- [X] Simple config files (no ending)
- [X] Allows LIST command, recursively
- [X] Support ++TMREPLACETM++
Expand Down
49 changes: 23 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use directories::UserDirs;
use fehler::{throw, throws};
use home_dir::HomeDirExt;
//use home_dir::HomeDirExt;
use itertools::Itertools;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
//use shellexpand::full;
use shlex::Shlex;
use std::{
env,
Expand Down Expand Up @@ -688,15 +689,18 @@ impl Session {
if line.contains("new-window") {
tmwin += 1;
}
let modline = line
.replace("SESSION", &self.sesname)
.replace("$HOME", "~/")
.replace("${HOME}", "~/")
.replace("TMWIN", &tmwin.to_string())
.expand_home()?
.into_os_string()
.into_string()
.expect("String convert failed");
let modline = shellexpand::full(
&line
.replace("SESSION", &self.sesname)
.replace("$HOME", "~/")
.replace("${HOME}", "~/")
.replace("TMWIN", &tmwin.to_string()),
)?
.to_string();
// .expand_home()?
// .into_os_string()
// .into_string()
// .expect("String convert failed");
self.targets.push(modline);
}
}
Expand Down Expand Up @@ -1189,26 +1193,19 @@ fn parse_line(line: &str, replace: &Option<String>, current_dir: &Path) -> Resul
// but obviously people may mistype and have a single LIST
// in a line.
// Also, ~ and $HOME/${HOME} expansion are supported.
let cmd: String = cmdparser
.next()
.ok_or_else(|| anyhow!("Empty LIST found - no command given"))?
.replace("$HOME", "~/")
.replace("${HOME}", "~/")
.expand_home()?
.into_os_string()
.into_string()
.expect("String convert failed");
let cmd: String = shellexpand::full(
&cmdparser
.next()
.ok_or_else(|| anyhow!("Empty LIST found - no command given"))?
.replace("$HOME", "~/")
.replace("${HOME}", "~/"),
)?
.to_string();
// Next we want the arguments.
// Also, ~ and $HOME/${HOME} expansion are supported.
let args: Vec<String> = cmdparser
.map(|l| l.replace("$HOME", "~/").replace("${HOME}", "~/"))
.map(|l| {
l.expand_home()
.expect("Could not successfully expand ~ for arguments of LIST call")
.into_os_string()
.into_string()
.expect("String convert failed")
})
.map(|l| shellexpand::full(&l).expect("Could not expand").to_string())
.collect();
debug!("cmd is {}", cmd);
debug!("args are {:?}", args);
Expand Down

0 comments on commit be30abd

Please sign in to comment.