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

Add context-around-highlight option #2780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions src/line_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down