create Palette trait (#1813)

* create Pallet trait

* correct spelling to palette

* move palette to it's own module
This commit is contained in:
Kurtis 2020-05-17 10:48:57 -07:00 committed by GitHub
parent e728cecb4d
commit d4dd8284a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 43 deletions

View File

@ -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));
}

View File

@ -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;

View File

@ -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<FlatShape>) {
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<P: Palette>(&mut self, shape: &Spanned<FlatShape>, 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<Style>) {
for pos in styled_span.span.start()..styled_span.span.end() {
self.styles[pos] = styled_span.item;
}
}

View File

@ -0,0 +1,63 @@
use ansi_term::{Color, Style};
use nu_protocol::hir::FlatShape;
use nu_source::{Span, Spanned};
pub trait Palette {
fn styles_for_shape(&self, shape: &Spanned<FlatShape>) -> Vec<Spanned<Style>>;
}
pub struct DefaultPalette {}
impl Palette for DefaultPalette {
fn styles_for_shape(&self, shape: &Spanned<FlatShape>) -> Vec<Spanned<Style>> {
match &shape.item {
FlatShape::OpenDelimiter(_) => single_style_span(Color::White.normal(), shape.span),
FlatShape::CloseDelimiter(_) => single_style_span(Color::White.normal(), shape.span),
FlatShape::ItVariable | FlatShape::Keyword => {
single_style_span(Color::Purple.bold(), shape.span)
}
FlatShape::Variable | FlatShape::Identifier => {
single_style_span(Color::Purple.normal(), shape.span)
}
FlatShape::Type => single_style_span(Color::Blue.bold(), shape.span),
FlatShape::Operator => single_style_span(Color::Yellow.normal(), shape.span),
FlatShape::DotDot => single_style_span(Color::Yellow.bold(), shape.span),
FlatShape::Dot => single_style_span(Style::new().fg(Color::White), shape.span),
FlatShape::InternalCommand => single_style_span(Color::Cyan.bold(), shape.span),
FlatShape::ExternalCommand => single_style_span(Color::Cyan.normal(), shape.span),
FlatShape::ExternalWord => single_style_span(Color::Green.bold(), shape.span),
FlatShape::BareMember => single_style_span(Color::Yellow.bold(), shape.span),
FlatShape::StringMember => single_style_span(Color::Yellow.bold(), shape.span),
FlatShape::String => single_style_span(Color::Green.normal(), shape.span),
FlatShape::Path => single_style_span(Color::Cyan.normal(), shape.span),
FlatShape::GlobPattern => single_style_span(Color::Cyan.bold(), shape.span),
FlatShape::Word => single_style_span(Color::Green.normal(), shape.span),
FlatShape::Pipe => single_style_span(Color::Purple.bold(), shape.span),
FlatShape::Flag => single_style_span(Color::Blue.bold(), shape.span),
FlatShape::ShorthandFlag => single_style_span(Color::Blue.bold(), shape.span),
FlatShape::Int => single_style_span(Color::Purple.bold(), shape.span),
FlatShape::Decimal => single_style_span(Color::Purple.bold(), shape.span),
FlatShape::Whitespace | FlatShape::Separator => {
single_style_span(Color::White.normal(), shape.span)
}
FlatShape::Comment => single_style_span(Color::Green.bold(), shape.span),
FlatShape::Garbage => {
single_style_span(Style::new().fg(Color::White).on(Color::Red), shape.span)
}
FlatShape::Size { number, unit } => vec![
Spanned::<Style> {
span: *number,
item: Color::Purple.bold(),
},
Spanned::<Style> {
span: *unit,
item: Color::Cyan.bold(),
},
],
}
}
}
fn single_style_span(style: Style, span: Span) -> Vec<Spanned<Style>> {
vec![Spanned::<Style> { span, item: style }]
}