Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/uu/ptx/src/ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,17 @@ fn format_dumb_line(
};

// Calculate the width for the left half (before the keyword)
let half_width = config.line_width / 2;
let half_width = cmp::max(config.line_width / 2, config.gap_size);

let left_part_len = if left_part.contains(&config.trunc_str) {
left_part.len() - config.trunc_str.len()
} else {
left_part.len()
};

// Right-justify the left part within the left half
let padding = if left_part.len() < half_width {
half_width - left_part.len()
half_width - left_part_len
} else {
0
};
Expand Down
37 changes: 37 additions & 0 deletions tests/by-util/test_ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,40 @@ fn test_gnu_mode_dumb_format() {
" a b\n a b\n",
);
}

#[test]
fn test_gnu_compatibility_narrow_width() {
new_ucmd!()
.args(&["-w", "2"])
.pipe_in("qux")
.succeeds()
.stdout_only(" qux\n");
}

#[test]
fn test_gnu_compatibility_truncation_width() {
new_ucmd!()
.args(&["-w", "10"])
.pipe_in("foo bar")
.succeeds()
.stdout_only(" / bar\n foo/\n");
}

#[test]
fn test_unicode_padding_alignment() {
let input = "a\né";
new_ucmd!()
.args(&["-w", "10"])
.pipe_in(input)
.succeeds()
.stdout_only(" a\n é\n");
}

#[test]
fn test_unicode_truncation_alignment() {
new_ucmd!()
.args(&["-w", "10"])
.pipe_in("föö bar")
.succeeds()
.stdout_only(" / bar\n föö/\n");
}
Loading