Skip to content

Commit d21f1e8

Browse files
authored
Underline highlighted lines in ANSI theme (#1985)
* Underline highlighted lines in ANSI theme * add test for ansi highlight underline, fix underscore in plain
1 parent f6ce5d2 commit d21f1e8

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Features
44

55
- New style component `header-filesize` to show size of the displayed file in the header. See #1988 (@mdibaiee)
6+
- Use underline for line highlighting on ANSI, see #1730 (@mdibaiee)
67

78
## Bugfixes
89

src/printer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
463463
let highlight_this_line =
464464
self.config.highlighted_lines.0.check(line_number) == RangeCheckResult::InRange;
465465

466+
if highlight_this_line && self.config.theme == "ansi" {
467+
self.ansi_style.update("^[4m");
468+
}
469+
466470
let background_color = self
467471
.background_color_highlight
468472
.filter(|_| highlight_this_line);
@@ -649,6 +653,11 @@ impl<'a> Printer for InteractivePrinter<'a> {
649653
writeln!(handle)?;
650654
}
651655

656+
if highlight_this_line && self.config.theme == "ansi" {
657+
self.ansi_style.update("^[24m");
658+
write!(handle, "\x1B[24m")?;
659+
}
660+
652661
Ok(())
653662
}
654663
}

tests/integration_tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,25 @@ fn grid_for_file_without_newline() {
12901290
.stderr("");
12911291
}
12921292

1293+
// For ANSI theme, use underscore as a highlighter
1294+
#[test]
1295+
fn ansi_highlight_underline() {
1296+
bat()
1297+
.arg("--paging=never")
1298+
.arg("--color=never")
1299+
.arg("--terminal-width=80")
1300+
.arg("--wrap=never")
1301+
.arg("--decorations=always")
1302+
.arg("--theme=ansi")
1303+
.arg("--style=plain")
1304+
.arg("--highlight-line=1")
1305+
.write_stdin("Ansi Underscore Test\nAnother Line")
1306+
.assert()
1307+
.success()
1308+
.stdout("\x1B[4mAnsi Underscore Test\n\x1B[24mAnother Line")
1309+
.stderr("");
1310+
}
1311+
12931312
// Ensure that ANSI passthrough is emitted properly for both wrapping and non-wrapping printer.
12941313
#[test]
12951314
fn ansi_passthrough_emit() {

0 commit comments

Comments
 (0)