mirror of
				https://github.com/sharkdp/bat.git
				synced 2025-11-04 09:01:56 +00:00 
			
		
		
		
	Better API for highlighting lines
This commit is contained in:
		@@ -18,8 +18,8 @@ use bat::{
 | 
				
			|||||||
    config::{Config, PagingMode},
 | 
					    config::{Config, PagingMode},
 | 
				
			||||||
    errors::*,
 | 
					    errors::*,
 | 
				
			||||||
    input::Input,
 | 
					    input::Input,
 | 
				
			||||||
    HighlightedLineRanges, LineRange, LineRanges, MappingTarget, StyleComponent, StyleComponents,
 | 
					    line_range::{HighlightedLineRanges, LineRange, LineRanges},
 | 
				
			||||||
    SyntaxMapping, WrappingMode,
 | 
					    MappingTarget, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn is_truecolor_terminal() -> bool {
 | 
					fn is_truecolor_terminal() -> bool {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,7 +19,7 @@ mod diff;
 | 
				
			|||||||
pub mod errors;
 | 
					pub mod errors;
 | 
				
			||||||
pub mod input;
 | 
					pub mod input;
 | 
				
			||||||
mod less;
 | 
					mod less;
 | 
				
			||||||
pub(crate) mod line_range;
 | 
					pub mod line_range;
 | 
				
			||||||
mod output;
 | 
					mod output;
 | 
				
			||||||
mod preprocessor;
 | 
					mod preprocessor;
 | 
				
			||||||
mod pretty_printer;
 | 
					mod pretty_printer;
 | 
				
			||||||
@@ -29,7 +29,7 @@ pub(crate) mod syntax_mapping;
 | 
				
			|||||||
mod terminal;
 | 
					mod terminal;
 | 
				
			||||||
pub(crate) mod wrap;
 | 
					pub(crate) mod wrap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub use line_range::{HighlightedLineRanges, LineRange, LineRanges};
 | 
					pub use line_range::LineRange;
 | 
				
			||||||
pub use pretty_printer::PrettyPrinter;
 | 
					pub use pretty_printer::PrettyPrinter;
 | 
				
			||||||
pub use style::{StyleComponent, StyleComponents};
 | 
					pub use style::{StyleComponent, StyleComponents};
 | 
				
			||||||
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
 | 
					pub use syntax_mapping::{MappingTarget, SyntaxMapping};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,8 +2,8 @@ use crate::errors::*;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Clone)]
 | 
					#[derive(Debug, Clone)]
 | 
				
			||||||
pub struct LineRange {
 | 
					pub struct LineRange {
 | 
				
			||||||
    pub lower: usize,
 | 
					    lower: usize,
 | 
				
			||||||
    pub upper: usize,
 | 
					    upper: usize,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Default for LineRange {
 | 
					impl Default for LineRange {
 | 
				
			||||||
@@ -16,6 +16,13 @@ impl Default for LineRange {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl LineRange {
 | 
					impl LineRange {
 | 
				
			||||||
 | 
					    pub fn new(from: usize, to: usize) -> Self {
 | 
				
			||||||
 | 
					        LineRange {
 | 
				
			||||||
 | 
					            lower: from,
 | 
				
			||||||
 | 
					            upper: to,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn from(range_raw: &str) -> Result<LineRange> {
 | 
					    pub fn from(range_raw: &str) -> Result<LineRange> {
 | 
				
			||||||
        LineRange::parse_range(range_raw)
 | 
					        LineRange::parse_range(range_raw)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,8 +2,13 @@ use std::ffi::OsStr;
 | 
				
			|||||||
use std::io::Read;
 | 
					use std::io::Read;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::{
 | 
					use crate::{
 | 
				
			||||||
    assets::HighlightingAssets, config::Config, controller::Controller, errors::Result,
 | 
					    assets::HighlightingAssets,
 | 
				
			||||||
    input::Input, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode,
 | 
					    config::Config,
 | 
				
			||||||
 | 
					    controller::Controller,
 | 
				
			||||||
 | 
					    errors::Result,
 | 
				
			||||||
 | 
					    input::Input,
 | 
				
			||||||
 | 
					    line_range::{HighlightedLineRanges, LineRanges},
 | 
				
			||||||
 | 
					    LineRange, StyleComponents, SyntaxMapping, WrappingMode,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[cfg(feature = "paging")]
 | 
					#[cfg(feature = "paging")]
 | 
				
			||||||
@@ -13,6 +18,8 @@ pub struct PrettyPrinter<'a> {
 | 
				
			|||||||
    inputs: Vec<Input<'a>>,
 | 
					    inputs: Vec<Input<'a>>,
 | 
				
			||||||
    config: Config<'a>,
 | 
					    config: Config<'a>,
 | 
				
			||||||
    assets: HighlightingAssets,
 | 
					    assets: HighlightingAssets,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    highlighted_lines: Vec<LineRange>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl<'a> PrettyPrinter<'a> {
 | 
					impl<'a> PrettyPrinter<'a> {
 | 
				
			||||||
@@ -26,6 +33,7 @@ impl<'a> PrettyPrinter<'a> {
 | 
				
			|||||||
            inputs: vec![],
 | 
					            inputs: vec![],
 | 
				
			||||||
            config,
 | 
					            config,
 | 
				
			||||||
            assets: HighlightingAssets::from_binary(),
 | 
					            assets: HighlightingAssets::from_binary(),
 | 
				
			||||||
 | 
					            highlighted_lines: vec![],
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -132,9 +140,11 @@ impl<'a> PrettyPrinter<'a> {
 | 
				
			|||||||
        self
 | 
					        self
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Specify which lines should be highlighted (default: none)
 | 
					    /// Specify a range of lines that should be highlighted (default: none).
 | 
				
			||||||
    pub fn highlighted_lines(&mut self, ranges: HighlightedLineRanges) -> &mut Self {
 | 
					    /// This can be called multiple times to highlight more than one range
 | 
				
			||||||
        self.config.highlighted_lines = ranges;
 | 
					    /// of lines.
 | 
				
			||||||
 | 
					    pub fn highlight(&mut self, range: LineRange) -> &mut Self {
 | 
				
			||||||
 | 
					        self.highlighted_lines.push(range);
 | 
				
			||||||
        self
 | 
					        self
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -154,6 +164,9 @@ impl<'a> PrettyPrinter<'a> {
 | 
				
			|||||||
    /// If you want to call 'run' multiple times, you have to call the appropriate
 | 
					    /// If you want to call 'run' multiple times, you have to call the appropriate
 | 
				
			||||||
    /// input_* methods again.
 | 
					    /// input_* methods again.
 | 
				
			||||||
    pub fn run(&mut self) -> Result<bool> {
 | 
					    pub fn run(&mut self) -> Result<bool> {
 | 
				
			||||||
 | 
					        self.config.highlighted_lines =
 | 
				
			||||||
 | 
					            HighlightedLineRanges(LineRanges::from(self.highlighted_lines.clone()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let mut inputs: Vec<Input> = vec![];
 | 
					        let mut inputs: Vec<Input> = vec![];
 | 
				
			||||||
        std::mem::swap(&mut inputs, &mut self.inputs);
 | 
					        std::mem::swap(&mut inputs, &mut self.inputs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user