Skip to content

Commit

Permalink
Refactor keyword highlighter and update Cargo.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
bensadeh committed Oct 12, 2024
1 parent 2012532 commit 43417bd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion example-logs/example1
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ docker[654]: Container abcdefgh started successfully
ssh-d[222]: Accepted publickey for root from 192.168.1.20
cron[101]: Running scheduled job ID 23456


# JSON
{"timestamp": "2022-09-22T08:11:36.171800155Z", "message": "Task completed", "status": "success"}
{"timestamp": "2022-09-22T08:11:36.171800155Z", "message": "Connection failed", "status": "error"}
{"timestamp": "2022-09-22T08:11:36.171800155Z", "message": "Invalid input received", "status": "warning"}
{"timestamp": "2022-09-22T08:11:36.171800155Z", "message": "Configuration loaded", "status": "info"}
{"timestamp": "2022-09-22T08:11:36.171800155Z", "message": "Invalid request", "status": "debug"}
64 changes: 64 additions & 0 deletions src/highlighter/builtins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use inlet_manifold::{Color, KeywordConfig, Style};

pub fn get_builtin_keywords(disable_builtin_keywords: bool) -> Vec<KeywordConfig> {
match disable_builtin_keywords {
true => vec![],
false => builtin_keywords(),
}
}

fn builtin_keywords() -> Vec<KeywordConfig> {
let severity_levels = vec![
KeywordConfig {
words: vec!["ERROR".to_string()],
style: Style::new().fg(Color::Red),
},
KeywordConfig {
words: vec!["WARN".to_string(), "WARNING".to_string()],
style: Style::new().fg(Color::Yellow),
},
KeywordConfig {
words: vec!["INFO".to_string()],
style: Style::new().fg(Color::White),
},
KeywordConfig {
words: vec!["SUCCESS".to_string(), "DEBUG".to_string()],
style: Style::new().fg(Color::Green),
},
KeywordConfig {
words: vec!["TRACE".to_string()],
style: Style::new().faint(),
},
];

let rest_keywords = vec![
KeywordConfig {
words: vec!["GET".to_string()],
style: Style::new().fg(Color::Black).on(Color::Green),
},
KeywordConfig {
words: vec!["POST".to_string()],
style: Style::new().fg(Color::Black).on(Color::Yellow),
},
KeywordConfig {
words: vec!["PUT".to_string(), "PATCH".to_string()],
style: Style::new().fg(Color::Black).on(Color::Magenta),
},
KeywordConfig {
words: vec!["DELETE".to_string()],
style: Style::new().fg(Color::Black).on(Color::Red),
},
];

let booleans = [KeywordConfig {
words: vec!["null".to_string(), "true".to_string(), "false".to_string()],
style: Style::new().fg(Color::Red).italic(),
}];

vec![]
.into_iter()
.chain(severity_levels)
.chain(rest_keywords)
.chain(booleans)
.collect()
}
94 changes: 23 additions & 71 deletions src/highlighter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod builtins;
pub mod groups;

use crate::highlighter::builtins::get_builtin_keywords;
use crate::highlighter::groups::HighlighterGroups;
use crate::theme::Theme;
use inlet_manifold::highlighter::HighlightBuilder;
use inlet_manifold::*;

pub fn get_highlighter(
Expand Down Expand Up @@ -53,18 +56,12 @@ pub fn get_highlighter(
builder.with_number_highlighter(theme.numbers);
}

{
let builtin_keywords = get_builtin_keywords(disable_builtin_keywords);

let keywords = vec![]
.into_iter()
.chain(theme.keywords)
.chain(builtin_keywords)
.chain(keyword_configs_from_cli)
.collect();

builder.with_keyword_highlighter(keywords);
}
add_keywords(
theme.keywords,
keyword_configs_from_cli,
disable_builtin_keywords,
&mut builder,
);

if !theme.regexes.is_empty() {
for regex in theme.regexes {
Expand All @@ -79,65 +76,20 @@ pub fn get_highlighter(
builder.build()
}

fn get_builtin_keywords(disable_builtin_keywords: bool) -> Vec<KeywordConfig> {
match disable_builtin_keywords {
true => vec![],
false => builtin_keywords(),
}
}
fn add_keywords(
keywords_from_toml: Vec<KeywordConfig>,
keyword_from_cli: Vec<KeywordConfig>,
disable_builtin_keywords: bool,
builder: &mut HighlightBuilder,
) {
let builtin_keywords = get_builtin_keywords(disable_builtin_keywords);

fn builtin_keywords() -> Vec<KeywordConfig> {
let severity_levels = vec![
KeywordConfig {
words: vec!["ERROR".to_string()],
style: Style::new().fg(Color::Red),
},
KeywordConfig {
words: vec!["WARN".to_string(), "WARNING".to_string()],
style: Style::new().fg(Color::Yellow),
},
KeywordConfig {
words: vec!["INFO".to_string()],
style: Style::new().fg(Color::White),
},
KeywordConfig {
words: vec!["SUCCESS".to_string(), "DEBUG".to_string()],
style: Style::new().fg(Color::Green),
},
KeywordConfig {
words: vec!["TRACE".to_string()],
style: Style::new().faint(),
},
];

let rest_keywords = vec![
KeywordConfig {
words: vec!["GET".to_string()],
style: Style::new().fg(Color::Black).on(Color::Green),
},
KeywordConfig {
words: vec!["POST".to_string()],
style: Style::new().fg(Color::Black).on(Color::Yellow),
},
KeywordConfig {
words: vec!["PUT".to_string(), "PATCH".to_string()],
style: Style::new().fg(Color::Black).on(Color::Magenta),
},
KeywordConfig {
words: vec!["DELETE".to_string()],
style: Style::new().fg(Color::Black).on(Color::Red),
},
];

let booleans = [KeywordConfig {
words: vec!["null".to_string(), "true".to_string(), "false".to_string()],
style: Style::new().fg(Color::Red).italic(),
}];

vec![]
let keywords = vec![]
.into_iter()
.chain(severity_levels)
.chain(rest_keywords)
.chain(booleans)
.collect()
.chain(builtin_keywords)
.chain(keywords_from_toml)
.chain(keyword_from_cli)
.collect();

builder.with_keyword_highlighter(keywords);
}

0 comments on commit 43417bd

Please sign in to comment.