Skip to content

Commit

Permalink
let force the colors of regexp
Browse files Browse the repository at this point in the history
Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Jun 28, 2023
1 parent bafc32e commit 4bd9e5f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,24 @@ fn regexp_colorize(regexps: &[String]) -> HashMap<String, Color> {
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
}
Expand Down

0 comments on commit 4bd9e5f

Please sign in to comment.