Skip to content

Commit

Permalink
improve subdired support
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Sep 22, 2023
1 parent 48fc80d commit 5c5f91d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
34 changes: 19 additions & 15 deletions src/uu/ls/src/dired.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ impl fmt::Display for BytePosition {
// When --dired is used, all lines starts with 2 spaces
static DIRED_TRAILING_OFFSET: usize = 2;

fn get_offset_from_previous_line(dired_positions: &[BytePosition]) -> usize {
if let Some(last_position) = dired_positions.last() {
last_position.end + 1
} else {
0
}
}

/// Calculates the byte positions for DIRED
pub fn calculate_dired(
output_display_len: usize,
dfn_len: usize,
dired_positions: &[BytePosition],
) -> (usize, usize) {
let offset_from_previous_line = if let Some(last_position) = dired_positions.last() {
last_position.end + 1
} else {
0
};
let offset_from_previous_line = get_offset_from_previous_line(dired_positions);

let start = output_display_len + offset_from_previous_line;
let end = start + dfn_len;
Expand All @@ -55,14 +59,10 @@ pub fn indent(out: &mut BufWriter<Stdout>) -> UResult<()> {
}

pub fn calculate_subdired(dired: &mut DiredOutput, path_len: usize) {
let offset = if dired.subdired_positions.is_empty() {
DIRED_TRAILING_OFFSET
} else {
dired.subdired_positions[dired.subdired_positions.len() - 1].start + DIRED_TRAILING_OFFSET
};
let offset_from_previous_line = get_offset_from_previous_line(&dired.dired_positions);
dired.subdired_positions.push(BytePosition {
start: offset,
end: path_len + offset,
start: offset_from_previous_line + DIRED_TRAILING_OFFSET,
end: offset_from_previous_line + path_len + DIRED_TRAILING_OFFSET,
});
}

Expand All @@ -73,7 +73,8 @@ pub fn print_dired_output(
out: &mut BufWriter<Stdout>,
) -> UResult<()> {
out.flush()?;
if !dired.just_printed_total {
// TODO manage when -R and the last doesn't have file
if !dired.just_printed_total && dired.dired_positions.len() >= 1 {

Check failure on line 77 in src/uu/ls/src/dired.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (macos-12, unix)

ERROR: `cargo clippy`: length comparison to one (file:'src/uu/ls/src/dired.rs', line:77)
print_positions("//DIRED//", &dired.dired_positions);
}
if config.recursive {
Expand All @@ -93,11 +94,14 @@ fn print_positions(prefix: &str, positions: &Vec<BytePosition>) {
}

pub fn add_total(total_len: usize, dired: &mut DiredOutput) {
// if we are using ls -R, the position N+1.. need to be with an offset

let offset_from_previous_line = get_offset_from_previous_line(&dired.dired_positions);
dired.just_printed_total = true;
dired.dired_positions.push(BytePosition {
start: 0,
start: offset_from_previous_line + 0,

Check failure on line 102 in src/uu/ls/src/dired.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (macos-12, unix)

ERROR: `cargo clippy`: this operation has no effect (file:'src/uu/ls/src/dired.rs', line:102)
// the 1 is from the line ending (\n)
end: total_len + DIRED_TRAILING_OFFSET - 1,
end: offset_from_previous_line + total_len + DIRED_TRAILING_OFFSET - 1,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2141,7 +2141,9 @@ fn enter_directory(
write!(out, "\n")?;

Check failure on line 2141 in src/uu/ls/src/ls.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (macos-12, unix)

ERROR: `cargo clippy`: using `write!()` with a format string that ends in a single newline (file:'src/uu/ls/src/ls.rs', line:2141)
if config.dired {
dired::indent(out)?;
dired::calculate_subdired(dired, e.display_name.len());
}

show_dir_name(&e.p_buf, out);
write!(out, "\n")?;

Check failure on line 2148 in src/uu/ls/src/ls.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (macos-12, unix)

ERROR: `cargo clippy`: using `write!()` with a format string that ends in a single newline (file:'src/uu/ls/src/ls.rs', line:2148)
enter_directory(e, rd, config, out, listed_ancestors, dired)?;
Expand Down
62 changes: 48 additions & 14 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3552,27 +3552,61 @@ fn test_ls_dired_recursive() {
}

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

at.mkdir("d");
at.mkdir("d/d1");
at.mkdir("d/d2");
at.touch("d/d2/a");
at.touch("d/d2/ab");
at.touch("d/d1/ab");
let mut cmd = scene.ucmd();

let result = scene
.ucmd()
.arg("--dired")
.arg("-l")
.arg("-R")
.arg("d")
.succeeds()
.stdout_does_not_contain("//DIRED//")
.stdout_contains(" d/d1:")
.stdout_contains(" d/d2:")
.stdout_contains(" total 0")
.stdout_contains("//SUBDIRED// 2 3")
.stdout_contains("//DIRED-OPTIONS// --quoting-style");
cmd.arg("--dired").arg("-l").arg("-R").arg("d");

let result = cmd.succeeds();

let output = result.stdout_str().to_string();
println!("Output:\n{}", output);
// TODO: Il manque le offset du direname
let dired_line = output
.lines()
.find(|&line| line.starts_with("//DIRED//"))
.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 filenames: Vec<String> = positions
.chunks(2)
.map(|chunk| {
let start_pos = chunk[0];
let end_pos = chunk[1];
let filename = String::from_utf8(output.as_bytes()[start_pos..=end_pos].to_vec())
.unwrap()
.trim()
.to_string();
println!("Extracted filename: {}", filename);
filename
})
.collect();

println!("Extracted filenames: {:?}", filenames);
assert_eq!(filenames, vec!["a1", "a22", "a333", "a4444", "d"]);

/* .stdout_contains(" d/d1:")
.stdout_contains(" d/d2:")
.stdout_contains(" total 0")
.stdout_contains("//DIRED//")
.stdout_contains("//SUBDIRED// 2 3")
.stdout_contains("//DIRED-OPTIONS// --quoting-style");*/
}

#[test]
Expand Down

0 comments on commit 5c5f91d

Please sign in to comment.