Skip to content

Commit 6f696b9

Browse files
CrazyRokasylvestre
authored andcommitted
ptx: fix incorrect column width calculation and padding logic
1 parent 54ba74b commit 6f696b9

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/uu/ptx/src/ptx.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,17 @@ fn format_dumb_line(
614614
};
615615

616616
// Calculate the width for the left half (before the keyword)
617-
let half_width = config.line_width / 2;
617+
let half_width = cmp::max(config.line_width / 2, config.gap_size);
618+
619+
let left_part_len = if left_part.contains(&config.trunc_str) {
620+
left_part.len() - config.trunc_str.len()
621+
} else {
622+
left_part.len()
623+
};
618624

619625
// Right-justify the left part within the left half
620626
let padding = if left_part.len() < half_width {
621-
half_width - left_part.len()
627+
half_width - left_part_len
622628
} else {
623629
0
624630
};

tests/by-util/test_ptx.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,40 @@ fn test_gnu_mode_dumb_format() {
264264
" a b\n a b\n",
265265
);
266266
}
267+
268+
#[test]
269+
fn test_gnu_compatibility_narrow_width() {
270+
new_ucmd!()
271+
.args(&["-w", "2"])
272+
.pipe_in("qux")
273+
.succeeds()
274+
.stdout_only(" qux\n");
275+
}
276+
277+
#[test]
278+
fn test_gnu_compatibility_truncation_width() {
279+
new_ucmd!()
280+
.args(&["-w", "10"])
281+
.pipe_in("foo bar")
282+
.succeeds()
283+
.stdout_only(" / bar\n foo/\n");
284+
}
285+
286+
#[test]
287+
fn test_unicode_padding_alignment() {
288+
let input = "a\né";
289+
new_ucmd!()
290+
.args(&["-w", "10"])
291+
.pipe_in(input)
292+
.succeeds()
293+
.stdout_only(" a\n é\n");
294+
}
295+
296+
#[test]
297+
fn test_unicode_truncation_alignment() {
298+
new_ucmd!()
299+
.args(&["-w", "10"])
300+
.pipe_in("föö bar")
301+
.succeeds()
302+
.stdout_only(" / bar\n föö/\n");
303+
}

0 commit comments

Comments
 (0)