Skip to content

Commit c78baaa

Browse files
CopilotKSXGitHub
andcommitted
Address review comments: refactor LsColors, add color test without LS_COLORS
Co-authored-by: KSXGitHub <11488886+KSXGitHub@users.noreply.github.com>
1 parent 1b2eb7f commit c78baaa

2 files changed

Lines changed: 61 additions & 18 deletions

File tree

src/ls_colors.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::visualizer::coloring::Color;
2-
use lscolors::{Indicator, LsColors as LsColorsCrate};
3-
use std::convert::Infallible;
4-
use std::str::FromStr;
2+
use lscolors::{self, Indicator};
53

64
/// ANSI color prefix strings for terminal output, initialized from the `LS_COLORS` environment
75
/// variable.
@@ -16,10 +14,17 @@ pub struct LsColors {
1614
impl LsColors {
1715
/// Initialize by reading the current environment's `LS_COLORS`.
1816
pub fn from_env() -> Self {
19-
Self::from_ls_colors_crate(&LsColorsCrate::from_env().unwrap_or_default())
17+
Self::from_ls_colors_crate(&lscolors::LsColors::from_env().unwrap_or_default())
2018
}
2119

22-
fn from_ls_colors_crate(ls_colors: &LsColorsCrate) -> Self {
20+
/// Parse an `LS_COLORS`-format string into an [`LsColors`].
21+
///
22+
/// Unrecognized or invalid entries are silently ignored.
23+
pub fn from_str(input: &str) -> Self {
24+
Self::from_ls_colors_crate(&lscolors::LsColors::from_string(input))
25+
}
26+
27+
fn from_ls_colors_crate(ls_colors: &lscolors::LsColors) -> Self {
2328
let prefix_for = |indicator: Indicator| {
2429
ls_colors
2530
.style_for_indicator(indicator)
@@ -44,15 +49,3 @@ impl LsColors {
4449
}
4550
}
4651
}
47-
48-
impl FromStr for LsColors {
49-
type Err = Infallible;
50-
/// Parse an `LS_COLORS`-format string into an [`LsColors`].
51-
///
52-
/// This never fails; unrecognized or invalid entries are silently ignored.
53-
fn from_str(input: &str) -> Result<Self, Self::Err> {
54-
Ok(Self::from_ls_colors_crate(&LsColorsCrate::from_string(
55-
input,
56-
)))
57-
}
58-
}

tests/usual_cli.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,56 @@ fn colorful_equals_colorless() {
852852
assert_eq!(colorful_stripped, colorless);
853853
}
854854

855+
#[test]
856+
fn color_always_with_and_without_ls_colors() {
857+
let workspace = SampleWorkspace::default();
858+
859+
let with_ls_colors = Command::new(PDU)
860+
.with_current_dir(&workspace)
861+
.with_arg("--color=always")
862+
.with_arg("--total-width=100")
863+
.with_env("LS_COLORS", LS_COLORS)
864+
.pipe(stdio)
865+
.output()
866+
.expect("spawn command with --color=always and LS_COLORS");
867+
inspect_stderr(&with_ls_colors.stderr);
868+
assert!(
869+
with_ls_colors.status.success(),
870+
"pdu exited with non-zero status"
871+
);
872+
let with_ls_colors_stripped = with_ls_colors
873+
.stdout
874+
.pipe(String::from_utf8)
875+
.expect("UTF-8")
876+
.pipe(strip_ansi_escapes::strip_str)
877+
.trim_end()
878+
.to_string();
879+
880+
let mut without_ls_colors_cmd = Command::new(PDU)
881+
.with_current_dir(&workspace)
882+
.with_arg("--color=always")
883+
.with_arg("--total-width=100");
884+
without_ls_colors_cmd.env_remove("LS_COLORS");
885+
let without_ls_colors = without_ls_colors_cmd
886+
.pipe(stdio)
887+
.output()
888+
.expect("spawn command with --color=always and without LS_COLORS");
889+
inspect_stderr(&without_ls_colors.stderr);
890+
assert!(
891+
without_ls_colors.status.success(),
892+
"pdu exited with non-zero status"
893+
);
894+
let without_ls_colors_stripped = without_ls_colors
895+
.stdout
896+
.pipe(String::from_utf8)
897+
.expect("UTF-8")
898+
.pipe(strip_ansi_escapes::strip_str)
899+
.trim_end()
900+
.to_string();
901+
902+
assert_eq!(with_ls_colors_stripped, without_ls_colors_stripped);
903+
}
904+
855905
#[cfg(unix)]
856906
#[test]
857907
fn color_always() {
@@ -880,7 +930,7 @@ fn color_always() {
880930
data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse());
881931
*data_tree.name_mut() = OsStringDisplay::os_string_from(".");
882932

883-
let ls_colors: LsColors = LS_COLORS.parse().expect("parse LS_COLORS");
933+
let ls_colors = LsColors::from_str(LS_COLORS);
884934
let map = HashMap::from([
885935
(
886936
OsStringDisplay::os_string_from("file-a1.txt"),

0 commit comments

Comments
 (0)