Skip to content

Commit

Permalink
allow showing emojis instead of the level
Browse files Browse the repository at this point in the history
Signed-off-by: Chmouel Boudjnah <chmouel@chmouel.com>
  • Loading branch information
chmouel committed Apr 27, 2022
1 parent 871d7e2 commit 85fb954
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 11 deletions.
1 change: 1 addition & 0 deletions misc/completions/_snazy
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ _snazy() {
'-V[Print version information]' \
'--version[Print version information]' \
'--kail-no-prefix[Hide container prefix when showing the log with kail]' \
'--level-symbols[Replace log level with pretty symbols]' \
&& ret=0
}

Expand Down
1 change: 1 addition & 0 deletions misc/completions/_snazy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Register-ArgumentCompleter -Native -CommandName 'snazy' -ScriptBlock {
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version information')
[CompletionResult]::new('--kail-no-prefix', 'kail-no-prefix', [CompletionResultType]::ParameterName, 'Hide container prefix when showing the log with kail')
[CompletionResult]::new('--level-symbols', 'level-symbols', [CompletionResultType]::ParameterName, 'Replace log level with pretty symbols')
break
}
})
Expand Down
2 changes: 1 addition & 1 deletion misc/completions/snazy.bash
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _snazy() {

case "${cmd}" in
snazy)
opts="-h -V -r -f -k -c --help --version --regexp --filter-levels --time-format --kail-no-prefix --json-keys --color"
opts="-h -V -r -f -k -c --help --version --regexp --filter-levels --time-format --kail-no-prefix --level-symbols --json-keys --color"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down
1 change: 1 addition & 0 deletions misc/completions/snazy.elv
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set edit:completion:arg-completer[snazy] = {|@words|
cand -V 'Print version information'
cand --version 'Print version information'
cand --kail-no-prefix 'Hide container prefix when showing the log with kail'
cand --level-symbols 'Replace log level with pretty symbols'
}
]
$completions[$command]
Expand Down
1 change: 1 addition & 0 deletions misc/completions/snazy.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ complete -c snazy -s c -l color -d 'When to use colors: never, *auto*, always' -
complete -c snazy -s h -l help -d 'Print help information'
complete -c snazy -s V -l version -d 'Print version information'
complete -c snazy -l kail-no-prefix -d 'Hide container prefix when showing the log with kail'
complete -c snazy -l level-symbols -d 'Replace log level with pretty symbols'
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub fn build_cli() -> Command<'static> {
.long("kail-no-prefix")
.help("Hide container prefix when showing the log with kail"),
)
.arg(
Arg::new("level-symbols")
.long("level-symbols")
.help("Replace log level with pretty symbols")
)
.arg(
Arg::new("json-keys")
.long("json-keys")
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Config {
pub filter_levels: Vec<String>,
pub regexp_colours: HashMap<String, Color>,
pub json_keys: HashMap<String, String>,
pub level_symbols: bool,
}

impl Default for Config {
Expand All @@ -22,6 +23,7 @@ impl Default for Config {
filter_levels: Vec::new(),
regexp_colours: HashMap::new(),
json_keys: HashMap::new(),
level_symbols: bool::default(),
}
}
}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn construct_config(matches: clap::ArgMatches) -> Config {

Config {
kail_no_prefix: matches.is_present("kail-no-prefix"),
level_symbols: matches.is_present("level-symbols"),
filter_levels: matches
.values_of("filter-levels")
.map(|v| v.map(String::from).collect())
Expand All @@ -82,7 +83,7 @@ fn construct_config(matches: clap::ArgMatches) -> Config {
}

fn main() {
let matches = crate::cli::build_cli().get_matches_from(env::args_os());
let matches = cli::build_cli().get_matches_from(env::args_os());
let config = construct_config(matches);
crate::parse::read_from_stdin(Arc::new(config))
parse::read_from_stdin(Arc::new(config))
}
26 changes: 18 additions & 8 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::config::Config;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::io::{self, BufRead};
use std::sync::Arc;
use yansi::{Paint, Style}; // 0.6.5

use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use yansi::{Paint, Style};

use crate::config::Config;

// 0.6.5

#[derive(Serialize, Deserialize, Debug)]
struct Pac {
Expand Down Expand Up @@ -123,7 +127,7 @@ pub fn read_from_stdin(config: Arc<Config>) {
continue;
}

if let Some(msg) = crate::parse::getinfo(parseline, &config) {
if let Some(msg) = getinfo(parseline, &config) {
let unwrapped = serde_json::to_string(&msg).unwrap();
//check if unwrapped is not an empty hashmap
if unwrapped == "{}" {
Expand All @@ -137,7 +141,10 @@ pub fn read_from_stdin(config: Arc<Config>) {
continue;
}

let level = crate::utils::color_by_level(msg.get("level").unwrap());
let mut level = crate::utils::color_by_level(msg.get("level").unwrap());
if config.level_symbols {
level = crate::utils::level_symbols(msg.get("level").unwrap());
}
let mut ts = String::new();
if msg.contains_key("ts") {
ts = Paint::fixed(13, msg.get("ts").unwrap()).to_string();
Expand All @@ -160,7 +167,7 @@ pub fn read_from_stdin(config: Arc<Config>) {
}
}

println!("{} {} {}{}", Paint::wrapping(level), ts, other, themsg);
println!("{} {} {}{}", level, ts, other, themsg);
}
}
}
Expand All @@ -181,6 +188,7 @@ mod tests {
.unwrap();
assert_eq!(msg["msg"], "hello moto");
}

#[test]
fn test_kail_prefix() {
let line = r#"ns/pod[container]: {"severity":"INFO","timestamp":"2022-04-25T14:20:32.505637358Z","logger":"pipelinesascode","caller":"pipelineascode/status.go:59","message":"updated","provider":"github","event":"8b400490-c4a1-11ec-9219-63bc5bbc8228"}"#;
Expand All @@ -195,6 +203,7 @@ mod tests {
assert!(msg["msg"].contains("ns/pod[container]"));
assert!(msg["msg"].contains("updated"));
}

#[test]
fn test_kail_no_prefix() {
let line = r#"ns/pod[container]: {"severity":"INFO","timestamp":"2022-04-25T14:20:32.505637358Z","logger":"pipelinesascode","caller":"pipelineascode/status.go:59","message":" updated","provider":"github","event":"8b400490-c4a1-11ec-9219-63bc5bbc8228"}"#;
Expand All @@ -209,6 +218,7 @@ mod tests {

assert_eq!(msg["msg"], "updated");
}

#[test]
fn test_pac_provider_icon() {
let line = r#"ns/pod[container]: {"severity":"INFO","timestamp":"2022-04-25T14:20:32.505637358Z","logger":"pipelinesascode","caller":"pipelineascode/status.go:59","message":" github","provider":"github","event":"8b400490-c4a1-11ec-9219-63bc5bbc8228"}"#;
Expand Down
12 changes: 12 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ use chrono::NaiveDateTime;
use serde_json::Value;
use yansi::Paint;

/// replace info level DEBUG, WARNING, ERROR, INFO, FATAL by pretty characters
pub fn level_symbols(level: &str) -> String {
match level {
"DEBUG" => "🐛".to_string(),
"WARNING" => "⚠️".to_string(),
"ERROR" => "🚨".to_string(),
"INFO" => "💡".to_string(),
"FATAL" => "💀".to_string(),
_ => "∙".to_string(),
}
}

pub fn color_by_level(level: &str) -> String {
match level {
"DEBUG" => format!("{:<19}", Paint::fixed(14, "DEBUG").to_string()),
Expand Down

0 comments on commit 85fb954

Please sign in to comment.