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

Code before:

use app::PagingMode;
use errors::*;

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

impl OutputType {
    pub fn from_mode(mode: PagingMode) -> Self {
        use self::PagingMode::*;
        match mode {
            Always => OutputType::try_pager(false),
            QuitIfOneScreen => OutputType::try_pager(true),
            _ => OutputType::stdout(),
        }
    }

    /// Try to launch the pager. Fall back to stdout in case of errors.
    fn try_pager(quit_if_one_screen: bool) -> Self {
        let pager = env::var("BAT_PAGER")
            .or_else(|_| env::var("PAGER"))
            .unwrap_or(String::from("less"));

        let pagerflags = shell_words::split(&pager).unwrap_or(vec![pager]);

        let less_path = PathBuf::from(&pagerflags[0]);
        let is_less = less_path.file_stem() == Some(&OsString::from("less"));

        let mut process = if is_less {
            let mut p = Command::new(&less_path);
            if pagerflags.len() == 1 {
                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(&less_path)
        };

        process
            .args(&pagerflags[1..])
            .stdin(Stdio::piped())
            .spawn()
            .map(OutputType::Pager)
            .unwrap_or_else(|_| OutputType::stdout())
    }

Review comment (bug):
`split` will fail if there is a parse error, for example:
```
split("less --option='foo")   # closing quote is missing
```

I don't think that `unwrap_or` would help in such a case?

Code after:

use app::PagingMode;
use errors::*;

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

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

    /// Try to launch the pager. Fall back to stdout in case of errors.
    fn try_pager(quit_if_one_screen: bool) -> Result<Self> {
        let pager = env::var("BAT_PAGER")
            .or_else(|_| env::var("PAGER"))
            .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((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 pagerflags.len() == 1 {
                        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)
                };

                Ok(process
                    .args(args)
                    .stdin(Stdio::piped())
                    .spawn()