Skip to content

Commit 6cb63d4

Browse files
committed
fix: search should ignore headers and line numbers
1 parent 018a482 commit 6cb63d4

File tree

4 files changed

+108
-28
lines changed

4 files changed

+108
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
- Fix arithmetic overflow in `LineRange::from` and `LineRange::parse_range`, see #2674, #2698 (@skoriop)
7272
- Fix paging not happening when stdout is interactive but stdin is not, see #2574 (@Nigecat)
7373
- Make `-pp` override `--paging` and vice versa when passed as a later argument, see #2660 (@J-Kappes)
74+
- Fix search features not ignore file name, file size and line numbers when using `less` #2675 (@tdtrung17693)
7475

7576
## Other
7677

src/controller.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ impl<'b> Controller<'b> {
4949
output_buffer: Option<&mut dyn std::fmt::Write>,
5050
mut handle_error: impl FnMut(&Error, &mut dyn Write),
5151
) -> Result<bool> {
52+
let panel_width = if self.config.loop_through {
53+
0
54+
} else {
55+
InteractivePrinter::get_panel_width(self.config, self.assets)
56+
};
5257
let mut output_type;
5358

5459
#[cfg(feature = "paging")]
@@ -73,7 +78,8 @@ impl<'b> Controller<'b> {
7378

7479
let wrapping_mode = self.config.wrapping_mode;
7580

76-
output_type = OutputType::from_mode(paging_mode, wrapping_mode, self.config.pager)?;
81+
output_type =
82+
OutputType::from_mode(paging_mode, wrapping_mode, self.config, panel_width)?;
7783
}
7884

7985
#[cfg(not(feature = "paging"))]
@@ -92,6 +98,7 @@ impl<'b> Controller<'b> {
9298
Some(buf) => OutputHandle::FmtWrite(buf),
9399
None => OutputHandle::IoWrite(output_type.handle()?),
94100
};
101+
95102
let mut no_errors: bool = true;
96103
let stderr = io::stderr();
97104

@@ -144,6 +151,7 @@ impl<'b> Controller<'b> {
144151
#[cfg(not(feature = "lessopen"))]
145152
input.open(stdin, stdout_identifier)?
146153
};
154+
147155
#[cfg(feature = "git")]
148156
let line_changes = if self.config.visible_lines.diff_mode()
149157
|| (!self.config.loop_through && self.config.style_components.changes())

src/output.rs

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::paging::PagingMode;
1010
#[cfg(feature = "paging")]
1111
use crate::wrapping::WrappingMode;
1212

13+
use crate::config::Config;
1314
#[cfg(feature = "paging")]
1415
#[derive(Debug, PartialEq)]
1516
enum SingleScreenAction {
@@ -29,13 +30,19 @@ impl OutputType {
2930
pub fn from_mode(
3031
paging_mode: PagingMode,
3132
wrapping_mode: WrappingMode,
32-
pager: Option<&str>,
33+
config: &Config,
34+
panel_width: usize,
3335
) -> Result<Self> {
3436
use self::PagingMode::*;
3537
Ok(match paging_mode {
36-
Always => OutputType::try_pager(SingleScreenAction::Nothing, wrapping_mode, pager)?,
38+
Always => OutputType::try_pager(
39+
SingleScreenAction::Nothing,
40+
wrapping_mode,
41+
config,
42+
panel_width,
43+
)?,
3744
QuitIfOneScreen => {
38-
OutputType::try_pager(SingleScreenAction::Quit, wrapping_mode, pager)?
45+
OutputType::try_pager(SingleScreenAction::Quit, wrapping_mode, config, panel_width)?
3946
}
4047
_ => OutputType::stdout(),
4148
})
@@ -46,11 +53,13 @@ impl OutputType {
4653
fn try_pager(
4754
single_screen_action: SingleScreenAction,
4855
wrapping_mode: WrappingMode,
49-
pager_from_config: Option<&str>,
56+
config: &Config,
57+
panel_width: usize,
5058
) -> Result<Self> {
5159
use crate::pager::{self, PagerKind, PagerSource};
5260
use std::process::{Command, Stdio};
5361

62+
let pager_from_config = config.pager;
5463
let pager_opt =
5564
pager::get_pager(pager_from_config).map_err(|_| "Could not parse pager command.")?;
5665

@@ -81,6 +90,11 @@ impl OutputType {
8190
// We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
8291
// or bats '--pager' command line option.
8392
let replace_arguments_to_less = pager.source == PagerSource::EnvVarPager;
93+
let less_version = match retrieve_less_version(&pager.bin) {
94+
None => 1,
95+
Some(LessVersion::Less(version)) => version,
96+
_ => 0,
97+
};
8498

8599
if args.is_empty() || replace_arguments_to_less {
86100
p.arg("-R"); // Short version of --RAW-CONTROL-CHARS for maximum compatibility
@@ -99,22 +113,57 @@ impl OutputType {
99113
//
100114
// For newer versions (530 or 558 on Windows), we omit '--no-init' as it
101115
// is not needed anymore.
102-
match retrieve_less_version(&pager.bin) {
103-
None => {
104-
p.arg("--no-init");
105-
}
106-
Some(LessVersion::Less(version))
107-
if (version < 530 || (cfg!(windows) && version < 558)) =>
108-
{
109-
p.arg("--no-init");
110-
}
111-
_ => {}
116+
if less_version == 1
117+
|| (less_version > 1
118+
&& (less_version < 530 || (cfg!(windows) && less_version < 558)))
119+
{
120+
p.arg("--no-init");
112121
}
113122
} else {
114123
p.args(args);
115124
}
125+
116126
p.env("LESSCHARSET", "UTF-8");
117127

128+
if less_version >= 600 {
129+
let mut row_header = 0;
130+
let mut col_header = 0;
131+
let have_grid = config.style_components.grid();
132+
let have_numbers = config.style_components.numbers();
133+
let have_header_filename = config.style_components.header_filename();
134+
let have_header_filesize = config.style_components.header_filesize();
135+
136+
if have_grid {
137+
// for top line
138+
row_header += 1;
139+
}
140+
141+
if have_header_filename && have_header_filesize {
142+
// 2 headers
143+
row_header += 2;
144+
if have_grid {
145+
// for bottom line
146+
row_header += 1;
147+
}
148+
} else if have_header_filesize || have_header_filename {
149+
row_header += 1;
150+
if have_grid {
151+
// for bottom line
152+
row_header += 1;
153+
}
154+
}
155+
156+
if have_numbers && panel_width > 0 {
157+
col_header += panel_width;
158+
}
159+
160+
if row_header > 0 || col_header > 0 {
161+
let header_args = format!("{row_header},{col_header}");
162+
p.args(vec!["--header", &header_args]);
163+
p.arg("--no-search-headers");
164+
}
165+
}
166+
118167
#[cfg(feature = "lessopen")]
119168
// Ensures that 'less' does not preprocess input again if '$LESSOPEN' is set.
120169
p.arg("--no-lessopen");

src/printer.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,7 @@ impl<'a> InteractivePrinter<'a> {
227227
};
228228

229229
// Create decorations.
230-
let mut decorations: Vec<Box<dyn Decoration>> = Vec::new();
231-
232-
if config.style_components.numbers() {
233-
decorations.push(Box::new(LineNumberDecoration::new(&colors)));
234-
}
235-
236-
#[cfg(feature = "git")]
237-
{
238-
if config.style_components.changes() {
239-
decorations.push(Box::new(LineChangesDecoration::new(&colors)));
240-
}
241-
}
242-
230+
let mut decorations: Vec<Box<dyn Decoration>> = Self::get_decorations(config, assets);
243231
let mut panel_width: usize =
244232
decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
245233

@@ -296,6 +284,40 @@ impl<'a> InteractivePrinter<'a> {
296284
})
297285
}
298286

287+
fn get_decorations(
288+
config: &'a Config,
289+
assets: &'a HighlightingAssets,
290+
) -> Vec<Box<dyn Decoration>> {
291+
let theme = assets.get_theme(&config.theme);
292+
let colors = if config.colored_output {
293+
Colors::colored(theme, config.true_color)
294+
} else {
295+
Colors::plain()
296+
};
297+
298+
// Create decorations.
299+
let mut decorations: Vec<Box<dyn Decoration>> = Vec::new();
300+
301+
if config.style_components.numbers() {
302+
decorations.push(Box::new(LineNumberDecoration::new(&colors)));
303+
}
304+
305+
#[cfg(feature = "git")]
306+
{
307+
if config.style_components.changes() {
308+
decorations.push(Box::new(LineChangesDecoration::new(&colors)));
309+
}
310+
}
311+
312+
return decorations;
313+
}
314+
315+
pub(crate) fn get_panel_width(config: &'a Config, assets: &'a HighlightingAssets) -> usize {
316+
let decorations = Self::get_decorations(config, assets);
317+
318+
return decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
319+
}
320+
299321
fn print_horizontal_line_term(
300322
&mut self,
301323
handle: &mut OutputHandle,

0 commit comments

Comments
 (0)