Skip to content

Commit

Permalink
ls --dired: replace the previous "total: xx" padding method
Browse files Browse the repository at this point in the history
by something easier
  • Loading branch information
sylvestre committed Sep 22, 2023
1 parent 4094231 commit 134dbc3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
47 changes: 18 additions & 29 deletions src/uu/ls/src/dired.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct BytePosition {
pub struct DiredOutput {
pub dired_positions: Vec<BytePosition>,
pub subdired_positions: Vec<BytePosition>,
pub just_printed_total: bool,
pub padding: usize,
}

impl fmt::Display for BytePosition {
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn print_dired_output(
out.flush()?;
if config.recursive {
print_positions("//SUBDIRED//", &dired.subdired_positions);
} else if !dired.just_printed_total {
} else if dired.padding == 0 {
print_positions("//DIRED//", &dired.dired_positions);
}
println!("//DIRED-OPTIONS// --quoting-style={}", config.quoting_style);
Expand All @@ -92,12 +92,9 @@ fn print_positions(prefix: &str, positions: &Vec<BytePosition>) {
}

pub fn add_total(total_len: usize, dired: &mut DiredOutput) {
dired.just_printed_total = true;
dired.dired_positions.push(BytePosition {
start: 0,
// the 1 is from the line ending (\n)
end: total_len + DIRED_TRAILING_OFFSET - 1,
});
// when dealing with " total: xx", it isn't part of the //DIRED//
// so, we just keep the size line to add it to the position of the next file
dired.padding = total_len + DIRED_TRAILING_OFFSET;
}

/// Calculates byte positions and updates the dired structure.
Expand All @@ -114,28 +111,20 @@ 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, true);
update_positions(start, end, dired);
}

/// 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"
/// 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, adjust: bool) {
if dired.just_printed_total {
if let Some(last_position) = dired.dired_positions.last_mut() {
*last_position = BytePosition {
start: if adjust {
start + last_position.end
} else {
start
},
end: if adjust { end + last_position.end } else { end },
};
dired.just_printed_total = false;
}
} else {
dired.dired_positions.push(BytePosition { start, end });
}
pub fn update_positions(start: usize, end: usize, dired: &mut DiredOutput) {
// padding can be 0 but as it doesn't matter<
dired.dired_positions.push(BytePosition {
start: start + dired.padding,
end: end + dired.padding,
});
// Remove the previous padding
dired.padding = 0;
}

#[cfg(test)]
Expand All @@ -158,17 +147,17 @@ mod tests {
let mut dired = DiredOutput {
dired_positions: vec![BytePosition { start: 5, end: 10 }],
subdired_positions: vec![],
just_printed_total: true,
padding: 10,
};

// Test with adjust = true
update_positions(15, 20, &mut dired, true);
update_positions(15, 20, &mut dired);
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, false);
update_positions(30, 35, &mut dired);
let last_position = dired.dired_positions.last().unwrap();
assert_eq!(last_position.start, 30);
assert_eq!(last_position.end, 35);
Expand Down
2 changes: 1 addition & 1 deletion src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2550,7 +2550,7 @@ fn display_item_long(
displayed_file.len(),
&dired.dired_positions,
);
dired::update_positions(start, end, dired, false);
dired::update_positions(start, end, dired);
}
write!(output_display, "{}{}", displayed_file, config.line_ending).unwrap();
} else {
Expand Down
3 changes: 0 additions & 3 deletions util/build-gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,6 @@ sed -i -e "s/Try 'mv --help' for more information/For more information, try '--h
# disable these test cases
sed -i -E "s|^([^#]*2_31.*)$|#\1|g" tests/printf/printf-cov.pl

# with ls --dired, in case of error, we have a slightly different error position
sed -i -e "s|44 45|47 48|" tests/ls/stat-failed.sh

sed -i -e "s/du: invalid -t argument/du: invalid --threshold argument/" -e "s/du: option requires an argument/error: a value is required for '--threshold <SIZE>' but none was supplied/" -e "/Try 'du --help' for more information./d" tests/du/threshold.sh

# disable two kind of tests:
Expand Down

0 comments on commit 134dbc3

Please sign in to comment.