From 4bd9e5fc523261c5b3dd24bc85e7ef83ff03ea35 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Wed, 28 Jun 2023 16:41:10 +0200 Subject: [PATCH] let force the colors of regexp Signed-off-by: Chmouel Boudjnah --- README.md | 6 ++++++ src/cli.rs | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5464c8..7e226c0 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,12 @@ for input and snazzily print your logs from the stream (one line at a time). followed by a REGEXP and `snazy` will highlight it. You can have many `-r` switches with many regexps, and you get different highlight for each match. +* If you want to have the highlight forced to some colors you can add the color at the begining of the regexp followed by a colon. The colors can be one of `yellow`, `red`, `green`, `blue`, `magenta`, `cyan`, `white`, `black`. For example if you want to highlight ERROR in red and WARNING in yellow you can do: + +```shell +% kubectl log pod|snazy -r red:ERROR -r yellow:WARNING -r green:INFO +``` + * If `snazy` don't recognize the line as json it will symply straight print it. Either way it will still apply regexp highlighting of the `-r` option or do the action commands matching (see below). This let you use it for any logs diff --git a/src/cli.rs b/src/cli.rs index dd7bbae..e68a672 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -131,7 +131,24 @@ fn regexp_colorize(regexps: &[String]) -> HashMap { Color::Blue, ]; for (i, regexp) in regexps.iter().enumerate() { - regexp_colours.insert((*regexp).to_string(), colours[i % colours.len()]); + let mut chosen = colours[i % colours.len()]; + let mut reg = regexp.to_string(); + if let Some(colour) = regexp.split(':').next() { + // match colour in colours + chosen = match colour { + "yellow" => Color::Yellow, + "cyan" => Color::Cyan, + "red" => Color::Red, + "magenta" => Color::Magenta, + "blue" => Color::Blue, + "green" => Color::Green, + "white" => Color::White, + "black" => Color::Black, + _ => Color::Default, + }; + reg = regexp.replace(format!("{colour}:").as_str(), ""); + } + regexp_colours.insert(reg, chosen); } regexp_colours }