mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 15:22:38 +00:00
Merge pull request #5305 from sylvestre/dired-prefix
ls --dired: replace the previous "total: xx" padding method
This commit is contained in:
commit
50255a2463
3 changed files with 22 additions and 33 deletions
|
@ -20,7 +20,7 @@ pub struct BytePosition {
|
||||||
pub struct DiredOutput {
|
pub struct DiredOutput {
|
||||||
pub dired_positions: Vec<BytePosition>,
|
pub dired_positions: Vec<BytePosition>,
|
||||||
pub subdired_positions: Vec<BytePosition>,
|
pub subdired_positions: Vec<BytePosition>,
|
||||||
pub just_printed_total: bool,
|
pub padding: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for BytePosition {
|
impl fmt::Display for BytePosition {
|
||||||
|
@ -75,7 +75,7 @@ pub fn print_dired_output(
|
||||||
out.flush()?;
|
out.flush()?;
|
||||||
if config.recursive {
|
if config.recursive {
|
||||||
print_positions("//SUBDIRED//", &dired.subdired_positions);
|
print_positions("//SUBDIRED//", &dired.subdired_positions);
|
||||||
} else if !dired.just_printed_total {
|
} else if dired.padding == 0 {
|
||||||
print_positions("//DIRED//", &dired.dired_positions);
|
print_positions("//DIRED//", &dired.dired_positions);
|
||||||
}
|
}
|
||||||
println!("//DIRED-OPTIONS// --quoting-style={}", config.quoting_style);
|
println!("//DIRED-OPTIONS// --quoting-style={}", config.quoting_style);
|
||||||
|
@ -92,12 +92,9 @@ fn print_positions(prefix: &str, positions: &Vec<BytePosition>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_total(total_len: usize, dired: &mut DiredOutput) {
|
pub fn add_total(total_len: usize, dired: &mut DiredOutput) {
|
||||||
dired.just_printed_total = true;
|
// when dealing with " total: xx", it isn't part of the //DIRED//
|
||||||
dired.dired_positions.push(BytePosition {
|
// so, we just keep the size line to add it to the position of the next file
|
||||||
start: 0,
|
dired.padding = total_len + DIRED_TRAILING_OFFSET;
|
||||||
// the 1 is from the line ending (\n)
|
|
||||||
end: total_len + DIRED_TRAILING_OFFSET - 1,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculates byte positions and updates the dired structure.
|
/// Calculates byte positions and updates the dired structure.
|
||||||
|
@ -114,28 +111,20 @@ pub fn calculate_and_update_positions(
|
||||||
});
|
});
|
||||||
let start = output_display_len + offset + DIRED_TRAILING_OFFSET;
|
let start = output_display_len + offset + DIRED_TRAILING_OFFSET;
|
||||||
let end = start + dfn_len;
|
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.
|
/// 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
|
/// insert when it isn't the about total
|
||||||
pub fn update_positions(start: usize, end: usize, dired: &mut DiredOutput, adjust: bool) {
|
pub fn update_positions(start: usize, end: usize, dired: &mut DiredOutput) {
|
||||||
if dired.just_printed_total {
|
// padding can be 0 but as it doesn't matter<
|
||||||
if let Some(last_position) = dired.dired_positions.last_mut() {
|
dired.dired_positions.push(BytePosition {
|
||||||
*last_position = BytePosition {
|
start: start + dired.padding,
|
||||||
start: if adjust {
|
end: end + dired.padding,
|
||||||
start + last_position.end
|
});
|
||||||
} else {
|
// Remove the previous padding
|
||||||
start
|
dired.padding = 0;
|
||||||
},
|
|
||||||
end: if adjust { end + last_position.end } else { end },
|
|
||||||
};
|
|
||||||
dired.just_printed_total = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
dired.dired_positions.push(BytePosition { start, end });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -158,17 +147,17 @@ mod tests {
|
||||||
let mut dired = DiredOutput {
|
let mut dired = DiredOutput {
|
||||||
dired_positions: vec![BytePosition { start: 5, end: 10 }],
|
dired_positions: vec![BytePosition { start: 5, end: 10 }],
|
||||||
subdired_positions: vec![],
|
subdired_positions: vec![],
|
||||||
just_printed_total: true,
|
padding: 10,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Test with adjust = true
|
// 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();
|
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.start, 25); // 15 + 10 (end of the previous position)
|
||||||
assert_eq!(last_position.end, 30); // 20 + 10 (end of the previous position)
|
assert_eq!(last_position.end, 30); // 20 + 10 (end of the previous position)
|
||||||
|
|
||||||
// Test with adjust = false
|
// 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();
|
let last_position = dired.dired_positions.last().unwrap();
|
||||||
assert_eq!(last_position.start, 30);
|
assert_eq!(last_position.start, 30);
|
||||||
assert_eq!(last_position.end, 35);
|
assert_eq!(last_position.end, 35);
|
||||||
|
|
|
@ -2550,7 +2550,7 @@ fn display_item_long(
|
||||||
displayed_file.len(),
|
displayed_file.len(),
|
||||||
&dired.dired_positions,
|
&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();
|
write!(output_display, "{}{}", displayed_file, config.line_ending).unwrap();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -261,11 +261,11 @@ sed -i -e "s/Try 'mv --help' for more information/For more information, try '--h
|
||||||
# disable these test cases
|
# disable these test cases
|
||||||
sed -i -E "s|^([^#]*2_31.*)$|#\1|g" tests/printf/printf-cov.pl
|
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
|
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
|
||||||
|
|
||||||
|
# with ls --dired, in case of error, we have a slightly different error position
|
||||||
|
sed -i -e "s|44 45|48 49|" tests/ls/stat-failed.sh
|
||||||
|
|
||||||
# disable two kind of tests:
|
# disable two kind of tests:
|
||||||
# "hostid BEFORE --help" doesn't fail for GNU. we fail. we are probably doing better
|
# "hostid BEFORE --help" doesn't fail for GNU. we fail. we are probably doing better
|
||||||
# "hostid BEFORE --help AFTER " same for this
|
# "hostid BEFORE --help AFTER " same for this
|
||||||
|
|
Loading…
Reference in a new issue