1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

ls: unquoted names should be indented with a space if there is a quoted name (#5740)

* ls: change indentation when file names include quotes to match GNU's

* display_grid: Changed order of parameters because it made more sense

* ls -l: Add padding for unquoted filenames

* Fix checking for quoted filenames

* Simplify if
This commit is contained in:
Matei Mantu 2023-12-29 16:19:26 +02:00 committed by GitHub
parent 448e4c2224
commit 21d09dd404
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2414,6 +2414,11 @@ fn display_items(
// Display the SELinux security context or '?' if none is found. When used with the `-l` // Display the SELinux security context or '?' if none is found. When used with the `-l`
// option, print the security context to the left of the size column. // option, print the security context to the left of the size column.
let quoted = items.iter().any(|item| {
let name = escape_name(&item.display_name, &config.quoting_style);
name.starts_with('\'')
});
if config.format == Format::Long { if config.format == Format::Long {
let padding_collection = calculate_padding_collection(items, config, out); let padding_collection = calculate_padding_collection(items, config, out);
@ -2431,7 +2436,15 @@ fn display_items(
display_additional_leading_info(item, &padding_collection, config, out)?; display_additional_leading_info(item, &padding_collection, config, out)?;
write!(out, "{more_info}")?; write!(out, "{more_info}")?;
} }
display_item_long(item, &padding_collection, config, out, dired, style_manager)?; display_item_long(
item,
&padding_collection,
config,
out,
dired,
style_manager,
quoted,
)?;
} }
} else { } else {
let mut longest_context_len = 1; let mut longest_context_len = 1;
@ -2448,7 +2461,6 @@ fn display_items(
let padding = calculate_padding_collection(items, config, out); let padding = calculate_padding_collection(items, config, out);
let mut names_vec = Vec::new(); let mut names_vec = Vec::new();
for i in items { for i in items {
let more_info = display_additional_leading_info(i, &padding, config, out)?; let more_info = display_additional_leading_info(i, &padding, config, out)?;
let cell = display_item_name(i, config, prefix_context, more_info, out, style_manager); let cell = display_item_name(i, config, prefix_context, more_info, out, style_manager);
@ -2458,8 +2470,12 @@ fn display_items(
let names = names_vec.into_iter(); let names = names_vec.into_iter();
match config.format { match config.format {
Format::Columns => display_grid(names, config.width, Direction::TopToBottom, out)?, Format::Columns => {
Format::Across => display_grid(names, config.width, Direction::LeftToRight, out)?, display_grid(names, config.width, Direction::TopToBottom, out, quoted)?;
}
Format::Across => {
display_grid(names, config.width, Direction::LeftToRight, out, quoted)?;
}
Format::Commas => { Format::Commas => {
let mut current_col = 0; let mut current_col = 0;
let mut names = names; let mut names = names;
@ -2524,6 +2540,7 @@ fn display_grid(
width: u16, width: u16,
direction: Direction, direction: Direction,
out: &mut BufWriter<Stdout>, out: &mut BufWriter<Stdout>,
quoted: bool,
) -> UResult<()> { ) -> UResult<()> {
if width == 0 { if width == 0 {
// If the width is 0 we print one single line // If the width is 0 we print one single line
@ -2545,7 +2562,15 @@ fn display_grid(
let mut grid = Grid::new(GridOptions { filling, direction }); let mut grid = Grid::new(GridOptions { filling, direction });
for name in names { for name in names {
grid.add(name); 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) { match grid.fit_into_width(width as usize) {
@ -2597,6 +2622,7 @@ fn display_item_long(
out: &mut BufWriter<Stdout>, out: &mut BufWriter<Stdout>,
dired: &mut DiredOutput, dired: &mut DiredOutput,
style_manager: &mut StyleManager, style_manager: &mut StyleManager,
quoted: bool,
) -> UResult<()> { ) -> UResult<()> {
let mut output_display: String = String::new(); let mut output_display: String = String::new();
if config.dired { if config.dired {
@ -2689,8 +2715,15 @@ fn display_item_long(
write!(output_display, " {} ", display_date(md, config)).unwrap(); write!(output_display, " {} ", display_date(md, config)).unwrap();
let displayed_item = let item_name =
display_item_name(item, config, None, String::new(), out, style_manager).contents; display_item_name(item, config, None, String::new(), out, style_manager).contents;
let displayed_item = if quoted && !item_name.starts_with('\'') {
format!(" {}", item_name)
} else {
item_name
};
if config.dired { if config.dired {
let (start, end) = dired::calculate_dired( let (start, end) = dired::calculate_dired(
&dired.dired_positions, &dired.dired_positions,