From cfc11b47a575011630c8755e61adec82a5e61bd1 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 26 Apr 2021 14:41:41 +0200 Subject: [PATCH 1/5] ls: fix grid alignment with --color --- src/uu/ls/src/ls.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 44c644849..b5d8162cd 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -1615,6 +1615,10 @@ fn display_file_name(path: &PathData, strip: Option<&Path>, config: &Config) -> } } + // We need to keep track of the width ourselfs instead of letting term_grid + // infer it because the color codes mess up term_grid's width calculation. + let mut width = name.len(); + if let Some(ls_colors) = &config.color { name = color_name(&ls_colors, &path.p_buf, name, path.md()?); } @@ -1643,6 +1647,7 @@ fn display_file_name(path: &PathData, strip: Option<&Path>, config: &Config) -> if let Some(c) = char_opt { name.push(c); + width += 1; } } @@ -1653,7 +1658,10 @@ fn display_file_name(path: &PathData, strip: Option<&Path>, config: &Config) -> } } - Some(name.into()) + Some(Cell { + contents: name, + width, + }) } fn color_name(ls_colors: &LsColors, path: &Path, name: String, md: &Metadata) -> String { From a0eb4a0283046a20acd3b9cde674015f8d573caf Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 26 Apr 2021 14:42:42 +0200 Subject: [PATCH 2/5] ls: add test for color grid alignment --- tests/by-util/test_ls.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 6a1c7764b..1a3bdf78a 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -775,6 +775,18 @@ fn test_ls_color() { .arg("z") .succeeds() .stdout_only(""); + + // The colors must not mess up the grid layout + at.touch("b"); + scene + .ucmd() + .arg("--color") + .arg("-w=15") + .succeeds() + .stdout_only(format!( + "{} test-color\nb {}\n", + a_with_colors, z_with_colors + )); } #[cfg(unix)] @@ -1723,7 +1735,7 @@ fn test_ls_sort_extension() { let expected = vec![ ".", "..", - ".hidden", + ".hidden", "anotherFile", "file1", "file2", @@ -1741,8 +1753,14 @@ fn test_ls_sort_extension() { ]; let result = scene.ucmd().arg("-1aX").run(); - assert_eq!(result.stdout_str().split('\n').collect::>(), expected,); + assert_eq!( + result.stdout_str().split('\n').collect::>(), + expected, + ); let result = scene.ucmd().arg("-1a").arg("--sort=extension").run(); - assert_eq!(result.stdout_str().split('\n').collect::>(), expected,); + assert_eq!( + result.stdout_str().split('\n').collect::>(), + expected, + ); } From 58fd61b3e83b1c7de6f0429893b60e7069002725 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 26 Apr 2021 15:00:39 +0200 Subject: [PATCH 3/5] ls: fix grid alignment for unicode --- src/uu/ls/src/ls.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index b5d8162cd..6b87a21c4 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -40,6 +40,7 @@ use std::{ }; use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; use time::{strftime, Timespec}; +use unicode_width::UnicodeWidthStr; #[cfg(unix)] use uucore::libc::{S_IXGRP, S_IXOTH, S_IXUSR}; @@ -1617,7 +1618,7 @@ fn display_file_name(path: &PathData, strip: Option<&Path>, config: &Config) -> // We need to keep track of the width ourselfs instead of letting term_grid // infer it because the color codes mess up term_grid's width calculation. - let mut width = name.len(); + let mut width = name.width(); if let Some(ls_colors) = &config.color { name = color_name(&ls_colors, &path.p_buf, name, path.md()?); From c69b72c84036bc23013cb51941ce352f2a6c6fd3 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 26 Apr 2021 15:04:55 +0200 Subject: [PATCH 4/5] ls: forgot to commit Cargo.{toml, lock} --- Cargo.lock | 1 + src/uu/ls/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 321f41d89..e35bcc68c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2058,6 +2058,7 @@ dependencies = [ "term_grid", "termsize", "time", + "unicode-width", "uucore", "uucore_procs", ] diff --git a/src/uu/ls/Cargo.toml b/src/uu/ls/Cargo.toml index 3787d3562..d479a57f4 100644 --- a/src/uu/ls/Cargo.toml +++ b/src/uu/ls/Cargo.toml @@ -16,6 +16,7 @@ path = "src/ls.rs" [dependencies] clap = "2.33" +unicode-width = "0.1.8" number_prefix = "0.4" term_grid = "0.1.5" termsize = "0.1.6" From ece5e14b0d62a52607973aaa363e3a024afd8258 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 26 Apr 2021 22:51:02 +0200 Subject: [PATCH 5/5] fix a typo --- src/uu/ls/src/ls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 6b87a21c4..3903baba9 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -1616,7 +1616,7 @@ fn display_file_name(path: &PathData, strip: Option<&Path>, config: &Config) -> } } - // We need to keep track of the width ourselfs instead of letting term_grid + // We need to keep track of the width ourselves instead of letting term_grid // infer it because the color codes mess up term_grid's width calculation. let mut width = name.width();