From d4dd8284a6aa3717f3a1ed98376e0b38d26c944d Mon Sep 17 00:00:00 2001 From: Kurtis Date: Sun, 17 May 2020 10:48:57 -0700 Subject: [PATCH] create Palette trait (#1813) * create Pallet trait * correct spelling to palette * move palette to it's own module --- crates/nu-cli/src/commands/help.rs | 3 +- crates/nu-cli/src/shell.rs | 1 + crates/nu-cli/src/shell/helper.rs | 63 ++++++++++-------------------- crates/nu-cli/src/shell/palette.rs | 63 ++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 43 deletions(-) create mode 100644 crates/nu-cli/src/shell/palette.rs diff --git a/crates/nu-cli/src/commands/help.rs b/crates/nu-cli/src/commands/help.rs index c1b8577945..0abf9ad050 100644 --- a/crates/nu-cli/src/commands/help.rs +++ b/crates/nu-cli/src/commands/help.rs @@ -263,6 +263,7 @@ pub fn get_help(cmd: &dyn WholeStreamCommand, registry: &CommandRegistry) -> Str } } + let palette = crate::shell::palette::DefaultPalette {}; let examples = cmd.examples(); if !examples.is_empty() { long_desc.push_str("\nExamples:"); @@ -272,7 +273,7 @@ pub fn get_help(cmd: &dyn WholeStreamCommand, registry: &CommandRegistry) -> Str long_desc.push_str(" "); long_desc.push_str(example.description); let colored_example = - crate::shell::helper::Painter::paint_string(example.example, registry); + crate::shell::helper::Painter::paint_string(example.example, registry, &palette); long_desc.push_str(&format!("\n > {}\n", colored_example)); } diff --git a/crates/nu-cli/src/shell.rs b/crates/nu-cli/src/shell.rs index bae7a96a6d..cb67948402 100644 --- a/crates/nu-cli/src/shell.rs +++ b/crates/nu-cli/src/shell.rs @@ -4,6 +4,7 @@ pub(crate) mod completer; pub(crate) mod filesystem_shell; pub(crate) mod help_shell; pub(crate) mod helper; +pub(crate) mod palette; pub(crate) mod shell; pub(crate) mod shell_manager; pub(crate) mod value_shell; diff --git a/crates/nu-cli/src/shell/helper.rs b/crates/nu-cli/src/shell/helper.rs index b4d8005e59..c838313724 100644 --- a/crates/nu-cli/src/shell/helper.rs +++ b/crates/nu-cli/src/shell/helper.rs @@ -1,8 +1,9 @@ use crate::context::Context; +use crate::shell::palette::{DefaultPalette, Palette}; use ansi_term::{Color, Style}; use nu_parser::SignatureRegistry; use nu_protocol::hir::FlatShape; -use nu_source::{Span, Spanned, Tag, Tagged}; +use nu_source::{Spanned, Tag, Tagged}; use rustyline::completion::Completer; use rustyline::error::ReadlineError; use rustyline::highlight::Highlighter; @@ -61,7 +62,11 @@ impl Highlighter for Helper { } fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> { - Painter::paint_string(line, &self.context.registry().clone_box()) + Painter::paint_string( + line, + &self.context.registry().clone_box(), + &DefaultPalette {}, + ) } fn highlight_char(&self, _line: &str, _pos: usize) -> bool { @@ -96,7 +101,11 @@ impl Painter { } } - pub fn paint_string<'l>(line: &'l str, registry: &dyn SignatureRegistry) -> Cow<'l, str> { + pub fn paint_string<'l, P: Palette>( + line: &'l str, + registry: &dyn SignatureRegistry, + palette: &P, + ) -> Cow<'l, str> { let lite_block = nu_parser::lite_parse(line, 0); match lite_block { @@ -108,7 +117,7 @@ impl Painter { let mut painter = Painter::new(line); for shape in shapes { - painter.paint_shape(&shape); + painter.paint_shape(&shape, palette); } Cow::Owned(painter.into_string()) @@ -116,46 +125,16 @@ impl Painter { } } - fn paint_shape(&mut self, shape: &Spanned) { - let style = match &shape.item { - FlatShape::OpenDelimiter(_) => Color::White.normal(), - FlatShape::CloseDelimiter(_) => Color::White.normal(), - FlatShape::ItVariable | FlatShape::Keyword => Color::Purple.bold(), - FlatShape::Variable | FlatShape::Identifier => Color::Purple.normal(), - FlatShape::Type => Color::Blue.bold(), - FlatShape::Operator => Color::Yellow.normal(), - FlatShape::DotDot => Color::Yellow.bold(), - FlatShape::Dot => Style::new().fg(Color::White), - FlatShape::InternalCommand => Color::Cyan.bold(), - FlatShape::ExternalCommand => Color::Cyan.normal(), - FlatShape::ExternalWord => Color::Green.bold(), - FlatShape::BareMember => Color::Yellow.bold(), - FlatShape::StringMember => Color::Yellow.bold(), - FlatShape::String => Color::Green.normal(), - FlatShape::Path => Color::Cyan.normal(), - FlatShape::GlobPattern => Color::Cyan.bold(), - FlatShape::Word => Color::Green.normal(), - FlatShape::Pipe => Color::Purple.bold(), - FlatShape::Flag => Color::Blue.bold(), - FlatShape::ShorthandFlag => Color::Blue.bold(), - FlatShape::Int => Color::Purple.bold(), - FlatShape::Decimal => Color::Purple.bold(), - FlatShape::Whitespace | FlatShape::Separator => Color::White.normal(), - FlatShape::Comment => Color::Green.bold(), - FlatShape::Garbage => Style::new().fg(Color::White).on(Color::Red), - FlatShape::Size { number, unit } => { - self.paint(Color::Purple.bold(), number); - self.paint(Color::Cyan.bold(), unit); - return; - } - }; - - self.paint(style, &shape.span); + fn paint_shape(&mut self, shape: &Spanned, palette: &P) { + palette + .styles_for_shape(shape) + .iter() + .for_each(|x| self.paint(x)); } - fn paint(&mut self, style: Style, span: &Span) { - for pos in span.start()..span.end() { - self.styles[pos] = style; + fn paint(&mut self, styled_span: &Spanned