pull/2780/merge
Dipesh Kafle 1 month ago committed by GitHub
commit 8e056c2440
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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;
@ -265,14 +266,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,

@ -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")

@ -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

Loading…
Cancel
Save