mirror of https://github.com/vinc/moros.git
Improve shell autocompletion (#448)
* Fix autocompletion of second arg * Fix autocompletion after ^C * Autocomplete dirs on first arg
This commit is contained in:
parent
3348bb7150
commit
d2722f85ee
|
@ -33,10 +33,12 @@ impl Prompt {
|
|||
while let Some(c) = io::stdin().read_char() {
|
||||
match c {
|
||||
console::ETX_KEY => { // End of Text (^C)
|
||||
self.update_completion();
|
||||
println!();
|
||||
return Some(String::new());
|
||||
},
|
||||
console::EOT_KEY => { // End of Transmission (^D)
|
||||
self.update_completion();
|
||||
println!();
|
||||
return None;
|
||||
},
|
||||
|
|
|
@ -53,31 +53,40 @@ fn autocomplete_commands() -> Vec<String> {
|
|||
|
||||
fn shell_completer(line: &str) -> Vec<String> {
|
||||
let mut entries = Vec::new();
|
||||
|
||||
let args = split_args(line);
|
||||
let mut args = split_args(line);
|
||||
if line.ends_with(' ') {
|
||||
args.push(String::new());
|
||||
}
|
||||
let i = args.len() - 1;
|
||||
if args.len() == 1 && !args[0].starts_with('/') && !args[0].starts_with('~') { // Autocomplete command
|
||||
|
||||
// Autocomplete command
|
||||
if args.len() == 1 && !args[i].starts_with('/') && !args[i].starts_with('~') {
|
||||
for cmd in autocomplete_commands() {
|
||||
if let Some(entry) = cmd.strip_prefix(&args[i]) {
|
||||
entries.push(entry.into());
|
||||
}
|
||||
}
|
||||
} else { // Autocomplete path
|
||||
let pathname = fs::realpath(&args[i]);
|
||||
let dirname = fs::dirname(&pathname);
|
||||
let filename = fs::filename(&pathname);
|
||||
let sep = if dirname.ends_with('/') { "" } else { "/" };
|
||||
if let Ok(files) = fs::read_dir(dirname) {
|
||||
for file in files {
|
||||
let name = file.name();
|
||||
if name.starts_with(filename) {
|
||||
let end = if file.is_dir() { "/" } else { "" };
|
||||
let path = format!("{}{}{}{}", dirname, sep, name, end);
|
||||
entries.push(path[pathname.len()..].into());
|
||||
}
|
||||
|
||||
// Autocomplete path
|
||||
let pathname = fs::realpath(&args[i]);
|
||||
let dirname = fs::dirname(&pathname);
|
||||
let filename = fs::filename(&pathname);
|
||||
let sep = if dirname.ends_with('/') { "" } else { "/" };
|
||||
if let Ok(files) = fs::read_dir(dirname) {
|
||||
for file in files {
|
||||
let name = file.name();
|
||||
if name.starts_with(filename) {
|
||||
if args.len() == 1 && !file.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let end = if args.len() != 1 && file.is_dir() { "/" } else { "" };
|
||||
let path = format!("{}{}{}{}", dirname, sep, name, end);
|
||||
entries.push(path[pathname.len()..].into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entries.sort();
|
||||
entries
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue