Skip to content

Commit

Permalink
ls: upgrade uutils-term-grid to 0.5 (#5485)
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam authored May 1, 2024
1 parent d6399f3 commit 3346b41
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 61 deletions.
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 clap::{
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 @@ use std::{
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 @@ fn display_items(
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 @@ fn display_items(
}
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 @@ fn display_items(
}
_ => {
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 get_block_size(md: &Metadata, config: &Config) -> u64 {
}

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 @@ fn display_grid(
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| {
if n.starts_with('\'') {
format!(" {n}")
} else {
n
}
})
.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 @@ fn display_item_long(

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 @@ fn display_item_long(
}

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 @@ fn display_item_name(
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 @@ fn display_item_name(
}

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 @@ fn display_item_name(

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

Expand Down Expand Up @@ -3257,14 +3245,10 @@ fn display_item_name(
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

0 comments on commit 3346b41

Please sign in to comment.