Skip to content

Commit

Permalink
Make fi=0 disable fallback to no
Browse files Browse the repository at this point in the history
Supersedes #84.
  • Loading branch information
tavianator committed Jun 21, 2024
1 parent 014aa76 commit acc0dc6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ const LS_COLORS_DEFAULT: &str = "rs=0:lc=\x1b[:rc=m:cl=\x1b[K:ex=01;32:sg=30;43:
pub struct LsColors {
indicator_mapping: HashMap<Indicator, Style>,

/// Whether Indicator::RegularFile falls back to Indicator::Normal
/// (see <https://github.com/sharkdp/lscolors/issues/48#issuecomment-1582830387>)
file_normal_fallback: bool,

// Note: you might expect to see a `HashMap` for `suffix_mapping` as well, but we need to
// preserve the exact order of the mapping in order to be consistent with `ls`.
suffix_mapping: Vec<(FileNameSuffix, Option<Style>)>,
Expand All @@ -245,6 +249,7 @@ impl LsColors {
pub fn empty() -> Self {
LsColors {
indicator_mapping: HashMap::new(),
file_normal_fallback: true,
suffix_mapping: vec![],
}
}
Expand Down Expand Up @@ -280,6 +285,9 @@ impl LsColors {
self.indicator_mapping.insert(indicator, style);
} else {
self.indicator_mapping.remove(&indicator);
if indicator == Indicator::RegularFile {
self.file_normal_fallback = false;
}
}
}
}
Expand Down Expand Up @@ -403,7 +411,9 @@ impl LsColors {
// Note: using '.to_str()' here means that filename
// matching will not work with invalid-UTF-8 paths.
let filename = file.file_name();
return self.style_for_str(filename.to_str()?);
if let Some(style) = self.style_for_str(filename.to_str()?) {
return Some(style);
}
}

self.style_for_indicator(indicator)
Expand Down Expand Up @@ -509,7 +519,13 @@ impl LsColors {
_ => indicator,
})
})
.or_else(|| self.indicator_mapping.get(&Indicator::Normal))
.or_else(|| {
if indicator == Indicator::RegularFile && !self.file_normal_fallback {
None
} else {
self.indicator_mapping.get(&Indicator::Normal)
}
})
}
}

Expand Down Expand Up @@ -812,4 +828,18 @@ mod tests {
let style = lscolors.style_for_path(&tmp_file);
assert_eq!(None, style);
}

#[test]
fn file_normal_fallback() {
let tmp_dir = temp_dir();
let tmp_file_path = create_file(tmp_dir.path().join("test-file"));

let lscolors = LsColors::from_string("no=01;31");
let style = lscolors.style_for_path(&tmp_file_path).unwrap();
assert_eq!(Some(Color::Red), style.foreground);

let lscolors = LsColors::from_string("no=01;31:fi=0");
let style = lscolors.style_for_path(&tmp_file_path);
assert_eq!(None, style);
}
}

0 comments on commit acc0dc6

Please sign in to comment.