Skip to content

Commit

Permalink
dired: always put dired first in the args + minor fixes
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Hofstetter <[email protected]>
  • Loading branch information
sylvestre and cakebaker committed Oct 5, 2023
1 parent 68bcf23 commit 289c31c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
36 changes: 17 additions & 19 deletions src/uu/ls/src/dired.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ fn get_offset_from_previous_line(dired_positions: &[BytePosition]) -> usize {

/// Calculates the byte positions for DIRED
pub fn calculate_dired(
dired_positions: &[BytePosition],
output_display_len: usize,
dfn_len: usize,
dired_positions: &[BytePosition],
) -> (usize, usize) {
let offset_from_previous_line = get_offset_from_previous_line(dired_positions);

Expand All @@ -68,10 +68,9 @@ pub fn calculate_subdired(dired: &mut DiredOutput, path_len: usize) {
2
};

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

/// Prints the dired output based on the given configuration and dired structure.
Expand All @@ -81,7 +80,6 @@ pub fn print_dired_output(
out: &mut BufWriter<Stdout>,
) -> UResult<()> {
out.flush()?;
// TODO manage when -R and the last doesn't have file
if dired.padding == 0 && !dired.dired_positions.is_empty() {
print_positions("//DIRED//", &dired.dired_positions);
}
Expand All @@ -101,7 +99,7 @@ fn print_positions(prefix: &str, positions: &Vec<BytePosition>) {
println!();
}

pub fn add_total(total_len: usize, dired: &mut DiredOutput) {
pub fn add_total(dired: &mut DiredOutput, total_len: usize) {
if dired.padding == 0 {
let offset_from_previous_line = get_offset_from_previous_line(&dired.dired_positions);
// when dealing with " total: xx", it isn't part of the //DIRED//
Expand All @@ -116,16 +114,16 @@ pub fn add_total(total_len: usize, dired: &mut DiredOutput) {
}

// when using -R, we have the dirname. we need to add it to the padding
pub fn add_dir_name(dir_len: usize, dired: &mut DiredOutput) {
pub fn add_dir_name(dired: &mut DiredOutput, dir_len: usize) {
// 1 for the ":" in " dirname:"
dired.padding += dir_len + DIRED_TRAILING_OFFSET + 1;
}

/// Calculates byte positions and updates the dired structure.
pub fn calculate_and_update_positions(
dired: &mut DiredOutput,
output_display_len: usize,
dfn_len: usize,
dired: &mut DiredOutput,
) {
let offset = dired
.dired_positions
Expand All @@ -135,13 +133,13 @@ pub fn calculate_and_update_positions(
});
let start = output_display_len + offset + DIRED_TRAILING_OFFSET;
let end = start + dfn_len;
update_positions(start, end, dired);
update_positions(dired, start, end);
}

/// Updates the dired positions based on the given start and end positions.
/// update when it is the first element in the list (to manage "total X")
/// insert when it isn't the about total
pub fn update_positions(start: usize, end: usize, dired: &mut DiredOutput) {
pub fn update_positions(dired: &mut DiredOutput, start: usize, end: usize) {
// padding can be 0 but as it doesn't matter
dired.dired_positions.push(BytePosition {
start: start + dired.padding,
Expand All @@ -160,7 +158,7 @@ mod tests {
let output_display = "sample_output".to_string();
let dfn = "sample_file".to_string();
let dired_positions = vec![BytePosition { start: 5, end: 10 }];
let (start, end) = calculate_dired(output_display.len(), dfn.len(), &dired_positions);
let (start, end) = calculate_dired(&dired_positions, output_display.len(), dfn.len());

assert_eq!(start, 24);
assert_eq!(end, 35);
Expand Down Expand Up @@ -206,7 +204,7 @@ mod tests {
padding: 0,
};
let dir_len = 5;
add_dir_name(dir_len, &mut dired);
add_dir_name(&mut dired, dir_len);
assert_eq!(
dired,
DiredOutput {
Expand Down Expand Up @@ -235,7 +233,7 @@ mod tests {
};
// if we have "total: 2"
let total_len = 8;
add_total(total_len, &mut dired);
add_total(&mut dired, total_len);
// 22 = 8 (len) + 2 (padding) + 11 (previous position) + 1 (\n)
assert_eq!(dired.padding, 22);
}
Expand All @@ -257,12 +255,12 @@ mod tests {
padding: 0,
};
let dir_len = 5;
add_dir_name(dir_len, &mut dired);
add_dir_name(&mut dired, dir_len);
// 8 = 2 (" ") + 1 (\n) + 5 + 1 (: of dirname)
assert_eq!(dired.padding, 8);

let total_len = 8;
add_total(total_len, &mut dired);
add_total(&mut dired, total_len);
assert_eq!(dired.padding, 18);
}

Expand All @@ -275,13 +273,13 @@ mod tests {
};

// Test with adjust = true
update_positions(15, 20, &mut dired);
update_positions(&mut dired, 15, 20);
let last_position = dired.dired_positions.last().unwrap();
assert_eq!(last_position.start, 25); // 15 + 10 (end of the previous position)
assert_eq!(last_position.end, 30); // 20 + 10 (end of the previous position)

// Test with adjust = false
update_positions(30, 35, &mut dired);
update_positions(&mut dired, 30, 35);
let last_position = dired.dired_positions.last().unwrap();
assert_eq!(last_position.start, 30);
assert_eq!(last_position.end, 35);
Expand All @@ -300,7 +298,7 @@ mod tests {
};
let output_display_len = 15;
let dfn_len = 5;
calculate_and_update_positions(output_display_len, dfn_len, &mut dired);
calculate_and_update_positions(&mut dired, output_display_len, dfn_len);
assert_eq!(
dired.dired_positions,
vec![
Expand Down
18 changes: 9 additions & 9 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1925,17 +1925,17 @@ pub fn list(locs: Vec<&Path>, config: &Config) -> UResult<()> {
}
writeln!(out, "{}:", path_data.p_buf.display())?;
if config.dired {
// Fist directory displayed
// First directory displayed
let dir_len = path_data.display_name.len();
// add the //SUBDIRED// coordinates
dired::calculate_subdired(&mut dired, dir_len);
// Add the padding for the dir name
dired::add_dir_name(dir_len, &mut dired);
dired::add_dir_name(&mut dired, dir_len);
}
} else {
writeln!(out).unwrap();
writeln!(out)?;
show_dir_name(&path_data.p_buf, &mut out);
writeln!(out).unwrap();
writeln!(out)?;
}
}
let mut listed_ancestors = HashSet::new();
Expand Down Expand Up @@ -2114,7 +2114,7 @@ fn enter_directory(
let total = return_total(&entries, config, out)?;
write!(out, "{}", total.as_str())?;
if config.dired {
dired::add_total(total.len(), dired);
dired::add_total(dired, total.len());
}
}

Expand Down Expand Up @@ -2154,7 +2154,7 @@ fn enter_directory(
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);
dired::add_dir_name(dired, dir_name_size);
}

show_dir_name(&e.p_buf, out);
Expand Down Expand Up @@ -2573,11 +2573,11 @@ fn display_item_long(
let displayed_file = display_file_name(item, config, None, String::new(), out).contents;
if config.dired {
let (start, end) = dired::calculate_dired(
&dired.dired_positions,
output_display.len(),
displayed_file.len(),
&dired.dired_positions,
);
dired::update_positions(start, end, dired);
dired::update_positions(dired, start, end);
}
write!(output_display, "{}{}", displayed_file, config.line_ending).unwrap();
} else {
Expand Down Expand Up @@ -2665,9 +2665,9 @@ fn display_item_long(

if config.dired {
dired::calculate_and_update_positions(
dired,
output_display.len(),
displayed_file.trim().len(),
dired,
);
}
write!(output_display, "{}{}", displayed_file, config.line_ending).unwrap();
Expand Down
6 changes: 2 additions & 4 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3592,15 +3592,15 @@ fn test_ls_dired_recursive_multiple() {
at.touch("d/d2/c2");
at.touch("d/d1/f1");
at.touch("d/d1/file-long");
let mut cmd = scene.ucmd();

let mut cmd = scene.ucmd();
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//"))
Expand All @@ -3610,7 +3610,6 @@ fn test_ls_dired_recursive_multiple() {
.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

Expand Down Expand Up @@ -3760,7 +3759,6 @@ fn test_ls_subdired_complex() {
.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

Expand Down

0 comments on commit 289c31c

Please sign in to comment.