nushell/src/shell/helper.rs

63 lines
1.6 KiB
Rust
Raw Normal View History

2019-05-16 23:43:36 +02:00
use crate::shell::completer::{NuCompleter, NuPair};
use rustyline::completion::Completer;
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 {
crate fn new() -> Helper {
Helper {
completer: NuCompleter,
highlighter: MatchingBracketHighlighter::new(),
hinter: HistoryHinter {},
}
}
}
impl Completer for Helper {
type Candidate = NuPair;
fn complete(
&self,
line: &str,
pos: usize,
ctx: &rustyline::Context<'_>,
) -> Result<(usize, Vec<NuPair>), ReadlineError> {
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 {}