From f704f29751cf667303c611960f588df0b0f21306 Mon Sep 17 00:00:00 2001
From: prsabahrami
Date: Fri, 6 Sep 2024 18:11:01 -0400
Subject: [PATCH] Revert to replacement in parse_args
---
crates/deno_task_shell/src/parser.rs | 15 +++------------
crates/deno_task_shell/src/shell/commands/cd.rs | 13 +++++++++----
2 files changed, 12 insertions(+), 16 deletions(-)
diff --git a/crates/deno_task_shell/src/parser.rs b/crates/deno_task_shell/src/parser.rs
index 99ee76d..27919df 100644
--- a/crates/deno_task_shell/src/parser.rs
+++ b/crates/deno_task_shell/src/parser.rs
@@ -684,19 +684,10 @@ fn parse_word(pair: Pair) -> Result {
match part.as_rule() {
Rule::EXIT_STATUS => parts.push(WordPart::Variable("?".to_string())),
Rule::UNQUOTED_CHAR => {
- let char = part.as_str().chars().next().unwrap();
- let text = if char == '~' {
- dirs::home_dir()
- .map(|p| p.to_string_lossy().into_owned())
- .unwrap_or_else(|| "~".to_string())
- } else {
- char.to_string()
- };
-
- if let Some(WordPart::Text(ref mut existing_text)) = parts.last_mut() {
- existing_text.push_str(&text);
+ if let Some(WordPart::Text(ref mut text)) = parts.last_mut() {
+ text.push(part.as_str().chars().next().unwrap());
} else {
- parts.push(WordPart::Text(text));
+ parts.push(WordPart::Text(part.as_str().to_string()));
}
}
Rule::UNQUOTED_ESCAPE_CHAR => {
diff --git a/crates/deno_task_shell/src/shell/commands/cd.rs b/crates/deno_task_shell/src/shell/commands/cd.rs
index 5a9fd93..d5c1143 100644
--- a/crates/deno_task_shell/src/shell/commands/cd.rs
+++ b/crates/deno_task_shell/src/shell/commands/cd.rs
@@ -41,9 +41,8 @@ fn execute_cd(cwd: &Path, args: Vec) -> Result {
// create a new vector to avoid modifying the original
let mut args = args;
if args.is_empty() {
- // append homedir to args
- let home_dir = dirs::home_dir().unwrap();
- args.push(home_dir.to_string_lossy().to_string());
+ // append '~' to args
+ args.push("~".to_string());
}
let path = parse_args(args.clone())?;
let new_dir = cwd.join(&path);
@@ -64,7 +63,13 @@ fn parse_args(args: Vec) -> Result {
for arg in args {
match arg {
ArgKind::Arg(arg) => {
- paths.push(arg);
+ if arg.contains('~') {
+ let home_dir = dirs::home_dir().unwrap();
+ let arg = arg.replacen("~", home_dir.to_string_lossy().as_ref(), 1);
+ paths.push(arg);
+ } else {
+ paths.push(arg.to_string());
+ }
}
_ => arg.bail_unsupported()?,
}