Merge pull request #2133 from tertsdiepraam/ls/fix_color_grid_alignment

`ls`: fix grid alignment with `--color`
This commit is contained in:
Sylvestre Ledru 2021-04-26 22:51:21 +02:00 committed by GitHub
commit 7a3b44d972
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 4 deletions

1
Cargo.lock generated
View file

@ -2058,6 +2058,7 @@ dependencies = [
"term_grid",
"termsize",
"time",
"unicode-width",
"uucore",
"uucore_procs",
]

View file

@ -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"

View file

@ -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};
@ -1597,6 +1598,10 @@ fn display_file_name(path: &PathData, config: &Config) -> Option<Cell> {
}
}
// 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();
if let Some(ls_colors) = &config.color {
name = color_name(&ls_colors, &path.p_buf, name, path.md()?);
}
@ -1625,6 +1630,7 @@ fn display_file_name(path: &PathData, config: &Config) -> Option<Cell> {
if let Some(c) = char_opt {
name.push(c);
width += 1;
}
}
@ -1635,7 +1641,10 @@ fn display_file_name(path: &PathData, config: &Config) -> Option<Cell> {
}
}
Some(name.into())
Some(Cell {
contents: name,
width,
})
}
fn color_name(ls_colors: &LsColors, path: &Path, name: String, md: &Metadata) -> String {

View file

@ -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::<Vec<_>>(), expected,);
assert_eq!(
result.stdout_str().split('\n').collect::<Vec<_>>(),
expected,
);
let result = scene.ucmd().arg("-1a").arg("--sort=extension").run();
assert_eq!(result.stdout_str().split('\n').collect::<Vec<_>>(), expected,);
assert_eq!(
result.stdout_str().split('\n').collect::<Vec<_>>(),
expected,
);
}