Add tilde expansion to shell (#367)

* Add tilde expansion to shell

* Add tilde expansion to auto complete

* Move tilde expansion into split_args

* Add doc
This commit is contained in:
Vincent Ollivier 2022-07-10 22:00:13 +02:00 committed by GitHub
parent 2f25c4a1ef
commit 79682d2302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -167,3 +167,8 @@ by files matching the pattern.
For example `/tmp/*.txt` will match any files with the `txt` extension inside
`/tmp`, and `a?c.txt` will match a file named `abc.txt`.
## Tilde Expansion
The tilde character `~` is a shortcut to `$HOME` so `~/test` will be expanded
to `$HOME/test` by the shell.

View File

@ -56,7 +56,7 @@ fn shell_completer(line: &str) -> Vec<String> {
let args = split_args(line);
let i = args.len() - 1;
if args.len() == 1 && !args[0].starts_with('/') { // Autocomplete command
if args.len() == 1 && !args[0].starts_with('/') && !args[0].starts_with('~') { // Autocomplete command
for cmd in autocomplete_commands() {
if let Some(entry) = cmd.strip_prefix(&args[i]) {
entries.push(entry.into());
@ -187,7 +187,17 @@ pub fn split_args(cmd: &str) -> Vec<String> {
args.push("".to_string());
}
args
args.iter().map(|s| tilde_expansion(&s)).collect()
}
// Replace `~` with the value of `$HOME` when it's at the begining of an arg
fn tilde_expansion(arg: &str) -> String {
if let Some(home) = sys::process::env("HOME") {
if arg == "~" || arg.starts_with("~/") {
return arg.replacen("~", &home, 1);
}
}
arg.to_string()
}
fn cmd_proc(args: &[&str]) -> Result<(), ExitCode> {