Skip to content
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
79 changes: 79 additions & 0 deletions crates/cli/src/ui/badges.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticCategory {
Success,
Info,
Warning,
Error,
}

impl DiagnosticCategory {
pub fn icon(&self) -> &'static str {
match self {
DiagnosticCategory::Success => "βœ”",
DiagnosticCategory::Info => "β„Ή",
DiagnosticCategory::Warning => "⚠",
DiagnosticCategory::Error => "βœ–",
}
}

pub fn fallback(&self) -> &'static str {
match self {
DiagnosticCategory::Success => "[SUCCESS]",
DiagnosticCategory::Info => "[INFO]",
DiagnosticCategory::Warning => "[WARN]",
DiagnosticCategory::Error => "[ERROR]",
}
}

pub fn color_code(&self) -> &'static str {
match self {
DiagnosticCategory::Success => "\x1b[32m",
DiagnosticCategory::Info => "\x1b[34m",
DiagnosticCategory::Warning => "\x1b[33m",
DiagnosticCategory::Error => "\x1b[31m",
}
}
}

pub struct DiagnosticBadge {
pub category: DiagnosticCategory,
pub supports_unicode: bool,
}

impl DiagnosticBadge {
pub fn new(category: DiagnosticCategory, supports_unicode: bool) -> Self {
Self {
category,
supports_unicode,
}
}

pub fn format_message(&self, message: &str) -> String {
let badge_text = if self.supports_unicode {
self.category.icon()
} else {
self.category.fallback()
};

format!(
"{}{} \x1b[0m{}",
self.category.color_code(),
badge_text,
message
)
}
}

impl fmt::Display for DiagnosticBadge {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let badge_text = if self.supports_unicode {
self.category.icon()
} else {
self.category.fallback()
};

write!(f, "{}{}\x1b[0m", self.category.color_code(), badge_text)
}
}
Loading