Skip to main content

osvm/utils/
color.rs

1//! Color utility functions for consistent CLI output formatting
2//!
3//! This module provides standardized colors and formatting for different types of CLI output
4//! such as success messages, warnings, errors, and information. It centralizes color schemes
5//! to ensure consistent user experience across all commands.
6
7use colored::{Color, Colorize};
8
9/// Format text as a success message (green)
10#[must_use]
11pub fn success(text: &str) -> colored::ColoredString {
12    text.green()
13}
14
15/// Format text as a warning message (yellow)
16#[must_use]
17pub fn warning(text: &str) -> colored::ColoredString {
18    text.yellow()
19}
20
21/// Format text as an error message (red)
22#[must_use]
23pub fn error(text: &str) -> colored::ColoredString {
24    text.red()
25}
26
27/// Format text as a heading (cyan bold)
28#[must_use]
29pub fn heading(text: &str) -> colored::ColoredString {
30    text.cyan().bold()
31}
32
33/// Format text as a subheading (blue bold)
34#[must_use]
35pub fn subheading(text: &str) -> colored::ColoredString {
36    text.blue().bold()
37}
38
39/// Format text as important information (bright white)
40#[must_use]
41pub fn important(text: &str) -> colored::ColoredString {
42    text.bright_white()
43}
44
45/// Format text as secondary information (bright black/gray)
46#[must_use]
47pub fn secondary(text: &str) -> colored::ColoredString {
48    text.bright_black()
49}
50
51/// Format text as a command example (cyan)
52#[must_use]
53pub fn command(text: &str) -> colored::ColoredString {
54    text.cyan()
55}
56
57/// Format text as a URL or path (cyan underlined)
58#[must_use]
59pub fn url(text: &str) -> colored::ColoredString {
60    text.cyan().underline()
61}
62
63/// Format text as a key in key-value pair (yellow)
64#[must_use]
65pub fn key(text: &str) -> colored::ColoredString {
66    text.yellow()
67}
68
69/// Format text as a value in key-value pair (bright white)
70#[must_use]
71pub fn value(text: &str) -> colored::ColoredString {
72    text.bright_white()
73}
74
75/// Format node status with appropriate color
76#[must_use]
77pub fn node_status(status: &str) -> colored::ColoredString {
78    match status.to_lowercase().as_str() {
79        "running" => format!("{} ●", status).green(),
80        "stopped" => format!("{} ○", status).yellow(),
81        "error" => format!("{} ✕", status).red(),
82        _ => format!("{} ?", status).bright_black(),
83    }
84}
85
86/// Format numeric value with appropriate color based on threshold
87#[must_use]
88pub fn numeric_value(
89    value: f64,
90    warning_threshold: f64,
91    error_threshold: f64,
92) -> colored::ColoredString {
93    if value >= error_threshold {
94        format!("{value:.1}").red()
95    } else if value >= warning_threshold {
96        format!("{value:.1}").yellow()
97    } else {
98        format!("{value:.1}").green()
99    }
100}
101
102/// Format table header text (blue bold)
103#[must_use]
104pub fn table_header(text: &str) -> colored::ColoredString {
105    text.blue().bold()
106}
107
108/// Format separator line (bright black)
109#[must_use]
110pub fn separator() -> colored::ColoredString {
111    "---------------------------------------------------------------------------------"
112        .bright_black()
113}
114
115/// Format text with custom color
116#[must_use]
117pub fn custom(text: &str, color: Color) -> colored::ColoredString {
118    text.color(color)
119}
120
121/// Format text in bold
122#[must_use]
123pub fn bold(text: &str) -> colored::ColoredString {
124    text.bold()
125}
126
127/// Format text in italic
128#[must_use]
129pub fn italic(text: &str) -> colored::ColoredString {
130    text.italic()
131}
132
133/// Format text as code/monospace
134#[must_use]
135pub fn code(text: &str) -> colored::ColoredString {
136    text.white().on_bright_black()
137}