on master help sign in
Better API for highlighting lines
commitec0ce054559e3821ddfcbbd0fa60b0b30117b6fa authorsharkdp <davidpeter@web.de> dateWed 22 Apr 2020, 19:15 (7 years ago) parent13e6b3f
4 files changed+31−11
src/bin/bat/app.rs+2−2 src/lib.rs+2−2 src/line_range.rs+9−2 src/pretty_printer.rs+18−5
src/bin/bat/app.rs+2−2
@@ -18,8 +18,8 @@ use bat::{
1818 config::{Config, PagingMode},
1919 errors::*,
2020 input::Input,
21− HighlightedLineRanges, LineRange, LineRanges, MappingTarget, StyleComponent, StyleComponents,
22− SyntaxMapping, WrappingMode,
21+ line_range::{HighlightedLineRanges, LineRange, LineRanges},
22+ MappingTarget, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
2323};
2424
2525fn is_truecolor_terminal() -> bool {
src/lib.rs+2−2
@@ -19,7 +19,7 @@ mod diff;
1919pub mod errors;
2020pub mod input;
2121mod less;
22−pub(crate) mod line_range;
22+pub mod line_range;
2323mod output;
2424mod preprocessor;
2525mod pretty_printer;
@@ -29,7 +29,7 @@ pub(crate) mod syntax_mapping;
2929mod terminal;
3030pub(crate) mod wrap;
3131
32−pub use line_range::{HighlightedLineRanges, LineRange, LineRanges};
32+pub use line_range::LineRange;
3333pub use pretty_printer::PrettyPrinter;
3434pub use style::{StyleComponent, StyleComponents};
3535pub use syntax_mapping::{MappingTarget, SyntaxMapping};
src/line_range.rs+9−2
@@ -2,8 +2,8 @@ use crate::errors::*;
22
33#[derive(Debug, Clone)]
44pub struct LineRange {
5− pub lower: usize,
6− pub upper: usize,
5+ lower: usize,
6+ upper: usize,
77}
88
99impl Default for LineRange {
@@ -16,6 +16,13 @@ impl Default for LineRange {
1616}
1717
1818impl LineRange {
19+ pub fn new(from: usize, to: usize) -> Self {
20+ LineRange {
21+ lower: from,
22+ upper: to,
23+ }
24+ }
25+
1926 pub fn from(range_raw: &str) -> Result<LineRange> {
2027 LineRange::parse_range(range_raw)
2128 }
src/pretty_printer.rs+18−5
@@ -2,8 +2,13 @@ use std::ffi::OsStr;
22use std::io::Read;
33
44use crate::{
5− assets::HighlightingAssets, config::Config, controller::Controller, errors::Result,
6− input::Input, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode,
5+ assets::HighlightingAssets,
6+ config::Config,
7+ controller::Controller,
8+ errors::Result,
9+ input::Input,
10+ line_range::{HighlightedLineRanges, LineRanges},
11+ LineRange, StyleComponents, SyntaxMapping, WrappingMode,
712};
813
914#[cfg(feature = "paging")]
@@ -13,6 +18,8 @@ pub struct PrettyPrinter<'a> {
1318 inputs: Vec<Input<'a>>,
1419 config: Config<'a>,
1520 assets: HighlightingAssets,
21+
22+ highlighted_lines: Vec<LineRange>,
1623}
1724
1825impl<'a> PrettyPrinter<'a> {
@@ -26,6 +33,7 @@ impl<'a> PrettyPrinter<'a> {
2633 inputs: vec![],
2734 config,
2835 assets: HighlightingAssets::from_binary(),
36+ highlighted_lines: vec![],
2937 }
3038 }
3139
@@ -132,9 +140,11 @@ impl<'a> PrettyPrinter<'a> {
132140 self
133141 }
134142
135− /// Specify which lines should be highlighted (default: none)
136− pub fn highlighted_lines(&mut self, ranges: HighlightedLineRanges) -> &mut Self {
137− self.config.highlighted_lines = ranges;
143+ /// Specify a range of lines that should be highlighted (default: none).
144+ /// This can be called multiple times to highlight more than one range
145+ /// of lines.
146+ pub fn highlight(&mut self, range: LineRange) -> &mut Self {
147+ self.highlighted_lines.push(range);
138148 self
139149 }
140150
@@ -154,6 +164,9 @@ impl<'a> PrettyPrinter<'a> {
154164 /// If you want to call 'run' multiple times, you have to call the appropriate
155165 /// input_* methods again.
156166 pub fn run(&mut self) -> Result<bool> {
167+ self.config.highlighted_lines =
168+ HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone()));
169+
157170 let mut inputs: Vec<Input> = vec![];
158171 std::mem::swap(&mut inputs, &mut self.inputs);
159172