Simpler highlight method

This commit is contained in:
sharkdp 2020-04-22 22:50:57 +02:00 committed by David Peter
parent 261a7ea154
commit 17f3a3b95d
3 changed files with 14 additions and 7 deletions

View file

@ -1,5 +1,5 @@
/// A program that prints its own source code using the bat library
use bat::{LineRange, PagingMode, PrettyPrinter, WrappingMode};
use bat::{PagingMode, PrettyPrinter, WrappingMode};
fn main() {
PrettyPrinter::new()
@ -8,7 +8,7 @@ fn main() {
.line_numbers(true)
.use_italics(true)
// The following line will be highlighted in the output:
.highlight(LineRange::new(line!() as usize, line!() as usize))
.highlight(line!() as usize)
.theme("1337")
.wrapping_mode(WrappingMode::Character)
.paging_mode(PagingMode::QuitIfOneScreen)

View file

@ -37,7 +37,6 @@ pub(crate) mod syntax_mapping;
mod terminal;
pub(crate) mod wrapping;
pub use line_range::LineRange;
pub use pretty_printer::PrettyPrinter;
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
pub use wrapping::WrappingMode;

View file

@ -10,9 +10,9 @@ use crate::{
controller::Controller,
error::Result,
input::Input,
line_range::{HighlightedLineRanges, LineRanges},
line_range::{HighlightedLineRanges, LineRange, LineRanges},
style::{StyleComponent, StyleComponents},
LineRange, SyntaxMapping, WrappingMode,
SyntaxMapping, WrappingMode,
};
#[cfg(feature = "paging")]
@ -209,11 +209,19 @@ impl<'a> PrettyPrinter<'a> {
self
}
/// Specify a line that should be highlighted (default: none).
/// This can be called multiple times to highlight more than one
/// line. See also: highlight_range.
pub fn highlight(&mut self, line: usize) -> &mut Self {
self.highlighted_lines.push(LineRange::new(line, line));
self
}
/// Specify a range of lines that should be highlighted (default: none).
/// This can be called multiple times to highlight more than one range
/// of lines.
pub fn highlight(&mut self, range: LineRange) -> &mut Self {
self.highlighted_lines.push(range);
pub fn highlight_range(&mut self, from: usize, to: usize) -> &mut Self {
self.highlighted_lines.push(LineRange::new(from, to));
self
}