2019-05-18 04:30:57 +02:00
|
|
|
use crate::shell::completer::NuCompleter;
|
2019-05-16 23:43:36 +02:00
|
|
|
|
2019-05-26 08:54:41 +02:00
|
|
|
use crate::prelude::*;
|
2019-05-18 04:30:57 +02:00
|
|
|
use rustyline::completion::{self, Completer, FilenameCompleter};
|
2019-05-16 23:43:36 +02:00
|
|
|
use rustyline::error::ReadlineError;
|
|
|
|
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
|
|
|
|
use rustyline::hint::{Hinter, HistoryHinter};
|
|
|
|
use std::borrow::Cow::{self, Owned};
|
|
|
|
|
|
|
|
crate struct Helper {
|
|
|
|
completer: NuCompleter,
|
|
|
|
highlighter: MatchingBracketHighlighter,
|
|
|
|
hinter: HistoryHinter,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Helper {
|
2019-05-26 08:54:41 +02:00
|
|
|
crate fn new(commands: indexmap::IndexMap<String, Arc<dyn Command>>) -> Helper {
|
2019-05-16 23:43:36 +02:00
|
|
|
Helper {
|
2019-05-18 04:30:57 +02:00
|
|
|
completer: NuCompleter {
|
|
|
|
file_completer: FilenameCompleter::new(),
|
2019-05-26 08:54:41 +02:00
|
|
|
commands,
|
2019-05-18 04:30:57 +02:00
|
|
|
},
|
2019-05-16 23:43:36 +02:00
|
|
|
highlighter: MatchingBracketHighlighter::new(),
|
|
|
|
hinter: HistoryHinter {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Completer for Helper {
|
2019-05-18 04:30:57 +02:00
|
|
|
type Candidate = completion::Pair;
|
2019-05-16 23:43:36 +02:00
|
|
|
|
|
|
|
fn complete(
|
|
|
|
&self,
|
|
|
|
line: &str,
|
|
|
|
pos: usize,
|
|
|
|
ctx: &rustyline::Context<'_>,
|
2019-05-18 04:30:57 +02:00
|
|
|
) -> Result<(usize, Vec<completion::Pair>), ReadlineError> {
|
2019-05-16 23:43:36 +02:00
|
|
|
self.completer.complete(line, pos, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hinter for Helper {
|
|
|
|
fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> {
|
|
|
|
self.hinter.hint(line, pos, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Highlighter for Helper {
|
|
|
|
fn highlight_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> {
|
|
|
|
Owned("\x1b[32m".to_owned() + &prompt[0..prompt.len() - 2] + "\x1b[m> ")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
|
|
|
|
Owned("\x1b[1m".to_owned() + hint + "\x1b[m")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
|
|
|
|
self.highlighter.highlight(line, pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_char(&self, line: &str, pos: usize) -> bool {
|
|
|
|
self.highlighter.highlight_char(line, pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl rustyline::Helper for Helper {}
|