Skip to content

Commit

Permalink
Add Pager helper with info about where the value comes from
Browse files Browse the repository at this point in the history
In preparation of fixing issue sharkdp#1063.
This is a pure refactoring with no intended functional side effects.
  • Loading branch information
Enselic committed Nov 27, 2020
1 parent 6d98149 commit f420236
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ mod less;
pub mod line_range;
mod output;
#[cfg(feature = "paging")]
mod pager;
#[cfg(feature = "paging")]
pub(crate) mod paging;
mod preprocessor;
mod pretty_printer;
Expand Down
37 changes: 10 additions & 27 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,12 @@ impl OutputType {
wrapping_mode: WrappingMode,
pager_from_config: Option<&str>,
) -> Result<Self> {
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use crate::pager::*;

let mut replace_arguments_to_less = false;

let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
(Ok(bat_pager), _) => Some(bat_pager),
(_, Ok(pager)) => {
// less needs to be called with the '-R' option in order to properly interpret the
// ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
// therefore need to overwrite the arguments and add '-R'.
//
// We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
// or bats '--pager' command line option.
replace_arguments_to_less = true;
Some(pager)
}
_ => None,
};

let pager_from_config = pager_from_config.map(|p| p.to_string());

if pager_from_config.is_some() {
replace_arguments_to_less = false;
}

let pager = pager_from_config
.or(pager_from_env)
.unwrap_or_else(|| String::from("less"));
let Pager { pager, source } = get_pager(pager_from_config);

let pagerflags =
shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
Expand All @@ -94,6 +69,14 @@ impl OutputType {
let is_less = pager_path.file_stem() == Some(&OsString::from("less"));

let mut process = if is_less {
// less needs to be called with the '-R' option in order to properly interpret the
// ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
// therefore need to overwrite the arguments and add '-R'.
//
// 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 = source == PagerSource::PagerEnvVar;

let mut p = Command::new(&pager_path);
if args.is_empty() || replace_arguments_to_less {
p.arg("--RAW-CONTROL-CHARS");
Expand Down
45 changes: 45 additions & 0 deletions src/pager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#[derive(Debug, PartialEq)]
pub enum PagerSource {
/// From the env var BAT_PAGER
BatPagerEnvVar,

/// From the env var PAGER
PagerEnvVar,

/// From --config
Config,

/// No pager was specified, default is used
Default,
}

pub struct Pager {
pub pager: String,
pub source: PagerSource,
}

impl Pager {
fn new(
pager: &str,
source: PagerSource
) -> Pager {
Pager {
pager: String::from(pager),
source,
}
}
}

pub fn get_pager(
pager_from_config: Option<&str>,
) -> Pager {
if pager_from_config.is_some() {
return Pager::new(pager_from_config.unwrap(), PagerSource::Config);
} else {
return match (std::env::var("BAT_PAGER"), std::env::var("PAGER")) {
(Ok(bat_pager), _) => Pager::new(&bat_pager, PagerSource::BatPagerEnvVar),
(_, Ok(pager)) => Pager::new(&pager, PagerSource::PagerEnvVar),
_ => Pager::new("less", PagerSource::Default),
};
}
}

0 comments on commit f420236

Please sign in to comment.