mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
✨ Add ANSI color mode
This commit is contained in:
+54
-8
@@ -1,5 +1,11 @@
|
||||
pub type RecordFormatter = (Record) -> String
|
||||
|
||||
pub(all) enum ColorMode {
|
||||
Never
|
||||
Auto
|
||||
Always
|
||||
}
|
||||
|
||||
pub struct TextFormatter {
|
||||
show_timestamp : Bool
|
||||
show_level : Bool
|
||||
@@ -8,6 +14,7 @@ pub struct TextFormatter {
|
||||
separator : String
|
||||
field_separator : String
|
||||
template : String
|
||||
color_mode : ColorMode
|
||||
}
|
||||
|
||||
pub fn text_formatter(
|
||||
@@ -18,6 +25,7 @@ pub fn text_formatter(
|
||||
separator~ : String = " ",
|
||||
field_separator~ : String = " ",
|
||||
template~ : String = "",
|
||||
color_mode~ : ColorMode = ColorMode::Never,
|
||||
) -> TextFormatter {
|
||||
{
|
||||
show_timestamp,
|
||||
@@ -27,6 +35,44 @@ pub fn text_formatter(
|
||||
separator,
|
||||
field_separator,
|
||||
template,
|
||||
color_mode,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn color_mode_label(mode : ColorMode) -> String {
|
||||
match mode {
|
||||
ColorMode::Never => "never"
|
||||
ColorMode::Auto => "auto"
|
||||
ColorMode::Always => "always"
|
||||
}
|
||||
}
|
||||
|
||||
fn use_ansi_color(mode : ColorMode) -> Bool {
|
||||
match mode {
|
||||
ColorMode::Never => false
|
||||
ColorMode::Always => true
|
||||
ColorMode::Auto => match @env.get_env_var("NO_COLOR") {
|
||||
Some(_) => false
|
||||
None => true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ansi_wrap(text : String, code : String, enabled : Bool) -> String {
|
||||
if !enabled || text == "" {
|
||||
text
|
||||
} else {
|
||||
"\u{001b}[\{code}m\{text}\u{001b}[0m"
|
||||
}
|
||||
}
|
||||
|
||||
fn level_ansi_code(level : Level) -> String {
|
||||
match level {
|
||||
Level::Trace => "90"
|
||||
Level::Debug => "36"
|
||||
Level::Info => "32"
|
||||
Level::Warn => "33"
|
||||
Level::Error => "31;1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +90,7 @@ fn format_fields(fields : Array[Field], separator : String) -> String {
|
||||
|
||||
fn timestamp_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
if formatter.show_timestamp && rec.timestamp_ms != 0UL {
|
||||
rec.timestamp_ms.to_string()
|
||||
ansi_wrap(rec.timestamp_ms.to_string(), "90", use_ansi_color(formatter.color_mode))
|
||||
} else {
|
||||
""
|
||||
}
|
||||
@@ -52,7 +98,7 @@ fn timestamp_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
|
||||
fn level_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
if formatter.show_level {
|
||||
rec.level.label()
|
||||
ansi_wrap(rec.level.label(), level_ansi_code(rec.level), use_ansi_color(formatter.color_mode))
|
||||
} else {
|
||||
""
|
||||
}
|
||||
@@ -60,7 +106,7 @@ fn level_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
|
||||
fn target_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
if formatter.show_target && rec.target != "" {
|
||||
rec.target
|
||||
ansi_wrap(rec.target, "34", use_ansi_color(formatter.color_mode))
|
||||
} else {
|
||||
""
|
||||
}
|
||||
@@ -68,7 +114,7 @@ fn target_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
|
||||
fn fields_text(rec : Record, formatter : TextFormatter) -> String {
|
||||
if formatter.show_fields && rec.fields.length() != 0 {
|
||||
format_fields(rec.fields, formatter.field_separator)
|
||||
ansi_wrap(format_fields(rec.fields, formatter.field_separator), "35", use_ansi_color(formatter.color_mode))
|
||||
} else {
|
||||
""
|
||||
}
|
||||
@@ -92,20 +138,20 @@ pub fn format_text(rec : Record, formatter~ : TextFormatter = text_formatter())
|
||||
}
|
||||
let parts : Array[String] = []
|
||||
if formatter.show_timestamp && rec.timestamp_ms != 0UL {
|
||||
parts.push("[\{rec.timestamp_ms.to_string()}]")
|
||||
parts.push("[\{timestamp_text(rec, formatter)}]")
|
||||
}
|
||||
if formatter.show_level {
|
||||
parts.push("[\{rec.level.label()}]")
|
||||
parts.push("[\{level_text(rec, formatter)}]")
|
||||
}
|
||||
if formatter.show_target && rec.target != "" {
|
||||
parts.push("[\{rec.target}]")
|
||||
parts.push("[\{target_text(rec, formatter)}]")
|
||||
}
|
||||
parts.push(rec.message)
|
||||
let base = parts.join(formatter.separator)
|
||||
if !formatter.show_fields || rec.fields.length() == 0 {
|
||||
base
|
||||
} else {
|
||||
let details = format_fields(rec.fields, formatter.field_separator)
|
||||
let details = fields_text(rec, formatter)
|
||||
"\{base}\{formatter.separator}\{details}"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user