From 4aab872ee1033df31827a35a1af2ee9f1ec2e2e7 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 11 Mar 2023 21:45:34 +0800 Subject: feat: add support for NO_COLOR (#67) --- src/repl/highlighter.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/repl/highlighter.rs (limited to 'src/repl/highlighter.rs') diff --git a/src/repl/highlighter.rs b/src/repl/highlighter.rs new file mode 100644 index 0000000..593c307 --- /dev/null +++ b/src/repl/highlighter.rs @@ -0,0 +1,66 @@ +use crate::config::SharedConfig; + +use nu_ansi_term::{Color, Style}; +use reedline::{Highlighter, StyledText}; + +const MATCH_COLOR: Color = Color::Green; + +pub struct ReplHighlighter { + external_commands: Vec, + config: SharedConfig, +} + +impl ReplHighlighter { + /// Construct the default highlighter with a given set of extern commands/keywords to detect and highlight + pub fn new(config: SharedConfig, external_commands: Vec) -> ReplHighlighter { + Self { + config, + external_commands, + } + } +} + +impl Highlighter for ReplHighlighter { + fn highlight(&self, line: &str, _cursor: usize) -> StyledText { + let mut styled_text = StyledText::new(); + let color = if self.config.read().light_theme { + Color::Black + } else { + Color::White + }; + let match_color = match self.config.read().highlight { + true => MATCH_COLOR, + false => color, + }; + + if self + .external_commands + .clone() + .iter() + .any(|x| line.contains(x)) + { + let matches: Vec<&str> = self + .external_commands + .iter() + .filter(|c| line.contains(*c)) + .map(std::ops::Deref::deref) + .collect(); + let longest_match = matches.iter().fold("".to_string(), |acc, &item| { + if item.len() > acc.len() { + item.to_string() + } else { + acc + } + }); + let buffer_split: Vec<&str> = line.splitn(2, &longest_match).collect(); + + styled_text.push((Style::new().fg(color), buffer_split[0].to_string())); + styled_text.push((Style::new().fg(match_color), longest_match)); + styled_text.push((Style::new().fg(color), buffer_split[1].to_string())); + } else { + styled_text.push((Style::new().fg(color), line.to_string())); + } + + styled_text + } +} -- cgit v1.2.3