1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

ls: use the gnu_legacy feature from lscolors

This commit is contained in:
Sylvestre Ledru 2023-11-30 11:04:14 +01:00
parent 355b4872b1
commit b676216f9b
4 changed files with 60 additions and 37 deletions

View file

@ -290,6 +290,7 @@ itertools = "0.12.0"
libc = "0.2.150" libc = "0.2.150"
lscolors = { version = "0.16.0", default-features = false, features = [ lscolors = { version = "0.16.0", default-features = false, features = [
"nu-ansi-term", "nu-ansi-term",
"gnu_legacy",
] } ] }
memchr = "2" memchr = "2"
memmap2 = "0.9" memmap2 = "0.9"

View file

@ -22,7 +22,7 @@ number_prefix = { workspace = true }
uutils_term_grid = { workspace = true } uutils_term_grid = { workspace = true }
terminal_size = { workspace = true } terminal_size = { workspace = true }
glob = { workspace = true } glob = { workspace = true }
lscolors = { workspace = true } lscolors = { workspace = true, features = ["gnu_legacy"] }
uucore = { workspace = true, features = [ uucore = { workspace = true, features = [
"entries", "entries",
"fs", "fs",

View file

@ -3140,7 +3140,11 @@ fn display_file_name(
fn color_name(name: String, path: &Path, md: Option<&Metadata>, ls_colors: &LsColors) -> String { fn color_name(name: String, path: &Path, md: Option<&Metadata>, ls_colors: &LsColors) -> String {
match ls_colors.style_for_path_with_metadata(path, md) { match ls_colors.style_for_path_with_metadata(path, md) {
Some(style) => { Some(style) => {
return style.to_nu_ansi_term_style().paint(name).to_string(); return style
.to_nu_ansi_term_style()
.reset_before_style()
.paint(name)
.to_string();
} }
None => name, None => name,
} }

View file

@ -2,7 +2,7 @@
// //
// For the full copyright and license information, please view the LICENSE // For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code. // file that was distributed with this source code.
// spell-checker:ignore (words) READMECAREFULLY birthtime doesntexist oneline somebackup lrwx somefile somegroup somehiddenbackup somehiddenfile tabsize aaaaaaaa bbbb cccc dddddddd ncccc neee naaaaa nbcdef nfffff dired subdired tmpfs // spell-checker:ignore (words) READMECAREFULLY birthtime doesntexist oneline somebackup lrwx somefile somegroup somehiddenbackup somehiddenfile tabsize aaaaaaaa bbbb cccc dddddddd ncccc neee naaaaa nbcdef nfffff dired subdired tmpfs mdir1
#[cfg(any(unix, feature = "feat_selinux"))] #[cfg(any(unix, feature = "feat_selinux"))]
use crate::common::util::expected_result; use crate::common::util::expected_result;
@ -864,11 +864,11 @@ fn test_ls_zero() {
.succeeds() .succeeds()
.stdout_only("\"0-test-zero\"\x00\"2-test-zero\"\x00\"3-test-zero\"\x00"); .stdout_only("\"0-test-zero\"\x00\"2-test-zero\"\x00\"3-test-zero\"\x00");
scene let result = scene.ucmd().args(&["--zero", "--color=always"]).succeeds();
.ucmd() assert_eq!(
.args(&["--zero", "--color=always"]) result.stdout_str(),
.succeeds() "\u{1b}[0m\u{1b}[01;34m0-test-zero\x1b[0m\x002-test-zero\x003-test-zero\x00"
.stdout_only("\x1b[1;34m0-test-zero\x1b[0m\x002-test-zero\x003-test-zero\x00"); );
scene scene
.ucmd() .ucmd()
@ -921,12 +921,9 @@ fn test_ls_zero() {
"\"0-test-zero\"\x00\"1\\ntest-zero\"\x00\"2-test-zero\"\x00\"3-test-zero\"\x00", "\"0-test-zero\"\x00\"1\\ntest-zero\"\x00\"2-test-zero\"\x00\"3-test-zero\"\x00",
); );
scene let result = scene.ucmd().args(&["--zero", "--color=always"]).succeeds();
.ucmd() assert_eq!(result.stdout_str(),
.args(&["--zero", "--color=always"]) "\u{1b}[0m\u{1b}[01;34m0-test-zero\x1b[0m\x001\ntest-zero\x002-test-zero\x003-test-zero\x00",
.succeeds()
.stdout_only(
"\x1b[1;34m0-test-zero\x1b[0m\x001\ntest-zero\x002-test-zero\x003-test-zero\x00",
); );
scene scene
@ -1202,12 +1199,21 @@ fn test_ls_long_symlink_color() {
} }
fn capture_colored_string(input: &str) -> (Color, Name) { fn capture_colored_string(input: &str) -> (Color, Name) {
let colored_name = Regex::new(r"\x1b\[([0-9;]+)m(.+)\x1b\[0m").unwrap(); // Input can be:
// \u{1b}[0m\u{1b}[01;36mln-dir3\u{1b}[0m
// \u{1b}[0m\u{1b}[01;34m./dir1/dir2/dir3\u{1b}[0m
// \u{1b}[0m\u{1b}[01;36mln-file-invalid\u{1b}[0m
// \u{1b}[01;36mdir1/invalid-target\u{1b}[0m
let colored_name = Regex::new(r"(?:\x1b\[0m\x1b)?\[([0-9;]+)m(.+)\x1b\[0m").unwrap();
match colored_name.captures(input) { match colored_name.captures(input) {
Some(captures) => ( Some(captures) => {
dbg!(captures.get(1).unwrap().as_str().to_string());
dbg!(captures.get(2).unwrap().as_str().to_string());
return (
captures.get(1).unwrap().as_str().to_string(), captures.get(1).unwrap().as_str().to_string(),
captures.get(2).unwrap().as_str().to_string(), captures.get(2).unwrap().as_str().to_string(),
), );
}
None => (String::new(), input.to_string()), None => (String::new(), input.to_string()),
} }
} }
@ -1977,6 +1983,20 @@ fn test_ls_recursive_1() {
.stdout_is(out); .stdout_is(out);
} }
// Function to convert a string to its ASCII representation
fn to_ascii_representation(input: &str) -> String {
input
.chars()
.map(|c| {
if c.is_ascii_control() || !c.is_ascii() {
format!("\\x{:02x}", c as u32)
} else {
c.to_string()
}
})
.collect::<String>()
}
#[test] #[test]
fn test_ls_color() { fn test_ls_color() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
@ -1995,9 +2015,9 @@ fn test_ls_color() {
at.touch(nested_file); at.touch(nested_file);
at.touch("test-color"); at.touch("test-color");
let a_with_colors = "\x1b[1;34ma\x1b[0m"; let a_with_colors = "\x1b[0m\x1b[01;34ma\x1b[0m";
let z_with_colors = "\x1b[1;34mz\x1b[0m"; let z_with_colors = "\x1b[01;34mz\x1b[0m\n";
let nested_dir_with_colors = "\x1b[1;34mnested_dir\x1b[0m"; // spell-checker:disable-line let nested_dir_with_colors = "\x1b[0m\x1b[01;34mnested_dir\x1b[0m\x0anested_file"; // spell-checker:disable-line
// Color is disabled by default // Color is disabled by default
let result = scene.ucmd().succeeds(); let result = scene.ucmd().succeeds();
@ -2006,12 +2026,9 @@ fn test_ls_color() {
// Color should be enabled // Color should be enabled
for param in ["--color", "--col", "--color=always", "--col=always"] { for param in ["--color", "--col", "--color=always", "--col=always"] {
scene let result = scene.ucmd().arg(param).succeeds();
.ucmd() assert!(result.stdout_str().contains(a_with_colors));
.arg(param) assert!(result.stdout_str().contains(z_with_colors));
.succeeds()
.stdout_contains(a_with_colors)
.stdout_contains(z_with_colors);
} }
// Color should be disabled // Color should be disabled
@ -2020,12 +2037,8 @@ fn test_ls_color() {
assert!(!result.stdout_str().contains(z_with_colors)); assert!(!result.stdout_str().contains(z_with_colors));
// Nested dir should be shown and colored // Nested dir should be shown and colored
scene let result = scene.ucmd().arg("--color").arg("a").succeeds();
.ucmd() assert!(result.stdout_str().contains(nested_dir_with_colors));
.arg("--color")
.arg("a")
.succeeds()
.stdout_contains(nested_dir_with_colors);
// No output // No output
scene scene
@ -2037,13 +2050,18 @@ fn test_ls_color() {
// The colors must not mess up the grid layout // The colors must not mess up the grid layout
at.touch("b"); at.touch("b");
scene let result = scene
.ucmd() .ucmd()
.arg("--color") .arg("--color")
.arg("-w=15") .arg("-w=15")
.arg("-C") .arg("-C")
.succeeds() .succeeds();
.stdout_only(format!("{a_with_colors} test-color\nb {z_with_colors}\n")); let expected = format!("{} test-color\x0ab {}", a_with_colors, z_with_colors);
assert_eq!(
to_ascii_representation(result.stdout_str()),
to_ascii_representation(&expected)
);
assert_eq!(result.stdout_str(), expected);
} }
#[cfg(unix)] #[cfg(unix)]