use crate::context::Context; use ansi_term::{Color, Style}; use nu_parser::SignatureRegistry; use nu_protocol::hir::FlatShape; use nu_source::{Span, Spanned, Tag, Tagged}; use rustyline::completion::Completer; use rustyline::error::ReadlineError; use rustyline::highlight::Highlighter; use rustyline::hint::Hinter; use std::borrow::Cow::{self, Owned}; pub(crate) struct Helper { context: Context, pub colored_prompt: String, } impl Helper { pub(crate) fn new(context: Context) -> Helper { Helper { context, colored_prompt: String::new(), } } } impl Completer for Helper { type Candidate = rustyline::completion::Pair; fn complete( &self, line: &str, pos: usize, ctx: &rustyline::Context<'_>, ) -> Result<(usize, Vec), ReadlineError> { self.context.shell_manager.complete(line, pos, ctx) } } impl Hinter for Helper { fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option { self.context.shell_manager.hint(line, pos, ctx) } } impl Highlighter for Helper { fn highlight_prompt<'b, 's: 'b, 'p: 'b>( &'s self, prompt: &'p str, default: bool, ) -> Cow<'b, str> { use std::borrow::Cow::Borrowed; if default { Borrowed(&self.colored_prompt) } else { Borrowed(prompt) } } 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> { let lite_pipeline = nu_parser::lite_parse(line, 0); match lite_pipeline { Err(_) => Cow::Borrowed(line), Ok(lp) => { let classified = nu_parser::classify_pipeline(&lp, &self.context.registry().clone_box()); let shapes = nu_parser::shapes(&classified.commands); let mut painter = Painter::new(line); for shape in shapes { painter.paint_shape(&shape); } Cow::Owned(painter.into_string()) } } } fn highlight_char(&self, _line: &str, _pos: usize) -> bool { true } } #[allow(unused)] fn vec_tag(input: Vec>) -> Option { let mut iter = input.iter(); let first = iter.next()?.tag.clone(); let last = iter.last(); Some(match last { None => first, Some(last) => first.until(&last.tag), }) } struct Painter { original: Vec, styles: Vec