lawkit_python/
colors.rs

1use is_terminal::IsTerminal;
2use owo_colors::OwoColorize;
3
4/// 色付けが有効かどうか判定
5pub fn should_use_color(no_color_flag: bool) -> bool {
6    // --no-color フラグが指定されている場合は無色
7    if no_color_flag {
8        return false;
9    }
10
11    // 標準出力がターミナルかチェック (パイプやリダイレクト検出)
12    std::io::stdout().is_terminal()
13}
14
15/// PASSステータスの色付け
16pub fn pass(text: &str, no_color: bool) -> String {
17    if should_use_color(no_color) {
18        format!("{}", text.bright_green().bold())
19    } else {
20        text.to_string()
21    }
22}
23
24/// WARNステータスの色付け
25pub fn warn(text: &str, no_color: bool) -> String {
26    if should_use_color(no_color) {
27        format!("{}", text.bright_yellow().bold())
28    } else {
29        text.to_string()
30    }
31}
32
33/// FAILステータスの色付け
34pub fn fail(text: &str, no_color: bool) -> String {
35    if should_use_color(no_color) {
36        format!("{}", text.bright_red().bold())
37    } else {
38        text.to_string()
39    }
40}
41
42/// CRITICALステータスの色付け
43pub fn critical(text: &str, no_color: bool) -> String {
44    if should_use_color(no_color) {
45        format!("{}", text.bright_magenta().bold())
46    } else {
47        text.to_string()
48    }
49}
50
51/// INFOテキストの色付け
52pub fn info(text: &str, no_color: bool) -> String {
53    if should_use_color(no_color) {
54        format!("{}", text.bright_cyan())
55    } else {
56        text.to_string()
57    }
58}
59
60/// ALERTテキストの色付け
61pub fn alert(text: &str, no_color: bool) -> String {
62    if should_use_color(no_color) {
63        format!("{}", text.bright_yellow())
64    } else {
65        text.to_string()
66    }
67}
68
69/// 統一レベル表記システム
70pub fn level_critical(message: &str, no_color: bool) -> String {
71    format!("[{}] {}", critical("CRITICAL", no_color), message)
72}
73
74pub fn level_high(message: &str, no_color: bool) -> String {
75    format!("[{}] {}", fail("HIGH", no_color), message)
76}
77
78pub fn level_medium(message: &str, no_color: bool) -> String {
79    format!("[{}] {}", warn("MEDIUM", no_color), message)
80}
81
82pub fn level_low(message: &str, no_color: bool) -> String {
83    format!("[{}] {}", pass("LOW", no_color), message)
84}
85
86pub fn level_warning(message: &str, no_color: bool) -> String {
87    format!("[{}] {}", warn("WARNING", no_color), message)
88}
89
90pub fn level_pass(message: &str, no_color: bool) -> String {
91    format!("[{}] {}", pass("PASS", no_color), message)
92}
93
94#[allow(dead_code)]
95pub fn level_conflict(message: &str, no_color: bool) -> String {
96    format!("[{}] {}", fail("CONFLICT", no_color), message)
97}
98
99pub fn level_fail(message: &str, no_color: bool) -> String {
100    format!("[{}] {}", fail("FAIL", no_color), message)
101}
102
103pub fn level_warn(message: &str, no_color: bool) -> String {
104    format!("[{}] {}", warn("WARN", no_color), message)
105}
106
107/// ボールドテキスト  
108#[allow(dead_code)]
109pub fn bold(text: &str) -> String {
110    format!("{}", text.bold())
111}
112
113/// 赤色テキスト
114#[allow(dead_code)]
115pub fn red(text: &str) -> String {
116    format!("{}", text.red())
117}
118
119/// 黄色テキスト
120#[allow(dead_code)]
121pub fn yellow(text: &str) -> String {
122    format!("{}", text.yellow())
123}
124
125/// 緑色テキスト
126#[allow(dead_code)]
127pub fn green(text: &str) -> String {
128    format!("{}", text.green())
129}
130
131/// シアン色テキスト
132#[allow(dead_code)]
133pub fn cyan(text: &str) -> String {
134    format!("{}", text.cyan())
135}