Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign a different colour to each instance deterministically #4

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Assign a different colour to each instance deterministically
  • Loading branch information
thingsinjars committed Jan 14, 2025
commit 18115e0d14ab4174f818e55545e20f27862ddb14
11 changes: 9 additions & 2 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ impl std::fmt::Display for Log<Decorated> {
..
} = self;

// Define an array of acceptable colors
let colors = [color_yellow, color_green, color_blue, color_red, color_cyan];

// Determine the color based on instance_id
let color_index = instance_id.chars().map(|c| c as usize).sum::<usize>() % colors.len();
let selected_color = colors[color_index];

write!(
f,
"[{color_yellow}{instance_id: <19}{color_reset}] {color_green}{time: <30}{color_reset} | {message}"
"[{selected_color}{instance_id: <19}{color_reset}] {color_green}{time: <30}{color_reset} | {message}"
)
}
}
Expand Down Expand Up @@ -125,7 +132,7 @@ mod tests {
test!(
test_decorated:
Decorated =>
"[{color_yellow}test-instance {color_reset}] {color_green}2023-01-01T12:00:00Z {color_reset} | Test message"
"[{color_green}test-instance {color_reset}] {color_green}2023-01-01T12:00:00Z {color_reset} | Test message"
);

test!(
Expand Down
Loading