Skip to content

Commit c84f7ed

Browse files
committed
ls: adapt to uutils-term-grid 0.4
1 parent d6399f3 commit c84f7ed

File tree

4 files changed

+55
-61
lines changed

4 files changed

+55
-61
lines changed

Cargo.lock

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ feat_os_windows_legacy = [
258258
test = ["uu_test"]
259259

260260
[workspace.dependencies]
261+
ansi-width = "0.1.0"
261262
bigdecimal = "0.4"
262263
binary-heap-plus = "0.5.0"
263264
bstr = "1.9.1"
@@ -321,7 +322,7 @@ selinux = "0.4"
321322
signal-hook = "0.3.17"
322323
smallvec = { version = "1.13", features = ["union"] }
323324
tempfile = "3.10.1"
324-
uutils_term_grid = "0.3"
325+
uutils_term_grid = "0.5"
325326
terminal_size = "0.3.0"
326327
textwrap = { version = "0.16.1", features = ["terminal_size"] }
327328
thiserror = "1.0"

src/uu/ls/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ edition = "2021"
1515
path = "src/ls.rs"
1616

1717
[dependencies]
18+
ansi-width = "0.1.0"
1819
clap = { workspace = true, features = ["env"] }
1920
chrono = { workspace = true }
20-
unicode-width = { workspace = true }
2121
number_prefix = { workspace = true }
2222
uutils_term_grid = { workspace = true }
2323
terminal_size = { workspace = true }

src/uu/ls/src/ls.rs

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use lscolors::{LsColors, Style};
1414

1515
use std::{cell::OnceCell, num::IntErrorKind};
1616
use std::{collections::HashSet, io::IsTerminal};
17+
use ansi_width::ansi_width;
1718

1819
#[cfg(windows)]
1920
use std::os::windows::fs::MetadataExt;
@@ -33,8 +34,7 @@ use std::{
3334
os::unix::fs::{FileTypeExt, MetadataExt},
3435
time::Duration,
3536
};
36-
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
37-
use unicode_width::UnicodeWidthStr;
37+
use term_grid::{Direction, Filling, Grid, GridOptions};
3838
use uucore::error::USimpleError;
3939
use uucore::format::human::{human_readable, SizeFormat};
4040
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
@@ -2527,7 +2527,7 @@ fn display_items(
25272527
names_vec.push(cell);
25282528
}
25292529

2530-
let names = names_vec.into_iter();
2530+
let mut names = names_vec.into_iter();
25312531

25322532
match config.format {
25332533
Format::Columns => {
@@ -2538,20 +2538,19 @@ fn display_items(
25382538
}
25392539
Format::Commas => {
25402540
let mut current_col = 0;
2541-
let mut names = names;
25422541
if let Some(name) = names.next() {
2543-
write!(out, "{}", name.contents)?;
2544-
current_col = name.width as u16 + 2;
2542+
write!(out, "{}", name)?;
2543+
current_col = ansi_width(&name) as u16 + 2;
25452544
}
25462545
for name in names {
2547-
let name_width = name.width as u16;
2546+
let name_width = ansi_width(&name) as u16;
25482547
// If the width is 0 we print one single line
25492548
if config.width != 0 && current_col + name_width + 1 > config.width {
25502549
current_col = name_width + 2;
2551-
write!(out, ",\n{}", name.contents)?;
2550+
write!(out, ",\n{}", name)?;
25522551
} else {
25532552
current_col += name_width + 2;
2554-
write!(out, ", {}", name.contents)?;
2553+
write!(out, ", {}", name)?;
25552554
}
25562555
}
25572556
// Current col is never zero again if names have been printed.
@@ -2562,7 +2561,7 @@ fn display_items(
25622561
}
25632562
_ => {
25642563
for name in names {
2565-
write!(out, "{}{}", name.contents, config.line_ending)?;
2564+
write!(out, "{}{}", name, config.line_ending)?;
25662565
}
25672566
}
25682567
};
@@ -2596,7 +2595,7 @@ fn get_block_size(md: &Metadata, config: &Config) -> u64 {
25962595
}
25972596

25982597
fn display_grid(
2599-
names: impl Iterator<Item = Cell>,
2598+
names: impl Iterator<Item = String>,
26002599
width: u16,
26012600
direction: Direction,
26022601
out: &mut BufWriter<Stdout>,
@@ -2610,38 +2609,36 @@ fn display_grid(
26102609
write!(out, " ")?;
26112610
}
26122611
printed_something = true;
2613-
write!(out, "{}", name.contents)?;
2612+
write!(out, "{name}")?;
26142613
}
26152614
if printed_something {
26162615
writeln!(out)?;
26172616
}
26182617
} else {
2619-
// TODO: To match gnu/tests/ls/stat-dtype.sh
2620-
// we might want to have Filling::Text("\t".to_string());
2621-
let filling = Filling::Spaces(2);
2622-
let mut grid = Grid::new(GridOptions { filling, direction });
2623-
2624-
for name in names {
2625-
let formatted_name = Cell {
2626-
contents: if quoted && !name.contents.starts_with('\'') {
2627-
format!(" {}", name.contents)
2628-
} else {
2629-
name.contents
2630-
},
2631-
width: name.width,
2632-
};
2633-
grid.add(formatted_name);
2634-
}
2635-
2636-
match grid.fit_into_width(width as usize) {
2637-
Some(output) => {
2638-
write!(out, "{output}")?;
2639-
}
2640-
// Width is too small for the grid, so we fit it in one column
2641-
None => {
2642-
write!(out, "{}", grid.fit_into_columns(1))?;
2643-
}
2644-
}
2618+
let names = if quoted {
2619+
names
2620+
.map(|n| {
2621+
if n.starts_with('\'') {
2622+
format!(" {n}")
2623+
} else {
2624+
n
2625+
}
2626+
})
2627+
.collect()
2628+
} else {
2629+
names.collect()
2630+
};
2631+
let grid = Grid::new(
2632+
names,
2633+
GridOptions {
2634+
// TODO: To match gnu/tests/ls/stat-dtype.sh
2635+
// we might want to have Filling::Text("\t".to_string());
2636+
filling: Filling::Spaces(2),
2637+
direction,
2638+
width: width as usize,
2639+
},
2640+
);
2641+
write!(out, "{grid}")?;
26452642
}
26462643
Ok(())
26472644
}
@@ -2786,8 +2783,7 @@ fn display_item_long(
27862783

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

2789-
let item_name =
2790-
display_item_name(item, config, None, String::new(), out, style_manager).contents;
2786+
let item_name = display_item_name(item, config, None, String::new(), out, style_manager);
27912787

27922788
let displayed_item = if quoted && !item_name.starts_with('\'') {
27932789
format!(" {}", item_name)
@@ -2877,7 +2873,7 @@ fn display_item_long(
28772873
}
28782874

28792875
let displayed_item =
2880-
display_item_name(item, config, None, String::new(), out, style_manager).contents;
2876+
display_item_name(item, config, None, String::new(), out, style_manager);
28812877
let date_len = 12;
28822878

28832879
write!(
@@ -3138,14 +3134,10 @@ fn display_item_name(
31383134
more_info: String,
31393135
out: &mut BufWriter<Stdout>,
31403136
style_manager: &mut StyleManager,
3141-
) -> Cell {
3137+
) -> String {
31423138
// This is our return value. We start by `&path.display_name` and modify it along the way.
31433139
let mut name = escape_name(&path.display_name, &config.quoting_style);
31443140

3145-
// We need to keep track of the width ourselves instead of letting term_grid
3146-
// infer it because the color codes mess up term_grid's width calculation.
3147-
let mut width = name.width();
3148-
31493141
if config.hyperlink {
31503142
name = create_hyperlink(&name, path);
31513143
}
@@ -3155,9 +3147,6 @@ fn display_item_name(
31553147
}
31563148

31573149
if config.format != Format::Long && !more_info.is_empty() {
3158-
// increment width here b/c name was given colors and name.width() is now the wrong
3159-
// size for display
3160-
width += more_info.width();
31613150
name = more_info + &name;
31623151
}
31633152

@@ -3185,7 +3174,6 @@ fn display_item_name(
31853174

31863175
if let Some(c) = char_opt {
31873176
name.push(c);
3188-
width += 1;
31893177
}
31903178
}
31913179

@@ -3257,14 +3245,10 @@ fn display_item_name(
32573245
pad_left(&path.security_context, pad_count)
32583246
};
32593247
name = format!("{security_context} {name}");
3260-
width += security_context.len() + 1;
32613248
}
32623249
}
32633250

3264-
Cell {
3265-
contents: name,
3266-
width,
3267-
}
3251+
name
32683252
}
32693253

32703254
fn create_hyperlink(name: &str, path: &PathData) -> String {

0 commit comments

Comments
 (0)