mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-25 01:03:46 +01:00
Add context-around-highlight
This commit is contained in:
parent
7658334645
commit
0749f8c3d2
@ -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,
|
||||
|
@ -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…
Reference in New Issue
Block a user