Add context-around-highlight

This commit is contained in:
Dipesh Kafle 2023-11-22 23:36:13 +05:30
parent 7658334645
commit 0749f8c3d2
3 changed files with 50 additions and 5 deletions

View File

@ -7,6 +7,7 @@ use crate::{
clap_app,
config::{get_args_from_config_file, get_args_from_env_opts_var, get_args_from_env_vars},
};
use bat::line_range;
use clap::ArgMatches;
use console::Term;
@ -255,14 +256,42 @@ impl App {
.unwrap_or(2),
),
_ => VisibleLines::Ranges(
self.matches
_ => VisibleLines::Ranges({
let line_range = self
.matches
.get_many::<String>("line-range")
.map(|vs| vs.map(|s| LineRange::from(s.as_str())).collect())
.transpose()?
.map(LineRanges::from)
.unwrap_or_default(),
),
.map(LineRanges::from);
let context = self
.matches
.get_one::<String>("context-around-highlight")
.and_then(|s| usize::from_str_radix(s, 10).ok())
.unwrap_or_else(|| 0);
let range_due_to_highlighted_lines = self
.matches
.get_many::<String>("highlight-line")
.map(|ws| {
ws.map(|s| {
let lr = LineRange::from(s.as_str());
lr.map(|line_range| {
LineRange::new(
line_range.get_lower().saturating_sub(context),
line_range.get_upper().saturating_add(context),
)
})
})
.collect()
})
.transpose()?
.map(LineRanges::from);
match ( line_range, range_due_to_highlighted_lines ) {
( Some(range), None ) | ( None, Some(range) ) => range,
( None, None ) => LineRanges::default(),
_ => unreachable!("line-range and context-around-highlight are conflicting, so this is impossible")
}
}),
},
style_components,
syntax_mapping,

View File

@ -120,6 +120,16 @@ pub fn build_app(interactive_output: bool) -> Command {
'--highlight-line 30:+10' highlights lines 30 to 40",
),
)
.arg(
Arg::new("context-around-highlight")
.long("context-around-highlight")
.action(ArgAction::Set)
.requires("highlight-line")
.conflicts_with("line-range")
.conflicts_with("diff")
.value_name("N")
.help("Show lines from highlighted line - N through highlighted line + N"),
)
.arg(
Arg::new("file-name")
.long("file-name")

View File

@ -77,6 +77,12 @@ impl LineRange {
),
}
}
pub fn get_lower(&self) -> usize {
self.lower
}
pub fn get_upper(&self) -> usize {
self.upper
}
pub(crate) fn is_inside(&self, line: usize) -> bool {
line >= self.lower && line <= self.upper