@@ -10,6 +10,7 @@ use crate::paging::PagingMode;
1010#[ cfg( feature = "paging" ) ]
1111use crate :: wrapping:: WrappingMode ;
1212
13+ use crate :: config:: Config ;
1314#[ cfg( feature = "paging" ) ]
1415#[ derive( Debug , PartialEq ) ]
1516enum 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" ) ;
0 commit comments