From be30abd99dff744331226464c9aee8ca9e286b92 Mon Sep 17 00:00:00 2001 From: Joerg Jaspert Date: Sat, 29 Jul 2023 17:52:12 +0800 Subject: [PATCH] Replace homedir with shellexpand This also gives more flexibility in the commands used (any shell variable now valid) in cmd files. --- Cargo.lock | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + README.md | 2 +- src/main.rs | 49 +++++++++++++++++++++----------------------- 4 files changed, 81 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 63cc1f4..25efa16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,6 +93,18 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bstr" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a246e68bb43f6cd9db24bea052a53e40405417c5fb372e3d1a8a7f770a564ef5" +dependencies = [ + "memchr", + "once_cell", + "regex-automata 0.1.10", + "serde", +] + [[package]] name = "bumpalo" version = "3.13.0" @@ -174,7 +186,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.27", ] [[package]] @@ -204,6 +216,15 @@ dependencies = [ "dirs-sys", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs-sys" version = "0.4.1" @@ -481,6 +502,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "os_str_bytes" +version = "6.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" +dependencies = [ + "memchr", +] + [[package]] name = "overload" version = "0.1.1" @@ -590,10 +620,16 @@ checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" dependencies = [ "aho-corasick", "memchr", - "regex-automata", + "regex-automata 0.3.4", "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + [[package]] name = "regex-automata" version = "0.3.4" @@ -639,6 +675,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "serde" +version = "1.0.164" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" + [[package]] name = "sharded-slab" version = "0.1.4" @@ -648,6 +690,17 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shellexpand" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da03fa3b94cc19e3ebfc88c4229c49d8f08cdbd1228870a45f0ffdf84988e14b" +dependencies = [ + "bstr", + "dirs", + "os_str_bytes", +] + [[package]] name = "shlex" version = "1.1.0" @@ -739,6 +792,7 @@ dependencies = [ "quit", "rand", "regex", + "shellexpand", "shlex", "thiserror", "tmux_interface", diff --git a/Cargo.toml b/Cargo.toml index 9dc67bd..ddad618 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/README.md b/README.md index d0c4d91..15f0616 100644 --- a/README.md +++ b/README.md @@ -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++ diff --git a/src/main.rs b/src/main.rs index 7963ec4..748d245 100755 --- a/src/main.rs +++ b/src/main.rs @@ -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, @@ -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); } } @@ -1189,26 +1193,19 @@ fn parse_line(line: &str, replace: &Option, 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 = 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);