mirror of
				https://github.com/sharkdp/bat.git
				synced 2025-11-03 16:41:56 +00:00 
			
		
		
		
	Better API for highlighting lines
This commit is contained in:
		@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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};
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user