Skip to content

Commit 50255a2

Browse files
Merge pull request #5305 from sylvestre/dired-prefix
ls --dired: replace the previous "total: xx" padding method
2 parents bd0fb81 + 48613b4 commit 50255a2

3 files changed

Lines changed: 22 additions & 33 deletions

File tree

src/uu/ls/src/dired.rs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct BytePosition {
2020
pub struct DiredOutput {
2121
pub dired_positions: Vec<BytePosition>,
2222
pub subdired_positions: Vec<BytePosition>,
23-
pub just_printed_total: bool,
23+
pub padding: usize,
2424
}
2525

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

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

103100
/// Calculates byte positions and updates the dired structure.
@@ -114,28 +111,20 @@ pub fn calculate_and_update_positions(
114111
});
115112
let start = output_display_len + offset + DIRED_TRAILING_OFFSET;
116113
let end = start + dfn_len;
117-
update_positions(start, end, dired, true);
114+
update_positions(start, end, dired);
118115
}
119116

120117
/// Updates the dired positions based on the given start and end positions.
121-
/// update when it is the first element in the list (to manage "total X"
118+
/// update when it is the first element in the list (to manage "total X")
122119
/// insert when it isn't the about total
123-
pub fn update_positions(start: usize, end: usize, dired: &mut DiredOutput, adjust: bool) {
124-
if dired.just_printed_total {
125-
if let Some(last_position) = dired.dired_positions.last_mut() {
126-
*last_position = BytePosition {
127-
start: if adjust {
128-
start + last_position.end
129-
} else {
130-
start
131-
},
132-
end: if adjust { end + last_position.end } else { end },
133-
};
134-
dired.just_printed_total = false;
135-
}
136-
} else {
137-
dired.dired_positions.push(BytePosition { start, end });
138-
}
120+
pub fn update_positions(start: usize, end: usize, dired: &mut DiredOutput) {
121+
// padding can be 0 but as it doesn't matter<
122+
dired.dired_positions.push(BytePosition {
123+
start: start + dired.padding,
124+
end: end + dired.padding,
125+
});
126+
// Remove the previous padding
127+
dired.padding = 0;
139128
}
140129

141130
#[cfg(test)]
@@ -158,17 +147,17 @@ mod tests {
158147
let mut dired = DiredOutput {
159148
dired_positions: vec![BytePosition { start: 5, end: 10 }],
160149
subdired_positions: vec![],
161-
just_printed_total: true,
150+
padding: 10,
162151
};
163152

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

170159
// Test with adjust = false
171-
update_positions(30, 35, &mut dired, false);
160+
update_positions(30, 35, &mut dired);
172161
let last_position = dired.dired_positions.last().unwrap();
173162
assert_eq!(last_position.start, 30);
174163
assert_eq!(last_position.end, 35);

src/uu/ls/src/ls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2550,7 +2550,7 @@ fn display_item_long(
25502550
displayed_file.len(),
25512551
&dired.dired_positions,
25522552
);
2553-
dired::update_positions(start, end, dired, false);
2553+
dired::update_positions(start, end, dired);
25542554
}
25552555
write!(output_display, "{}{}", displayed_file, config.line_ending).unwrap();
25562556
} else {

util/build-gnu.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ sed -i -e "s/Try 'mv --help' for more information/For more information, try '--h
261261
# disable these test cases
262262
sed -i -E "s|^([^#]*2_31.*)$|#\1|g" tests/printf/printf-cov.pl
263263

264-
# with ls --dired, in case of error, we have a slightly different error position
265-
sed -i -e "s|44 45|47 48|" tests/ls/stat-failed.sh
266-
267264
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
268265

266+
# with ls --dired, in case of error, we have a slightly different error position
267+
sed -i -e "s|44 45|48 49|" tests/ls/stat-failed.sh
268+
269269
# disable two kind of tests:
270270
# "hostid BEFORE --help" doesn't fail for GNU. we fail. we are probably doing better
271271
# "hostid BEFORE --help AFTER " same for this

0 commit comments

Comments
 (0)