Skip to content

Commit

Permalink
ls --dired -R: verify also the SUBDIRED coordinate
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Oct 1, 2023
1 parent 680f31c commit 4397b7f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/uu/ls/src/dired.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ pub fn indent(out: &mut BufWriter<Stdout>) -> UResult<()> {
}

pub fn calculate_subdired(dired: &mut DiredOutput, path_len: usize) {
// if we have several directories:
let offset_from_previous_line = get_offset_from_previous_line(&dired.dired_positions);

let additional_offset = if dired.subdired_positions.is_empty() {
0
} else {
// if we have several directories: \n\n
2
};

dired.subdired_positions.push(BytePosition {
start: offset_from_previous_line + DIRED_TRAILING_OFFSET,
end: offset_from_previous_line + path_len + DIRED_TRAILING_OFFSET,
start: offset_from_previous_line + DIRED_TRAILING_OFFSET + additional_offset,
end: offset_from_previous_line + path_len + DIRED_TRAILING_OFFSET + additional_offset,
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2148,10 +2148,10 @@ fn enter_directory(
if config.dired {
// We already injected the first dir
// Continue with the others
// 4= \n + \n " "
dired.padding = 4;
// 2 = \n + \n
dired.padding = 2;
dired::indent(out)?;
let dir_name_size = e.display_name.len();
let dir_name_size = e.p_buf.to_string_lossy().len();
dired::calculate_subdired(dired, dir_name_size);
// inject dir name
dired::add_dir_name(dir_name_size, dired);
Expand Down
49 changes: 49 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3707,3 +3707,52 @@ fn test_ls_dired_complex() {
println!("Extracted filenames: {:?}", filenames);
assert_eq!(filenames, vec!["a1", "a22", "a333", "a4444", "d"]);
}

#[test]
fn test_ls_subdired_complex() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.mkdir("dir1");
at.mkdir("dir1/d");
at.mkdir("dir1/c2");
at.touch("dir1/a1");
at.touch("dir1/a22");
at.touch("dir1/a333");
at.touch("dir1/c2/a4444");

let mut cmd = scene.ucmd();
cmd.arg("--dired").arg("-l").arg("-R").arg("dir1");
let result = cmd.succeeds();

let output = result.stdout_str().to_string();
println!("Output:\n{}", output);

let dired_line = output
.lines()
.find(|&line| line.starts_with("//SUBDIRED//"))
.unwrap();
let positions: Vec<usize> = dired_line
.split_whitespace()
.skip(1)
.map(|s| s.parse().unwrap())
.collect();
println!("{:?}", positions);
println!("Parsed byte positions: {:?}", positions);
assert_eq!(positions.len() % 2, 0); // Ensure there's an even number of positions

let dirnames: Vec<String> = positions
.chunks(2)
.map(|chunk| {
let start_pos = chunk[0];
let end_pos = chunk[1];
let dirname =
String::from_utf8(output.as_bytes()[start_pos..end_pos].to_vec()).unwrap();
println!("Extracted dirname: {}", dirname);
dirname
})
.collect();

println!("Extracted dirnames: {:?}", dirnames);
assert_eq!(dirnames, vec!["dir1", "dir1/c2", "dir1/d"]);
}

0 comments on commit 4397b7f

Please sign in to comment.