summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2020-04-22 19:15:33 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2020-04-22 23:55:28 +0200
commitec0ce054559e3821ddfcbbd0fa60b0b30117b6fa (patch)
treea26fb2e2836b0e44890ff1d1cb9c1ecd960d0350
parent13e6b3fac7440a487f4c5952345f20ac681b571f (diff)
Better API for highlighting lines
-rw-r--r--src/bin/bat/app.rs4
-rw-r--r--src/lib.rs4
-rw-r--r--src/line_range.rs11
-rw-r--r--src/pretty_printer.rs23
4 files changed, 31 insertions, 11 deletions
diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs
index a290b7f0..f93c1310 100644
--- a/src/bin/bat/app.rs
+++ b/src/bin/bat/app.rs
@@ -18,8 +18,8 @@ use bat::{
config::{Config, PagingMode},
errors::*,
input::Input,
- HighlightedLineRanges, LineRange, LineRanges, MappingTarget, StyleComponent, StyleComponents,
- SyntaxMapping, WrappingMode,
+ line_range::{HighlightedLineRanges, LineRange, LineRanges},
+ MappingTarget, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
};
fn is_truecolor_terminal() -> bool {
diff --git a/src/lib.rs b/src/lib.rs
index 8997564b..0e5ff234 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,7 +19,7 @@ mod diff;
pub mod errors;
pub mod input;
mod less;
-pub(crate) mod line_range;
+pub mod line_range;
mod output;
mod preprocessor;
mod pretty_printer;
@@ -29,7 +29,7 @@ pub(crate) mod syntax_mapping;
mod terminal;
pub(crate) mod wrap;
-pub use line_range::{HighlightedLineRanges, LineRange, LineRanges};
+pub use line_range::LineRange;
pub use pretty_printer::PrettyPrinter;
pub use style::{StyleComponent, StyleComponents};
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
diff --git a/src/line_range.rs b/src/line_range.rs
index b7cfee5f..0abbb7ac 100644
--- a/src/line_range.rs
+++ b/src/line_range.rs
@@ -2,8 +2,8 @@ use crate::errors::*;
#[derive(Debug, Clone)]
pub struct LineRange {
- pub lower: usize,
- pub upper: usize,
+ lower: usize,
+ upper: usize,
}
impl Default for LineRange {
@@ -16,6 +16,13 @@ impl Default for LineRange {
}
impl LineRange {
+ pub fn new(from: usize, to: usize) -> Self {
+ LineRange {
+ lower: from,
+ upper: to,
+ }
+ }
+
pub fn from(range_raw: &str) -> Result<LineRange> {
LineRange::parse_range(range_raw)
}
diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs
index f4ac6605..67a4ee59 100644
--- a/src/pretty_printer.rs
+++ b/src/pretty_printer.rs
@@ -2,8 +2,13 @@ use std::ffi::OsStr;
use std::io::Read;
use crate::{
- assets::HighlightingAssets, config::Config, controller::Controller, errors::Result,
- input::Input, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode,
+ assets::HighlightingAssets,
+ config::Config,
+ controller::Controller,
+ errors::Result,
+ input::Input,
+ line_range::{HighlightedLineRanges, LineRanges},
+ LineRange, StyleComponents, SyntaxMapping, WrappingMode,
};
#[cfg(feature = "paging")]
@@ -13,6 +18,8 @@ pub struct PrettyPrinter<'a> {
inputs: Vec<Input<'a>>,
config: Config<'a>,
assets: HighlightingAssets,
+
+ highlighted_lines: Vec<LineRange>,
}
impl<'a> PrettyPrinter<'a> {
@@ -26,6 +33,7 @@ impl<'a> PrettyPrinter<'a> {
inputs: vec![],
config,
assets: HighlightingAssets::from_binary(),
+ highlighted_lines: vec![],
}
}
@@ -132,9 +140,11 @@ impl<'a> PrettyPrinter<'a> {
self
}
- /// Specify which lines should be highlighted (default: none)
- pub fn highlighted_lines(&mut self, ranges: HighlightedLineRanges) -> &mut Self {
- self.config.highlighted_lines = ranges;
+ /// 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);
self
}
@@ -154,6 +164,9 @@ impl<'a> PrettyPrinter<'a> {
/// If you want to call 'run' multiple times, you have to call the appropriate
/// input_* methods again.
pub fn run(&mut self) -> Result<bool> {
+ self.config.highlighted_lines =
+ HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone()));
+
let mut inputs: Vec<Input> = vec![];
std::mem::swap(&mut inputs, &mut self.inputs);