on master help sign in
Adds range syntax for line highlights
commit93881d9a6402631855f061adb7ca19d681c7d578 authorLiam Kalir <liamkalir9595@gmail.com> dateWed 22 Jan 2020, 22:26 (7 years ago) parent87776ae
5 files changed+40−23
src/bin/bat/app.rs+7−5 src/bin/bat/clap_app.rs+10−4 src/lib.rs+1−1 src/line_range.rs+19−8 src/printer.rs+3−5
src/bin/bat/app.rs+7−5
@@ -215,11 +215,13 @@ impl App {
215215 Some("always") => true,
216216 _ => false,
217217 },
218− highlight_lines: self
219− .matches
220− .values_of("highlight-line")
221− .and_then(|ws| ws.map(|w| w.parse().ok()).collect())
222− .unwrap_or_default(),
218+ highlight_lines: LineRanges::from(
219+ self.matches
220+ .values_of("highlight-line")
221+ .map(|ws| ws.map(LineRange::from).collect())
222+ .transpose()?
223+ .unwrap_or_else(|| vec![LineRange { lower: 0, upper: 0 }]),
224+ ),
223225 })
224226 }
225227
src/bin/bat/clap_app.rs+10−4
@@ -82,10 +82,15 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
8282 .takes_value(true)
8383 .number_of_values(1)
8484 .multiple(true)
85− .value_name("N")
86− .help("Highlight the given line.")
85+ .value_name("N:M")
86+ .help("Highlight lines N through M.")
8787 .long_help(
88− "Highlight the N-th line with a different background color",
88+ "Highlight the specified line ranges with a different background color \
89+ For example:\n \
90+ '--highlight-line 30:40' highlights lines 30 to 40\n \
91+ '--highlight-line :40' highlights lines 1 to 40\n \
92+ '--highlight-line 40:' highlights lines 40 to the end of the file\n \
93+ '--highlight-line 40' highlights only line 40",
8994 ),
9095 )
9196 .arg(
@@ -328,7 +333,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
328333 For example:\n \
329334 '--line-range 30:40' prints lines 30 to 40\n \
330335 '--line-range :40' prints lines 1 to 40\n \
331− '--line-range 40:' prints lines 40 to the end of the file",
336+ '--line-range 40:' prints lines 40 to the end of the file\n \
337+ '--line-range 40' prints only line 40",
332338 ),
333339 )
334340 .arg(
src/lib.rs+1−1
@@ -128,5 +128,5 @@ pub struct Config<'a> {
128128 pub use_italic_text: bool,
129129
130130 /// Lines to highlight
131− pub highlight_lines: Vec<usize>,
131+ pub highlight_lines: LineRanges,
132132}
src/line_range.rs+19−8
@@ -30,13 +30,19 @@ impl LineRange {
3030 }
3131
3232 let line_numbers: Vec<&str> = range_raw.split(':').collect();
33− if line_numbers.len() == 2 {
34− new_range.lower = line_numbers[0].parse()?;
35− new_range.upper = line_numbers[1].parse()?;
36− return Ok(new_range);
33+ match line_numbers.len() {
34+ 1 => {
35+ new_range.lower = line_numbers[0].parse()?;
36+ new_range.upper = new_range.lower;
37+ Ok(new_range)
38+ },
39+ 2 => {
40+ new_range.lower = line_numbers[0].parse()?;
41+ new_range.upper = line_numbers[1].parse()?;
42+ Ok(new_range)
43+ },
44+ _ => Err("expected at most single ':' character".into()),
3745 }
38−
39− Err("expected single ':' character".into())
4046 }
4147
4248 pub fn is_inside(&self, line: usize) -> bool {
@@ -65,6 +71,13 @@ fn test_parse_partial_max() {
6571 assert_eq!(usize::max_value(), range.upper);
6672}
6773
74+#[test]
75+fn test_parse_single() {
76+ let range = LineRange::from("40").expect("Shouldn't fail on test!");
77+ assert_eq!(40, range.lower);
78+ assert_eq!(40, range.upper);
79+}
80+
6881#[test]
6982fn test_parse_fail() {
7083 let range = LineRange::from("40:50:80");
@@ -73,8 +86,6 @@ fn test_parse_fail() {
7386 assert!(range.is_err());
7487 let range = LineRange::from(":40:");
7588 assert!(range.is_err());
76− let range = LineRange::from("40");
77− assert!(range.is_err());
7889}
7990
8091#[derive(Copy, Clone, Debug, PartialEq)]
src/printer.rs+3−5
@@ -24,6 +24,7 @@ use crate::diff::get_git_diff;
2424use crate::diff::LineChanges;
2525use crate::errors::*;
2626use crate::inputfile::{InputFile, InputFileReader};
27+use crate::line_range::RangeCheckResult;
2728use crate::preprocessor::{expand_tabs, replace_nonprintable};
2829use crate::style::OutputWrap;
2930use crate::terminal::{as_terminal_escaped, to_ansi_color};
@@ -369,11 +370,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
369370 let mut panel_wrap: Option<String> = None;
370371
371372 // Line highlighting
372− let highlight_this_line = self
373− .config
374− .highlight_lines
375− .iter()
376− .any(|&l| l == line_number);
373+ let highlight_this_line =
374+ self.config.highlight_lines.check(line_number) == RangeCheckResult::InRange;
377375
378376 let background_color = self
379377 .background_color_highlight