Skip to content

Commit

Permalink
Replacing ~ with homedir when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Sep 6, 2024
1 parent 8e7f28e commit 9570448
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 12 additions & 3 deletions crates/deno_task_shell/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,19 @@ fn parse_word(pair: Pair<Rule>) -> Result<Word> {
match part.as_rule() {
Rule::EXIT_STATUS => parts.push(WordPart::Variable("?".to_string())),
Rule::UNQUOTED_CHAR => {
if let Some(WordPart::Text(ref mut text)) = parts.last_mut() {
text.push(part.as_str().chars().next().unwrap());
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 {
parts.push(WordPart::Text(part.as_str().to_string()));
char.to_string()
};

if let Some(WordPart::Text(ref mut existing_text)) = parts.last_mut() {
existing_text.push_str(&text);
} else {
parts.push(WordPart::Text(text));
}
}
Rule::UNQUOTED_ESCAPE_CHAR => {
Expand Down
8 changes: 1 addition & 7 deletions crates/deno_task_shell/src/shell/commands/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ fn parse_args(args: Vec<String>) -> Result<String> {
for arg in args {
match arg {
ArgKind::Arg(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());
}
paths.push(arg);
}
_ => arg.bail_unsupported()?,
}
Expand Down

0 comments on commit 9570448

Please sign in to comment.