Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ feat_os_windows_legacy = [
test = ["uu_test"]

[workspace.dependencies]
ansi-width = "0.1.0"
bigdecimal = "0.4"
binary-heap-plus = "0.5.0"
bstr = "1.9.1"
Expand Down Expand Up @@ -321,7 +322,7 @@ selinux = "0.4"
signal-hook = "0.3.17"
smallvec = { version = "1.13", features = ["union"] }
tempfile = "3.10.1"
uutils_term_grid = "0.3"
uutils_term_grid = "0.5"
terminal_size = "0.3.0"
textwrap = { version = "0.16.1", features = ["terminal_size"] }
thiserror = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/uu/ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ edition = "2021"
path = "src/ls.rs"

[dependencies]
ansi-width = { workspace = true }
clap = { workspace = true, features = ["env"] }
chrono = { workspace = true }
unicode-width = { workspace = true }
number_prefix = { workspace = true }
uutils_term_grid = { workspace = true }
terminal_size = { workspace = true }
Expand Down
94 changes: 39 additions & 55 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use glob::{MatchOptions, Pattern};
use lscolors::{LsColors, Style};

use ansi_width::ansi_width;
use std::{cell::OnceCell, num::IntErrorKind};
use std::{collections::HashSet, io::IsTerminal};

Expand All @@ -33,8 +34,7 @@
os::unix::fs::{FileTypeExt, MetadataExt},
time::Duration,
};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use unicode_width::UnicodeWidthStr;
use term_grid::{Direction, Filling, Grid, GridOptions};
use uucore::error::USimpleError;
use uucore::format::human::{human_readable, SizeFormat};
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
Expand Down Expand Up @@ -2527,7 +2527,7 @@
names_vec.push(cell);
}

let names = names_vec.into_iter();
let mut names = names_vec.into_iter();

match config.format {
Format::Columns => {
Expand All @@ -2538,20 +2538,19 @@
}
Format::Commas => {
let mut current_col = 0;
let mut names = names;
if let Some(name) = names.next() {
write!(out, "{}", name.contents)?;
current_col = name.width as u16 + 2;
write!(out, "{}", name)?;
current_col = ansi_width(&name) as u16 + 2;
}
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 config.width != 0 && current_col + name_width + 1 > config.width {
current_col = name_width + 2;
write!(out, ",\n{}", name.contents)?;
write!(out, ",\n{}", name)?;
} else {
current_col += name_width + 2;
write!(out, ", {}", name.contents)?;
write!(out, ", {}", name)?;
}
}
// Current col is never zero again if names have been printed.
Expand All @@ -2562,7 +2561,7 @@
}
_ => {
for name in names {
write!(out, "{}{}", name.contents, config.line_ending)?;
write!(out, "{}{}", name, config.line_ending)?;
}
}
};
Expand Down Expand Up @@ -2596,7 +2595,7 @@
}

fn display_grid(
names: impl Iterator<Item = Cell>,
names: impl Iterator<Item = String>,
width: u16,
direction: Direction,
out: &mut BufWriter<Stdout>,
Expand All @@ -2610,38 +2609,36 @@
write!(out, " ")?;
}
printed_something = true;
write!(out, "{}", name.contents)?;
write!(out, "{name}")?;
}
if printed_something {
writeln!(out)?;
}
} else {
// TODO: To match gnu/tests/ls/stat-dtype.sh
// we might want to have Filling::Text("\t".to_string());
let filling = Filling::Spaces(2);
let mut grid = Grid::new(GridOptions { filling, direction });

for name in names {
let formatted_name = Cell {
contents: if quoted && !name.contents.starts_with('\'') {
format!(" {}", name.contents)
} else {
name.contents
},
width: name.width,
};
grid.add(formatted_name);
}

match grid.fit_into_width(width as usize) {
Some(output) => {
write!(out, "{output}")?;
}
// Width is too small for the grid, so we fit it in one column
None => {
write!(out, "{}", grid.fit_into_columns(1))?;
}
}
let names = if quoted {
names
.map(|n| {

Check warning on line 2620 in src/uu/ls/src/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/ls/src/ls.rs#L2619-L2620

Added lines #L2619 - L2620 were not covered by tests
if n.starts_with('\'') {
format!(" {n}")

Check warning on line 2622 in src/uu/ls/src/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/ls/src/ls.rs#L2622

Added line #L2622 was not covered by tests
} else {
n

Check warning on line 2624 in src/uu/ls/src/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/ls/src/ls.rs#L2624

Added line #L2624 was not covered by tests
}
})
.collect()
} else {
names.collect()
};
let grid = Grid::new(
names,
GridOptions {
// TODO: To match gnu/tests/ls/stat-dtype.sh
// we might want to have Filling::Text("\t".to_string());
filling: Filling::Spaces(2),
direction,
width: width as usize,
},
);
write!(out, "{grid}")?;
}
Ok(())
}
Expand Down Expand Up @@ -2786,8 +2783,7 @@

write!(output_display, " {} ", display_date(md, config)).unwrap();

let item_name =
display_item_name(item, config, None, String::new(), out, style_manager).contents;
let item_name = display_item_name(item, config, None, String::new(), out, style_manager);

let displayed_item = if quoted && !item_name.starts_with('\'') {
format!(" {}", item_name)
Expand Down Expand Up @@ -2877,7 +2873,7 @@
}

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;

write!(
Expand Down Expand Up @@ -3138,14 +3134,10 @@
more_info: String,
out: &mut BufWriter<Stdout>,
style_manager: &mut StyleManager,
) -> Cell {
) -> String {
// 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);

// 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 {
name = create_hyperlink(&name, path);
}
Expand All @@ -3155,9 +3147,6 @@
}

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;
}

Expand Down Expand Up @@ -3185,7 +3174,6 @@

if let Some(c) = char_opt {
name.push(c);
width += 1;
}
}

Expand Down Expand Up @@ -3257,14 +3245,10 @@
pad_left(&path.security_context, pad_count)
};
name = format!("{security_context} {name}");
width += security_context.len() + 1;
}
}

Cell {
contents: name,
width,
}
name
}

fn create_hyperlink(name: &str, path: &PathData) -> String {
Expand Down
Loading