Add back command completions

This commit is contained in:
Jonathan Turner
2019-08-10 17:02:15 +12:00
parent a38ae910ac
commit 60e7dfcf1b
7 changed files with 131 additions and 34 deletions

View File

@ -1,10 +1,11 @@
use crate::prelude::*;
use derive_new::new;
use rustyline::completion::{Completer, FilenameCompleter};
#[derive(new)]
crate struct NuCompleter {
pub file_completer: FilenameCompleter,
//pub commands: indexmap::IndexMap<String, Arc<dyn Command>>,
pub commands: CommandRegistry,
}
impl NuCompleter {
@ -14,7 +15,7 @@ impl NuCompleter {
pos: usize,
context: &rustyline::Context,
) -> rustyline::Result<(usize, Vec<rustyline::completion::Pair>)> {
//let commands: Vec<String> = self.commands.keys().cloned().collect();
let commands: Vec<String> = self.commands.names();
let mut completions = self.file_completer.complete(line, pos, context)?.1;
@ -45,7 +46,6 @@ impl NuCompleter {
replace_pos -= 1;
}
/*
for command in commands.iter() {
let mut pos = replace_pos;
let mut matched = true;
@ -63,13 +63,12 @@ impl NuCompleter {
}
if matched {
completions.push(CompletionPair {
completions.push(rustyline::completion::Pair {
display: command.clone(),
replacement: command.clone(),
});
}
}
*/
Ok((replace_pos, completions))
}

View File

@ -18,6 +18,7 @@ impl Clone for FilesystemShell {
path: self.path.clone(),
completer: NuCompleter {
file_completer: FilenameCompleter::new(),
commands: self.completer.commands.clone(),
},
hinter: HistoryHinter {},
}
@ -25,23 +26,28 @@ impl Clone for FilesystemShell {
}
impl FilesystemShell {
pub fn basic() -> Result<FilesystemShell, std::io::Error> {
pub fn basic(commands: CommandRegistry) -> Result<FilesystemShell, std::io::Error> {
let path = std::env::current_dir()?;
Ok(FilesystemShell {
path: path.to_string_lossy().to_string(),
completer: NuCompleter {
file_completer: FilenameCompleter::new(),
commands,
},
hinter: HistoryHinter {},
})
}
pub fn with_location(path: String) -> Result<FilesystemShell, std::io::Error> {
pub fn with_location(
path: String,
commands: CommandRegistry,
) -> Result<FilesystemShell, std::io::Error> {
Ok(FilesystemShell {
path,
completer: NuCompleter {
file_completer: FilenameCompleter::new(),
commands,
},
hinter: HistoryHinter {},
})

View File

@ -1,5 +1,6 @@
use crate::commands::command::EvaluatedStaticCommandArgs;
use crate::errors::ShellError;
use crate::prelude::*;
use crate::shell::filesystem_shell::FilesystemShell;
use crate::shell::shell::Shell;
use crate::stream::OutputStream;
@ -12,9 +13,11 @@ pub struct ShellManager {
}
impl ShellManager {
pub fn basic() -> Result<ShellManager, Box<dyn Error>> {
pub fn basic(commands: CommandRegistry) -> Result<ShellManager, Box<dyn Error>> {
Ok(ShellManager {
shells: Arc::new(Mutex::new(vec![Box::new(FilesystemShell::basic()?)])),
shells: Arc::new(Mutex::new(vec![Box::new(FilesystemShell::basic(
commands,
)?)])),
})
}