From 1b50b5b16225797ad42f4b042fcac4b4289c9b32 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 18 May 2019 07:06:01 -0700 Subject: [PATCH] Complete commands a bit better --- src/shell/completer.rs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/shell/completer.rs b/src/shell/completer.rs index f693aa2740..786ed09bbf 100644 --- a/src/shell/completer.rs +++ b/src/shell/completer.rs @@ -15,23 +15,12 @@ impl Completer for NuCompleter { pos: usize, context: &rustyline::Context, ) -> rustyline::Result<(usize, Vec)> { - let mut pairs = vec![ - completion::Pair { - display: "exit".to_string(), - replacement: "exit".to_string(), - }, - completion::Pair { - display: "ls".to_string(), - replacement: "ls".to_string(), - }, - completion::Pair { - display: "ps".to_string(), - replacement: "ps".to_string(), - }, + let commands = [ + "ps", "ls", "cd", "bat", "skip", "take", "select", "reject", "to-array", "where", + "sort-by", ]; let mut completions = self.file_completer.complete(line, pos, context)?.1; - completions.append(&mut pairs); let line_chars: Vec<_> = line.chars().collect(); let mut replace_pos = pos; @@ -42,6 +31,30 @@ impl Completer for NuCompleter { replace_pos -= 1; } + for command in commands.iter() { + let mut pos = replace_pos; + let mut matched = true; + if pos < line_chars.len() { + for chr in command.chars() { + if line_chars[pos] != chr { + matched = false; + break; + } + pos += 1; + if pos == line_chars.len() { + break; + } + } + } + + if matched { + completions.push(completion::Pair { + display: command.to_string(), + replacement: command.to_string(), + }); + } + } + Ok((replace_pos, completions)) }