Merge pull request #8 from jonathandturner/path_completion

Add path completion to existing completions
This commit is contained in:
Jonathan Turner 2019-05-17 20:14:35 -07:00 committed by GitHub
commit ef8a935185
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 33 deletions

View file

@ -1,6 +1,7 @@
use crate::errors::ShellError;
use crate::prelude::*;
use derive_new::new;
use std::env;
#[derive(new)]
pub struct Cd;
@ -17,6 +18,7 @@ impl crate::Command for Cd {
let mut stream = VecDeque::new();
let path = dunce::canonicalize(cwd.join(&target).as_path())?;
let _ = env::set_current_dir(&path);
stream.push_back(ReturnValue::change_cwd(path));
Ok(stream)
}

View file

@ -15,7 +15,7 @@ impl completion::Completer for Completer {
_pos: usize,
_ctx: &Context<'_>,
) -> rustyline::Result<(usize, Vec<completion::Pair>)> {
let pairs = self
let pairs: Vec<completion::Pair> = self
.commands
.keys()
.map(|k| completion::Pair {

View file

@ -1,26 +1,48 @@
use rustyline::completion::{Candidate, Completer};
use rustyline::completion::Completer;
use rustyline::completion::{self, FilenameCompleter};
use rustyline::line_buffer::LineBuffer;
#[derive(Debug)]
crate struct NuCompleter;
crate struct NuCompleter {
pub file_completer: FilenameCompleter,
}
impl Completer for NuCompleter {
type Candidate = NuPair;
type Candidate = completion::Pair;
fn complete(
&self,
_line: &str,
_pos: usize,
_context: &rustyline::Context,
) -> rustyline::Result<(usize, Vec<NuPair>)> {
Ok((
0,
vec![
NuPair("exit", "exit"),
NuPair("ls", "ls"),
NuPair("ps", "ps"),
],
))
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))
}
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
@ -28,14 +50,3 @@ impl Completer for NuCompleter {
line.replace(start..end, elected)
}
}
crate struct NuPair(&'static str, &'static str);
impl Candidate for NuPair {
fn display(&self) -> &str {
self.0
}
fn replacement(&self) -> &str {
self.1
}
}

View file

@ -1,6 +1,6 @@
use crate::shell::completer::{NuCompleter, NuPair};
use crate::shell::completer::NuCompleter;
use rustyline::completion::Completer;
use rustyline::completion::{self, Completer, FilenameCompleter};
use rustyline::error::ReadlineError;
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
use rustyline::hint::{Hinter, HistoryHinter};
@ -15,7 +15,9 @@ crate struct Helper {
impl Helper {
crate fn new() -> Helper {
Helper {
completer: NuCompleter,
completer: NuCompleter {
file_completer: FilenameCompleter::new(),
},
highlighter: MatchingBracketHighlighter::new(),
hinter: HistoryHinter {},
}
@ -23,14 +25,14 @@ impl Helper {
}
impl Completer for Helper {
type Candidate = NuPair;
type Candidate = completion::Pair;
fn complete(
&self,
line: &str,
pos: usize,
ctx: &rustyline::Context<'_>,
) -> Result<(usize, Vec<NuPair>), ReadlineError> {
) -> Result<(usize, Vec<completion::Pair>), ReadlineError> {
self.completer.complete(line, pos, ctx)
}
}