Skip to content

Commit 0b0968a

Browse files
karanabesylvestre
authored andcommitted
Refactor LS_COLORS handling and comments
1 parent 788c0e8 commit 0b0968a

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/uu/ls/src/colors.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const EMPTY_STYLE: &str = "\x1b[m";
2020

2121
enum RawIndicatorStyle {
2222
Empty,
23-
Code(String),
23+
Code(Indicator),
2424
}
2525

2626
/// We need this struct to be able to store the previous style.
@@ -77,8 +77,8 @@ impl<'a> StyleManager<'a> {
7777
// bypasses fallbacks, matching GNU ls behavior.
7878
return self.apply_empty_style(name, wrap);
7979
}
80-
Some(RawIndicatorStyle::Code(raw)) => {
81-
style_code.push_str(&self.build_raw_style_code(&raw));
80+
Some(RawIndicatorStyle::Code(indicator)) => {
81+
self.append_raw_style_code_for_indicator(indicator, &mut style_code);
8282
applied_raw_code = true;
8383
self.current_style = None;
8484
force_suffix_reset = true;
@@ -119,7 +119,25 @@ impl<'a> StyleManager<'a> {
119119
if raw.is_empty() {
120120
Some(RawIndicatorStyle::Empty)
121121
} else {
122-
Some(RawIndicatorStyle::Code(raw.clone()))
122+
Some(RawIndicatorStyle::Code(indicator))
123+
}
124+
}
125+
126+
// Append a raw SGR sequence for a validated LS_COLORS indicator.
127+
fn append_raw_style_code_for_indicator(
128+
&mut self,
129+
indicator: Indicator,
130+
style_code: &mut String,
131+
) {
132+
if !self.indicator_codes.contains_key(&indicator) {
133+
return;
134+
}
135+
style_code.push_str(self.reset(!self.initial_reset_is_done));
136+
style_code.push_str(ANSI_CSI);
137+
if let Some(raw) = self.indicator_codes.get(&indicator) {
138+
debug_assert!(!raw.is_empty());
139+
style_code.push_str(raw);
140+
style_code.push_str(ANSI_SGR_END);
123141
}
124142
}
125143

@@ -521,6 +539,7 @@ pub(crate) fn validate_ls_colors_env() -> Result<(), LsColorsParseError> {
521539
validate_ls_colors(&ls_colors)
522540
}
523541

542+
// GNU-like parser: ensure LS_COLORS has valid labels and well-formed escapes.
524543
fn validate_ls_colors(ls_colors: &str) -> Result<(), LsColorsParseError> {
525544
let bytes = ls_colors.as_bytes();
526545
let mut idx = 0;
@@ -567,6 +586,7 @@ fn validate_ls_colors(ls_colors: &str) -> Result<(), LsColorsParseError> {
567586
Ok(())
568587
}
569588

589+
// Parse a value with GNU-compatible escape sequences, returning the index of the terminator.
570590
fn parse_funky_string(
571591
bytes: &[u8],
572592
mut idx: usize,

0 commit comments

Comments
 (0)