Skip to content

Commit

Permalink
fix: cargo fmt + check less version
Browse files Browse the repository at this point in the history
  • Loading branch information
tdtrung17693 committed Oct 1, 2023
1 parent e8f6862 commit 2f00d17
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 58 deletions.
21 changes: 6 additions & 15 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ impl<'b> Controller<'b> {
let panel_width = if self.config.loop_through {
0
} else {
InteractivePrinter::get_panel_width(
self.config,
self.assets,
)
InteractivePrinter::get_panel_width(self.config, self.assets)
};
let mut output_type;

Expand All @@ -81,7 +78,8 @@ impl<'b> Controller<'b> {

let wrapping_mode = self.config.wrapping_mode;

output_type = OutputType::from_mode(paging_mode, wrapping_mode, self.config, panel_width)?;
output_type =
OutputType::from_mode(paging_mode, wrapping_mode, self.config, panel_width)?;
}

#[cfg(not(feature = "paging"))]
Expand All @@ -97,18 +95,13 @@ impl<'b> Controller<'b> {
};

let mut writer = match output_buffer {
Some(buf) => {
OutputHandle::FmtWrite(buf)
},
None => {
OutputHandle::IoWrite(output_type.handle()?)
},
Some(buf) => OutputHandle::FmtWrite(buf),
None => OutputHandle::IoWrite(output_type.handle()?),
};

let mut no_errors: bool = true;
let stderr = io::stderr();


for (index, input) in inputs.into_iter().enumerate() {
let identifier = stdout_identifier.as_ref();
let is_first = index == 0;
Expand Down Expand Up @@ -152,9 +145,7 @@ impl<'b> Controller<'b> {
Some(ref preprocessor) if self.config.use_lessopen => {
preprocessor.open(input, stdin, stdout_identifier)?
}
_ => {
input.open(stdin, stdout_identifier)?
}
_ => input.open(stdin, stdout_identifier)?,
}

#[cfg(not(feature = "lessopen"))]
Expand Down
84 changes: 46 additions & 38 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ impl OutputType {
) -> Result<Self> {
use self::PagingMode::*;
Ok(match paging_mode {
Always => OutputType::try_pager(SingleScreenAction::Nothing, wrapping_mode, config, panel_width)?,
Always => OutputType::try_pager(
SingleScreenAction::Nothing,
wrapping_mode,
config,
panel_width,
)?,
QuitIfOneScreen => {
OutputType::try_pager(SingleScreenAction::Quit, wrapping_mode, config, panel_width)?
}
Expand Down Expand Up @@ -85,6 +90,11 @@ impl OutputType {
// We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
// or bats '--pager' command line option.
let replace_arguments_to_less = pager.source == PagerSource::EnvVarPager;
let less_version = match retrieve_less_version(&pager.bin) {
None => 1,
Some(LessVersion::Less(version)) => version,
_ => 0,
};

if args.is_empty() || replace_arguments_to_less {
p.arg("-R"); // Short version of --RAW-CONTROL-CHARS for maximum compatibility
Expand All @@ -103,57 +113,55 @@ impl OutputType {
//
// For newer versions (530 or 558 on Windows), we omit '--no-init' as it
// is not needed anymore.
match retrieve_less_version(&pager.bin) {
None => {
p.arg("--no-init");
}
Some(LessVersion::Less(version))
if (version < 530 || (cfg!(windows) && version < 558)) =>
{
p.arg("--no-init");
}
_ => {}
if less_version == 1
|| (less_version > 1
&& (less_version < 530 || (cfg!(windows) && less_version < 558)))
{
p.arg("--no-init");
}
} else {
p.args(args);
}

p.env("LESSCHARSET", "UTF-8");

let mut row_header = 0;
let mut col_header = 0;
let have_grid = config.style_components.grid();
let have_numbers = config.style_components.numbers();
let have_header_filename = config.style_components.header_filename();
let have_header_filesize = config.style_components.header_filesize();
if less_version >= 600 {
let mut row_header = 0;
let mut col_header = 0;
let have_grid = config.style_components.grid();
let have_numbers = config.style_components.numbers();
let have_header_filename = config.style_components.header_filename();
let have_header_filesize = config.style_components.header_filesize();

if have_grid {
// for top line
row_header += 1;
}

if have_header_filename && have_header_filesize {
// 2 headers
row_header += 2;
if have_grid {
// for bottom line
// for top line
row_header += 1;
}
} else if have_header_filesize || have_header_filename {
row_header += 1;
if have_grid {
// for bottom line

if have_header_filename && have_header_filesize {
// 2 headers
row_header += 2;
if have_grid {
// for bottom line
row_header += 1;
}
} else if have_header_filesize || have_header_filename {
row_header += 1;
if have_grid {
// for bottom line
row_header += 1;
}
}
}

if have_numbers && panel_width > 0 {
col_header += panel_width;
}
if have_numbers && panel_width > 0 {
col_header += panel_width;
}

if row_header > 0 || col_header > 0 {
let header_args = format!("{row_header},{col_header}");
p.args(vec!["--header", &header_args]);
p.arg("--no-search-headers");
if row_header > 0 || col_header > 0 {
let header_args = format!("{row_header},{col_header}");
p.args(vec!["--header", &header_args]);
p.arg("--no-search-headers");
}
}

#[cfg(feature = "lessopen")]
Expand Down
7 changes: 2 additions & 5 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl<'a> InteractivePrinter<'a> {
fn get_decorations(
config: &'a Config,
assets: &'a HighlightingAssets,
) -> Vec<Box<dyn Decoration>>{
) -> Vec<Box<dyn Decoration>> {
let theme = assets.get_theme(&config.theme);
let colors = if config.colored_output {
Colors::colored(theme, config.true_color)
Expand All @@ -277,10 +277,7 @@ impl<'a> InteractivePrinter<'a> {
return decorations;
}

pub(crate) fn get_panel_width(
config: &'a Config,
assets: &'a HighlightingAssets,
) -> usize {
pub(crate) fn get_panel_width(config: &'a Config, assets: &'a HighlightingAssets) -> usize {
let decorations = Self::get_decorations(config, assets);

return decorations.len() + decorations.iter().fold(0, |a, x| a + x.width());
Expand Down

0 comments on commit 2f00d17

Please sign in to comment.