Skip to content

Commit 85e36e0

Browse files
committed
feat: add option to escape dollar signs
Now you can escape the dollar sign using `$$`
1 parent bb1d30c commit 85e36e0

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/commands/cmd_start/util.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ pub fn expand_vars<F>(input: &str, mut getter: F) -> Result<String>
484484
{
485485
// compile regex only once
486486
static RE: LazyLock<regex::Regex> = LazyLock::new(||
487-
regex::Regex::new(r"\$\{([^}]*)\}|\$([A-Za-z_][A-Za-z0-9_]*\b)"
487+
regex::Regex::new(r"\$\{([^}]*)\}|\$([A-Za-z_][A-Za-z0-9_]*\b|\$)"
488488
).unwrap());
489489

490490
let mut output = String::with_capacity(input.len() + 256);
@@ -508,13 +508,20 @@ pub fn expand_vars<F>(input: &str, mut getter: F) -> Result<String>
508508
// by default subtitute with empty string
509509
(name, Some(""))
510510
}
511+
} else if name == "$" {
512+
// escape the sign by just doing '$$'
513+
("", Some("$"))
511514
} else {
512515
(name, Some(""))
513516
};
514517

515-
// use default as fallback
516-
let value = getter(name)
517-
.or_else(|| default.map(|x| x.to_owned()));
518+
// if name is empty then just use default
519+
let value = if name.is_empty() {
520+
default.map(|x| x.to_owned())
521+
} else {
522+
getter(name)
523+
.or_else(|| default.map(|x| x.to_owned()))
524+
};
518525

519526
// if still None then just error out
520527
let Some(value) = value else {
@@ -608,5 +615,13 @@ mod tests {
608615
).unwrap(),
609616
"cd /home/user/.config/arcam/config.toml".to_owned()
610617
);
618+
619+
assert_eq!(
620+
expand_vars_map(
621+
"/$${x}/$$xx",
622+
&HashMap::from([])
623+
).unwrap(),
624+
"/${x}/$xx".to_owned()
625+
);
611626
}
612627
}

0 commit comments

Comments
 (0)