Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/uu/ls/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,13 @@ enum SizeOrDeviceId {
/// dir1: <- This as well
/// file11
/// ```
/// Returns the number of bytes the rendered name occupies, which `--dired`
/// needs to place the header in `//SUBDIRED//`.
pub fn show_dir_name(
path_data: &PathData,
out: &mut BufWriter<Stdout>,
config: &Config,
) -> std::io::Result<()> {
) -> std::io::Result<usize> {
let escaped_name = escape_dir_name_with_locale(path_data.path().as_os_str(), config);

let name = if config.hyperlink && !config.dired {
Expand All @@ -171,7 +173,8 @@ pub fn show_dir_name(
};

write_os_str(out, &name)?;
write!(out, ":")
write!(out, ":")?;
Ok(name.len())
}

fn escape_with_locale<F>(name: &OsStr, config: &Config, fallback: F) -> OsString
Expand Down
30 changes: 12 additions & 18 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,29 +1078,23 @@ impl LsOutput for TextOutput<'_> {
config: &Config,
is_first: bool,
) -> UResult<()> {
if is_first {
if config.dired {
dired::indent(&mut self.state.out)?;
}
show_dir_name(path_data, &mut self.state.out, config)?;
writeln!(self.state.out)?;
if config.dired {
let dir_len = path_data.path().as_os_str().len();
dired::calculate_subdired(&mut self.dired, dir_len);
dired::add_dir_name(&mut self.dired, dir_len);
}
} else {
if !is_first {
writeln!(self.state.out)?;
if config.dired {
self.dired.line_offset += 1; // account for the blank line before recursive directory headings
self.dired.padding = 0;
dired::indent(&mut self.state.out)?;
let dir_name_size = path_data.path().as_os_str().len();
dired::calculate_subdired(&mut self.dired, dir_name_size);
dired::add_dir_name(&mut self.dired, dir_name_size);
}
show_dir_name(path_data, &mut self.state.out, config)?;
writeln!(self.state.out)?;
}
if config.dired {
dired::indent(&mut self.state.out)?;
}
let name_len = show_dir_name(path_data, &mut self.state.out, config)?;
writeln!(self.state.out)?;
if config.dired {
// The header is rendered with the quoting style in force, so the
// offsets must follow the rendered name, not the raw path.
dired::calculate_subdired(&mut self.dired, name_len);
dired::add_dir_name(&mut self.dired, name_len);
}
Ok(())
}
Expand Down
38 changes: 36 additions & 2 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5664,6 +5664,31 @@ fn test_ls_dired_order_format() {
.stdout_contains("//DIRED//");
}

#[test]
fn test_ls_dired_offsets_follow_quoted_dir_headers() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.mkdir("a b");
at.touch("a b/x");
at.mkdir("it's");
at.touch("it's/y");

// Quoting lengthens the directory headers; both offset lists must follow
// the rendered header rather than the raw path.
let result = scene
.ucmd()
.args(&[
"--dired",
"-R",
"--quoting-style=shell-escape",
"a b",
"it's",
])
.succeeds();
assert_eq!(dired_names(result.stdout_str()), ["x", "y"]);
assert_eq!(subdired_names(result.stdout_str()), ["'a b'", "\"it's\""]);
}

#[test]
fn test_ls_dired_and_zero_are_incompatible() {
let scene = TestScenario::new(util_name!());
Expand Down Expand Up @@ -5916,10 +5941,19 @@ fn test_ls_dired_symlink_name_only() {

/// Extracts the file names delimited by the //DIRED// byte offsets.
fn dired_names(output: &str) -> Vec<String> {
names_at_offsets(output, "//DIRED//")
}

/// Extracts the directory headers delimited by the //SUBDIRED// byte offsets.
fn subdired_names(output: &str) -> Vec<String> {
names_at_offsets(output, "//SUBDIRED//")
}

fn names_at_offsets(output: &str, tag: &str) -> Vec<String> {
let dired_line = output
.lines()
.find(|&line| line.starts_with("//DIRED//"))
.unwrap();
.find(|&line| line.starts_with(tag))
.unwrap_or_else(|| panic!("no {tag} line in the output"));
let positions: Vec<usize> = dired_line
.split_whitespace()
.skip(1)
Expand Down
Loading