Repository: sharkdp/bat
File: src/output.rs (Rust)

Code before:

pub enum OutputType {
    Pager(Child),
    Stdout(io::Stdout),
}

impl OutputType {
    pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
        use self::PagingMode::*;
        Ok(match mode {
            Always => OutputType::try_pager(false, pager)?,
            QuitIfOneScreen => OutputType::try_pager(true, pager)?,
            _ => OutputType::stdout(),
        })
    }

    /// Try to launch the pager. Fall back to stdout in case of errors.
    fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
        let pager_from_env = env::var("BAT_PAGER").or_else(|_| env::var("PAGER"));

        let mut pager = pager_from_config
            .map(|p| p.to_string())
            .or(pager_from_env.ok())
            .unwrap_or(String::from("less"));

        if pager == "bat" {
            pager = String::from("less");
        }

        let pagerflags = shell_words::split(&pager)
            .chain_err(|| "Could not parse (BAT_)PAGER environment variable.")?;

        match pagerflags.split_first() {
            Some((pager_name, args)) => {
                let pager_path = PathBuf::from(pager_name);
                let is_less = pager_path.file_stem() == Some(&OsString::from("less"));

                let mut process = if is_less {
                    let mut p = Command::new(&pager_path);
                    if args.is_empty() {
                        p.args(vec!["--RAW-CONTROL-CHARS", "--no-init"]);
                        if quit_if_one_screen {
                            p.arg("--quit-if-one-screen");
                        }
                    }
                    p.env("LESSCHARSET", "UTF-8");
                    p
                } else {
                    Command::new(&pager_path)
                };


Review comment (suggestion):
I think a better place to check for this would be a few lines below. See how we check for `less` in the `let is_less = ..` line. This also has the benefit of detecting things like `/usr/bin/bat`.

Code after:

pub enum OutputType {
    Pager(Child),
    Stdout(io::Stdout),
}

impl OutputType {
    pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
        use self::PagingMode::*;
        Ok(match mode {
            Always => OutputType::try_pager(false, pager)?,
            QuitIfOneScreen => OutputType::try_pager(true, pager)?,
            _ => OutputType::stdout(),
        })
    }

    /// Try to launch the pager. Fall back to stdout in case of errors.
    fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
        let pager_from_env = env::var("BAT_PAGER").or_else(|_| env::var("PAGER"));

        let pager = pager_from_config
            .map(|p| p.to_string())
            .or(pager_from_env.ok())
            .unwrap_or(String::from("less"));

        let pagerflags = shell_words::split(&pager)
            .chain_err(|| "Could not parse (BAT_)PAGER environment variable.")?;

        match pagerflags.split_first() {
            Some((initial_pager, mut args)) => {
                let pager_name;
                if initial_pager == "bat" {
                    pager_name = "less";
                    args = &[];
                } else {
                    pager_name = initial_pager;
                }

                let pager_path = PathBuf::from(pager_name);
                let is_less = pager_path.file_stem() == Some(&OsString::from("less"));

                let mut process = if is_less {
                    let mut p = Command::new(&pager_path);
                    if args.is_empty() {
                        p.args(vec!["--RAW-CONTROL-CHARS", "--no-init"]);
                        if quit_if_one_screen {
                            p.arg("--quit-if-one-screen");
                        }
                    }
                    p.env("LESSCHARSET", "UTF-8");
                    p