Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly render tab stops in --show-all #2038

Merged
merged 6 commits into from
Mar 6, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Features

- Correctly render tab stops in --show-all, see #2038 (@Synthetica9)

## Bugfixes

## Other
Expand Down
13 changes: 10 additions & 3 deletions src/preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,33 @@ pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String {
let tab_width = if tab_width == 0 { 4 } else { tab_width };

let mut idx = 0;
let mut line_idx = 0;
let len = input.len();
while idx < len {
if let Some((chr, skip_ahead)) = try_parse_utf8_char(&input[idx..]) {
idx += skip_ahead;
line_idx += 1;

match chr {
// space
' ' => output.push('·'),
// tab
'\t' => {
if tab_width == 1 {
let tab_stop = tab_width - ((line_idx - 1) % tab_width);
line_idx = 0;
if tab_stop == 1 {
output.push('↹');
} else {
output.push('├');
output.push_str(&"─".repeat(tab_width - 2));
output.push_str(&"─".repeat(tab_stop - 2));
output.push('┤');
}
}
// line feed
'\x0A' => output.push_str("␊\x0A"),
'\x0A' => {
output.push_str("␊\x0A");
line_idx = 0;
}
// carriage return
'\x0D' => output.push('␍'),
// null
Expand Down
46 changes: 46 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,52 @@ fn show_all_mode() {
.stderr("");
}

#[test]
fn show_all_extends_tab_markers_to_next_tabstop() {
bat()
.arg("tabs.txt")
.arg("--show-all")
.arg("--tabs=4")
.arg("--style=plain")
.assert()
.success()
.stdout(
"├──┤1├─┤2├─┤3├─┤4␊
1├─┤?␊
22├┤?␊
333↹?␊
4444├──┤?␊
55555├─┤?␊
666666├┤?␊
7777777↹?␊
88888888├──┤?␊
",
);
}

#[test]
fn show_all_extends_tab_markers_to_next_tabstop_width_8() {
bat()
.arg("tabs.txt")
.arg("--show-all")
.arg("--tabs=8")
.arg("--style=plain")
.assert()
.success()
.stdout(
"├──────┤1├─────┤2├─────┤3├─────┤4␊
1├─────┤?␊
22├────┤?␊
333├───┤?␊
4444├──┤?␊
55555├─┤?␊
666666├┤?␊
7777777↹?␊
88888888├──────┤?␊
",
);
}

#[test]
fn no_paging_arg() {
bat()
Expand Down