1use crate::verdict::Severity;
2use owo_colors::OwoColorize;
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum Stream {
9 Stdout,
10 Stderr,
11}
12
13pub fn use_color_for(stream: Stream) -> bool {
19 if std::env::var_os("NO_COLOR").is_some() {
20 return false;
21 }
22 match stream {
23 Stream::Stderr => is_terminal::is_terminal(std::io::stderr()),
24 Stream::Stdout => is_terminal::is_terminal(std::io::stdout()),
25 }
26}
27
28pub fn severity_label(severity: &Severity, stream: Stream) -> String {
32 let label = format!("[{}]", severity);
33 if !use_color_for(stream) {
34 return label;
35 }
36 match severity {
37 Severity::Critical => label.bright_red().to_string(),
38 Severity::High => label.red().to_string(),
39 Severity::Medium => label.yellow().to_string(),
40 Severity::Low => label.cyan().to_string(),
41 Severity::Info => label.dimmed().to_string(),
42 }
43}
44
45pub fn bold(text: &str, stream: Stream) -> String {
47 if use_color_for(stream) {
48 text.bold().to_string()
49 } else {
50 text.to_string()
51 }
52}
53
54pub fn bold_red(text: &str, stream: Stream) -> String {
56 if use_color_for(stream) {
57 text.bold().red().to_string()
58 } else {
59 text.to_string()
60 }
61}
62
63pub fn green(text: &str, stream: Stream) -> String {
65 if use_color_for(stream) {
66 text.green().to_string()
67 } else {
68 text.to_string()
69 }
70}
71
72pub fn red(text: &str, stream: Stream) -> String {
74 if use_color_for(stream) {
75 text.red().to_string()
76 } else {
77 text.to_string()
78 }
79}
80
81pub fn yellow(text: &str, stream: Stream) -> String {
83 if use_color_for(stream) {
84 text.yellow().to_string()
85 } else {
86 text.to_string()
87 }
88}
89
90pub fn dim(text: &str, stream: Stream) -> String {
92 if use_color_for(stream) {
93 text.dimmed().to_string()
94 } else {
95 text.to_string()
96 }
97}
98
99pub fn pass_mark(stream: Stream) -> String {
101 if use_color_for(stream) {
102 "\u{2713}".green().to_string()
103 } else {
104 "[ok]".to_string()
105 }
106}
107
108pub fn fail_mark(stream: Stream) -> String {
110 if use_color_for(stream) {
111 "\u{2717}".red().to_string()
112 } else {
113 "[!!]".to_string()
114 }
115}