Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds range syntax for line highlights #809

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,13 @@ impl App {
Some("always") => true,
_ => false,
},
highlight_lines: self
.matches
.values_of("highlight-line")
.and_then(|ws| ws.map(|w| w.parse().ok()).collect())
.unwrap_or_default(),
highlight_lines: LineRanges::from(
self.matches
.values_of("highlight-line")
.map(|ws| ws.map(LineRange::from).collect())
.transpose()?
.unwrap_or_else(|| vec![LineRange { lower: 0, upper: 0 }]),
),
})
}

Expand Down
14 changes: 10 additions & 4 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.takes_value(true)
.number_of_values(1)
.multiple(true)
.value_name("N")
.help("Highlight the given line.")
.value_name("N:M")
.help("Highlight lines N through M.")
.long_help(
"Highlight the N-th line with a different background color",
"Highlight the specified line ranges with a different background color \
For example:\n \
'--highlight-line 40' highlights line 40\n \
'--highlight-line 30:40' highlights lines 30 to 40\n \
'--highlight-line :40' highlights lines 1 to 40\n \
'--highlight-line 40:' highlights lines 40 to the end of the file"
),
)
.arg(
Expand Down Expand Up @@ -328,7 +333,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
For example:\n \
'--line-range 30:40' prints lines 30 to 40\n \
'--line-range :40' prints lines 1 to 40\n \
'--line-range 40:' prints lines 40 to the end of the file",
'--line-range 40:' prints lines 40 to the end of the file\n \
'--line-range 40' only prints line 40",
),
)
.arg(
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,5 @@ pub struct Config<'a> {
pub use_italic_text: bool,

/// Lines to highlight
pub highlight_lines: Vec<usize>,
pub highlight_lines: LineRanges,
}
27 changes: 19 additions & 8 deletions src/line_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ impl LineRange {
}

let line_numbers: Vec<&str> = range_raw.split(':').collect();
if line_numbers.len() == 2 {
new_range.lower = line_numbers[0].parse()?;
new_range.upper = line_numbers[1].parse()?;
return Ok(new_range);
match line_numbers.len() {
1 => {
new_range.lower = line_numbers[0].parse()?;
new_range.upper = new_range.lower;
Ok(new_range)
},
2 => {
new_range.lower = line_numbers[0].parse()?;
new_range.upper = line_numbers[1].parse()?;
Ok(new_range)
},
_ => Err("Line range contained more than one ':' character. Expected format: 'N' or 'N:M'".into()),
}

Err("expected single ':' character".into())
}

pub fn is_inside(&self, line: usize) -> bool {
Expand Down Expand Up @@ -65,6 +71,13 @@ fn test_parse_partial_max() {
assert_eq!(usize::max_value(), range.upper);
}

#[test]
fn test_parse_single() {
let range = LineRange::from("40").expect("Shouldn't fail on test!");
assert_eq!(40, range.lower);
assert_eq!(40, range.upper);
}

#[test]
fn test_parse_fail() {
let range = LineRange::from("40:50:80");
Expand All @@ -73,8 +86,6 @@ fn test_parse_fail() {
assert!(range.is_err());
let range = LineRange::from(":40:");
assert!(range.is_err());
let range = LineRange::from("40");
assert!(range.is_err());
}

#[derive(Copy, Clone, Debug, PartialEq)]
Expand Down
8 changes: 3 additions & 5 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::diff::get_git_diff;
use crate::diff::LineChanges;
use crate::errors::*;
use crate::inputfile::{InputFile, InputFileReader};
use crate::line_range::RangeCheckResult;
use crate::preprocessor::{expand_tabs, replace_nonprintable};
use crate::style::OutputWrap;
use crate::terminal::{as_terminal_escaped, to_ansi_color};
Expand Down Expand Up @@ -369,11 +370,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
let mut panel_wrap: Option<String> = None;

// Line highlighting
let highlight_this_line = self
.config
.highlight_lines
.iter()
.any(|&l| l == line_number);
let highlight_this_line =
self.config.highlight_lines.check(line_number) == RangeCheckResult::InRange;

let background_color = self
.background_color_highlight
Expand Down