Skip to content

Commit 8a96750

Browse files
style: Propagate color_enabled flag through clap_help styling helpers
clap_help_with_color_policy accepted a color_enabled parameter but did not actually use it. The helper functions (heading, command_name, placeholder) called supports_color() directly, ignoring the passed flag and making tests non-deterministic. Co-authored-by: SCE <sce@crocoder.dev>
1 parent f4d214b commit 8a96750

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

cli/src/services/style.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,22 @@ where
4848

4949
#[must_use]
5050
pub fn heading(text: &str) -> String {
51-
style_if_enabled(text, |s| s.cyan().bold().to_string())
51+
heading_with_color_policy(text, supports_color())
52+
}
53+
54+
#[must_use]
55+
fn heading_with_color_policy(text: &str, color_enabled: bool) -> String {
56+
style_if(text, color_enabled, |s| s.cyan().bold().to_string())
5257
}
5358

5459
#[must_use]
5560
pub fn command_name(text: &str) -> String {
56-
style_if_enabled(text, |s| s.green().to_string())
61+
command_name_with_color_policy(text, supports_color())
62+
}
63+
64+
#[must_use]
65+
fn command_name_with_color_policy(text: &str, color_enabled: bool) -> String {
66+
style_if(text, color_enabled, |s| s.green().to_string())
5767
}
5868

5969
#[must_use]
@@ -75,7 +85,12 @@ pub fn example_command(text: &str) -> String {
7585
#[must_use]
7686
#[allow(dead_code)]
7787
pub fn placeholder(text: &str) -> String {
78-
style_if_enabled(text, |s| s.italic().dimmed().to_string())
88+
placeholder_with_color_policy(text, supports_color())
89+
}
90+
91+
#[must_use]
92+
fn placeholder_with_color_policy(text: &str, color_enabled: bool) -> String {
93+
style_if(text, color_enabled, |s| s.italic().dimmed().to_string())
7994
}
8095

8196
#[must_use]
@@ -145,7 +160,7 @@ pub(crate) fn clap_help_with_color_policy(text: &str, color_enabled: bool) -> St
145160
let trailing_newline = text.ends_with('\n');
146161
let rendered = text
147162
.lines()
148-
.map(style_clap_help_line)
163+
.map(|line| style_clap_help_line(line, color_enabled))
149164
.collect::<Vec<_>>()
150165
.join("\n");
151166

@@ -156,34 +171,34 @@ pub(crate) fn clap_help_with_color_policy(text: &str, color_enabled: bool) -> St
156171
}
157172
}
158173

159-
fn style_clap_help_line(line: &str) -> String {
174+
fn style_clap_help_line(line: &str, color_enabled: bool) -> String {
160175
if line.is_empty() {
161176
return String::new();
162177
}
163178

164179
if let Some(remainder) = line.strip_prefix("Usage: ") {
165180
return format!(
166181
"{} {}",
167-
heading("Usage:"),
168-
style_clap_usage_segment(remainder)
182+
heading_with_color_policy("Usage:", color_enabled),
183+
style_clap_usage_segment(remainder, color_enabled)
169184
);
170185
}
171186

172187
if !line.starts_with(' ') && line.ends_with(':') {
173-
return heading(line);
188+
return heading_with_color_policy(line, color_enabled);
174189
}
175190

176191
if let Some((indent, token, remainder)) = split_help_table_row(line) {
177192
let styled_token = if token.starts_with('-') {
178-
style_help_placeholders(token)
193+
style_help_placeholders(token, color_enabled)
179194
} else {
180-
command_name(token)
195+
command_name_with_color_policy(token, color_enabled)
181196
};
182197

183198
return format!("{indent}{styled_token}{remainder}");
184199
}
185200

186-
style_help_placeholders(line)
201+
style_help_placeholders(line, color_enabled)
187202
}
188203

189204
fn split_help_table_row(line: &str) -> Option<(&str, &str, &str)> {
@@ -200,23 +215,23 @@ fn split_help_table_row(line: &str) -> Option<(&str, &str, &str)> {
200215
Some((&line[..indent_len], token, remainder))
201216
}
202217

203-
fn style_clap_usage_segment(segment: &str) -> String {
218+
fn style_clap_usage_segment(segment: &str, color_enabled: bool) -> String {
204219
segment
205220
.split(' ')
206221
.map(|part| {
207222
if part.is_empty() {
208223
String::new()
209224
} else if is_help_placeholder(part) {
210-
style_help_placeholders(part)
225+
style_help_placeholders(part, color_enabled)
211226
} else {
212-
command_name(part)
227+
command_name_with_color_policy(part, color_enabled)
213228
}
214229
})
215230
.collect::<Vec<_>>()
216231
.join(" ")
217232
}
218233

219-
fn style_help_placeholders(text: &str) -> String {
234+
fn style_help_placeholders(text: &str, color_enabled: bool) -> String {
220235
let mut styled = String::new();
221236
let mut chars = text.chars().peekable();
222237

@@ -235,7 +250,7 @@ fn style_help_placeholders(text: &str) -> String {
235250
}
236251

237252
if closed && is_help_placeholder(&token) {
238-
styled.push_str(&placeholder(&token));
253+
styled.push_str(&placeholder_with_color_policy(&token, color_enabled));
239254
} else {
240255
styled.push_str(&token);
241256
}

0 commit comments

Comments
 (0)