Skip to content

Commit

Permalink
Add tilde expansion to shell (#367)
Browse files Browse the repository at this point in the history
* Add tilde expansion to shell

* Add tilde expansion to auto complete

* Move tilde expansion into split_args

* Add doc
  • Loading branch information
vinc authored Jul 10, 2022
1 parent 2f25c4a commit 79682d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions doc/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
14 changes: 12 additions & 2 deletions src/usr/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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> {
Expand Down

0 comments on commit 79682d2

Please sign in to comment.