2019-05-18 04:30:57 +02:00
|
|
|
use rustyline::completion::Completer;
|
|
|
|
use rustyline::completion::{self, FilenameCompleter};
|
2019-05-16 23:43:36 +02:00
|
|
|
use rustyline::line_buffer::LineBuffer;
|
|
|
|
|
2019-05-18 04:30:57 +02:00
|
|
|
crate struct NuCompleter {
|
|
|
|
pub file_completer: FilenameCompleter,
|
|
|
|
}
|
2019-05-16 23:43:36 +02:00
|
|
|
|
|
|
|
impl Completer for NuCompleter {
|
2019-05-18 04:30:57 +02:00
|
|
|
type Candidate = completion::Pair;
|
2019-05-16 23:43:36 +02:00
|
|
|
|
|
|
|
fn complete(
|
|
|
|
&self,
|
2019-05-18 04:30:57 +02:00
|
|
|
line: &str,
|
|
|
|
pos: usize,
|
|
|
|
context: &rustyline::Context,
|
|
|
|
) -> rustyline::Result<(usize, Vec<completion::Pair>)> {
|
|
|
|
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 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;
|
|
|
|
while replace_pos > 0 {
|
|
|
|
if line_chars[replace_pos - 1] == ' ' {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
replace_pos -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((replace_pos, completions))
|
2019-05-16 23:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
|
|
|
|
let end = line.pos();
|
|
|
|
line.replace(start..end, elected)
|
|
|
|
}
|
|
|
|
}
|