use crate::config::Level;
const RESET: &str = "\x1b[0m";
const GRAY: &str = "\x1b[90m";
const PURPLE: &str = "\x1b[35m";
const CYAN: &str = "\x1b[36m";
const YELLOW: &str = "\x1b[33m";
const RED: &str = "\x1b[31m";
const GREEN: &str = "\x1b[32m";
const BOLD: &str = "\x1b[1m";
pub(crate) fn colorize(code: &str, text: &str, no_color: bool) -> String {
if no_color {
text.to_string()
} else {
format!("{code}{text}{RESET}")
}
}
pub(crate) fn format_level(level: Level, no_color: bool) -> String {
let badge = level.badge();
if no_color {
return badge.to_string();
}
let color = match level {
Level::Trace => GRAY,
Level::Debug => PURPLE,
Level::Info => CYAN,
Level::Success => GREEN,
Level::Warn => YELLOW,
Level::Error | Level::Fatal => RED,
};
let emphasis = if level == Level::Fatal { BOLD } else { "" };
format!("{color}{emphasis}{badge}{RESET}")
}
pub(crate) fn format_timestamp(ts: &str, no_color: bool) -> String {
colorize(GRAY, ts, no_color)
}
pub(crate) fn format_caller(caller: &str, no_color: bool) -> String {
colorize(GRAY, &format!("({caller})"), no_color)
}
pub(crate) fn format_field(key: &str, value: &str, no_color: bool) -> String {
format!(
"{}{}",
colorize(GRAY, &format!("{key}="), no_color),
colorize(GREEN, value, no_color),
)
}
pub(crate) fn format_error_field(value: &str, no_color: bool) -> String {
format!(
"{}{}",
colorize(GRAY, "error=", no_color),
colorize(RED, value, no_color),
)
}
pub(crate) fn short_caller(file: &str, line: u32) -> String {
let mut segments = file.rsplit('/');
let last = segments.next().unwrap_or(file);
match segments.next() {
Some(parent) => format!("{parent}/{last}:{line}"),
None => format!("{last}:{line}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn badges_without_color() {
assert_eq!(format_level(Level::Info, true), "INF");
assert_eq!(format_level(Level::Warn, true), "WAR");
assert_eq!(format_level(Level::Fatal, true), "FTL");
}
#[test]
fn badges_with_color_wrap_in_ansi() {
let info = format_level(Level::Info, false);
assert!(info.starts_with("\x1b["));
assert!(info.contains("INF"));
assert!(info.ends_with(RESET));
let fatal = format_level(Level::Fatal, false);
assert!(fatal.contains(RED));
assert!(fatal.contains(BOLD));
}
#[test]
fn shortens_caller_paths() {
assert_eq!(short_caller("server/main.rs", 42), "server/main.rs:42");
assert_eq!(
short_caller("/home/user/project/server/main.rs", 42),
"server/main.rs:42"
);
assert_eq!(short_caller("main.rs", 7), "main.rs:7");
}
#[test]
fn fields_render_key_and_value() {
assert_eq!(format_field("port", "8080", true), "port=8080");
assert_eq!(format_error_field("boom", true), "error=boom");
}
}