1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

ls: upgrade uutils-term-grid to 0.5 (#5485)

This commit is contained in:
Terts Diepraam 2024-05-01 10:16:15 +02:00 committed by GitHub
parent d6399f3a81
commit 3346b4147a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 55 additions and 61 deletions

17
Cargo.lock generated
View file

@ -43,6 +43,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "ansi-width"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "219e3ce6f2611d83b51ec2098a12702112c29e57203a6b0a0929b2cddb486608"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "anstream" name = "anstream"
version = "0.5.0" version = "0.5.0"
@ -2727,6 +2736,7 @@ dependencies = [
name = "uu_ls" name = "uu_ls"
version = "0.0.26" version = "0.0.26"
dependencies = [ dependencies = [
"ansi-width",
"chrono", "chrono",
"clap", "clap",
"glob", "glob",
@ -2736,7 +2746,6 @@ dependencies = [
"once_cell", "once_cell",
"selinux", "selinux",
"terminal_size 0.3.0", "terminal_size 0.3.0",
"unicode-width",
"uucore", "uucore",
"uutils_term_grid", "uutils_term_grid",
] ]
@ -3366,11 +3375,11 @@ checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a"
[[package]] [[package]]
name = "uutils_term_grid" name = "uutils_term_grid"
version = "0.3.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b389452a568698688dda38802068378a16c15c4af9b153cdd99b65391292bbc7" checksum = "9f8d288de1b98c546a4d17b857689ec9c84234ee317eaa018804112fb91e607a"
dependencies = [ dependencies = [
"unicode-width", "ansi-width",
] ]
[[package]] [[package]]

View file

@ -258,6 +258,7 @@ feat_os_windows_legacy = [
test = ["uu_test"] test = ["uu_test"]
[workspace.dependencies] [workspace.dependencies]
ansi-width = "0.1.0"
bigdecimal = "0.4" bigdecimal = "0.4"
binary-heap-plus = "0.5.0" binary-heap-plus = "0.5.0"
bstr = "1.9.1" bstr = "1.9.1"
@ -321,7 +322,7 @@ selinux = "0.4"
signal-hook = "0.3.17" signal-hook = "0.3.17"
smallvec = { version = "1.13", features = ["union"] } smallvec = { version = "1.13", features = ["union"] }
tempfile = "3.10.1" tempfile = "3.10.1"
uutils_term_grid = "0.3" uutils_term_grid = "0.5"
terminal_size = "0.3.0" terminal_size = "0.3.0"
textwrap = { version = "0.16.1", features = ["terminal_size"] } textwrap = { version = "0.16.1", features = ["terminal_size"] }
thiserror = "1.0" thiserror = "1.0"

View file

@ -15,9 +15,9 @@ edition = "2021"
path = "src/ls.rs" path = "src/ls.rs"
[dependencies] [dependencies]
ansi-width = { workspace = true }
clap = { workspace = true, features = ["env"] } clap = { workspace = true, features = ["env"] }
chrono = { workspace = true } chrono = { workspace = true }
unicode-width = { workspace = true }
number_prefix = { workspace = true } number_prefix = { workspace = true }
uutils_term_grid = { workspace = true } uutils_term_grid = { workspace = true }
terminal_size = { workspace = true } terminal_size = { workspace = true }

View file

@ -12,6 +12,7 @@ use clap::{
use glob::{MatchOptions, Pattern}; use glob::{MatchOptions, Pattern};
use lscolors::{LsColors, Style}; use lscolors::{LsColors, Style};
use ansi_width::ansi_width;
use std::{cell::OnceCell, num::IntErrorKind}; use std::{cell::OnceCell, num::IntErrorKind};
use std::{collections::HashSet, io::IsTerminal}; use std::{collections::HashSet, io::IsTerminal};
@ -33,8 +34,7 @@ use std::{
os::unix::fs::{FileTypeExt, MetadataExt}, os::unix::fs::{FileTypeExt, MetadataExt},
time::Duration, time::Duration,
}; };
use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; use term_grid::{Direction, Filling, Grid, GridOptions};
use unicode_width::UnicodeWidthStr;
use uucore::error::USimpleError; use uucore::error::USimpleError;
use uucore::format::human::{human_readable, SizeFormat}; use uucore::format::human::{human_readable, SizeFormat};
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))] #[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
@ -2527,7 +2527,7 @@ fn display_items(
names_vec.push(cell); names_vec.push(cell);
} }
let names = names_vec.into_iter(); let mut names = names_vec.into_iter();
match config.format { match config.format {
Format::Columns => { Format::Columns => {
@ -2538,20 +2538,19 @@ fn display_items(
} }
Format::Commas => { Format::Commas => {
let mut current_col = 0; let mut current_col = 0;
let mut names = names;
if let Some(name) = names.next() { if let Some(name) = names.next() {
write!(out, "{}", name.contents)?; write!(out, "{}", name)?;
current_col = name.width as u16 + 2; current_col = ansi_width(&name) as u16 + 2;
} }
for name in names { for name in names {
let name_width = name.width as u16; let name_width = ansi_width(&name) as u16;
// If the width is 0 we print one single line // If the width is 0 we print one single line
if config.width != 0 && current_col + name_width + 1 > config.width { if config.width != 0 && current_col + name_width + 1 > config.width {
current_col = name_width + 2; current_col = name_width + 2;
write!(out, ",\n{}", name.contents)?; write!(out, ",\n{}", name)?;
} else { } else {
current_col += name_width + 2; current_col += name_width + 2;
write!(out, ", {}", name.contents)?; write!(out, ", {}", name)?;
} }
} }
// Current col is never zero again if names have been printed. // Current col is never zero again if names have been printed.
@ -2562,7 +2561,7 @@ fn display_items(
} }
_ => { _ => {
for name in names { for name in names {
write!(out, "{}{}", name.contents, config.line_ending)?; write!(out, "{}{}", name, config.line_ending)?;
} }
} }
}; };
@ -2596,7 +2595,7 @@ fn get_block_size(md: &Metadata, config: &Config) -> u64 {
} }
fn display_grid( fn display_grid(
names: impl Iterator<Item = Cell>, names: impl Iterator<Item = String>,
width: u16, width: u16,
direction: Direction, direction: Direction,
out: &mut BufWriter<Stdout>, out: &mut BufWriter<Stdout>,
@ -2610,38 +2609,36 @@ fn display_grid(
write!(out, " ")?; write!(out, " ")?;
} }
printed_something = true; printed_something = true;
write!(out, "{}", name.contents)?; write!(out, "{name}")?;
} }
if printed_something { if printed_something {
writeln!(out)?; writeln!(out)?;
} }
} else { } else {
// TODO: To match gnu/tests/ls/stat-dtype.sh let names = if quoted {
// we might want to have Filling::Text("\t".to_string()); names
let filling = Filling::Spaces(2); .map(|n| {
let mut grid = Grid::new(GridOptions { filling, direction }); if n.starts_with('\'') {
format!(" {n}")
for name in names { } else {
let formatted_name = Cell { n
contents: if quoted && !name.contents.starts_with('\'') { }
format!(" {}", name.contents) })
} else { .collect()
name.contents } else {
}, names.collect()
width: name.width, };
}; let grid = Grid::new(
grid.add(formatted_name); names,
} GridOptions {
// TODO: To match gnu/tests/ls/stat-dtype.sh
match grid.fit_into_width(width as usize) { // we might want to have Filling::Text("\t".to_string());
Some(output) => { filling: Filling::Spaces(2),
write!(out, "{output}")?; direction,
} width: width as usize,
// Width is too small for the grid, so we fit it in one column },
None => { );
write!(out, "{}", grid.fit_into_columns(1))?; write!(out, "{grid}")?;
}
}
} }
Ok(()) Ok(())
} }
@ -2786,8 +2783,7 @@ fn display_item_long(
write!(output_display, " {} ", display_date(md, config)).unwrap(); write!(output_display, " {} ", display_date(md, config)).unwrap();
let item_name = let item_name = display_item_name(item, config, None, String::new(), out, style_manager);
display_item_name(item, config, None, String::new(), out, style_manager).contents;
let displayed_item = if quoted && !item_name.starts_with('\'') { let displayed_item = if quoted && !item_name.starts_with('\'') {
format!(" {}", item_name) format!(" {}", item_name)
@ -2877,7 +2873,7 @@ fn display_item_long(
} }
let displayed_item = let displayed_item =
display_item_name(item, config, None, String::new(), out, style_manager).contents; display_item_name(item, config, None, String::new(), out, style_manager);
let date_len = 12; let date_len = 12;
write!( write!(
@ -3138,14 +3134,10 @@ fn display_item_name(
more_info: String, more_info: String,
out: &mut BufWriter<Stdout>, out: &mut BufWriter<Stdout>,
style_manager: &mut StyleManager, style_manager: &mut StyleManager,
) -> Cell { ) -> String {
// This is our return value. We start by `&path.display_name` and modify it along the way. // This is our return value. We start by `&path.display_name` and modify it along the way.
let mut name = escape_name(&path.display_name, &config.quoting_style); let mut name = escape_name(&path.display_name, &config.quoting_style);
// 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 config.hyperlink { if config.hyperlink {
name = create_hyperlink(&name, path); name = create_hyperlink(&name, path);
} }
@ -3155,9 +3147,6 @@ fn display_item_name(
} }
if config.format != Format::Long && !more_info.is_empty() { if config.format != Format::Long && !more_info.is_empty() {
// increment width here b/c name was given colors and name.width() is now the wrong
// size for display
width += more_info.width();
name = more_info + &name; name = more_info + &name;
} }
@ -3185,7 +3174,6 @@ fn display_item_name(
if let Some(c) = char_opt { if let Some(c) = char_opt {
name.push(c); name.push(c);
width += 1;
} }
} }
@ -3257,14 +3245,10 @@ fn display_item_name(
pad_left(&path.security_context, pad_count) pad_left(&path.security_context, pad_count)
}; };
name = format!("{security_context} {name}"); name = format!("{security_context} {name}");
width += security_context.len() + 1;
} }
} }
Cell { name
contents: name,
width,
}
} }
fn create_hyperlink(name: &str, path: &PathData) -> String { fn create_hyperlink(name: &str, path: &PathData) -> String {