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

Code before:

impl PagerKind {
    fn from_bin(bin: &str) -> PagerKind {
        use std::path::Path;

        // Set to `less` by default on most Linux distros.
        let pager_bin = Path::new(bin).file_stem();

        // The name of the current running binary. Normally `bat` but sometimes
        // `batcat` for compatibility reasons.
        let current_bin = env::args_os().next();

        // Check if the current running binary is set to be our pager.
        let is_current_bin_pager = current_bin
            .map(|arg0| Path::new(&arg0).file_stem() == pager_bin)
            .unwrap_or(false);

        if is_current_bin_pager {
            PagerKind::Bat
        } else {
            match pager_bin.map(|s| s.to_string_lossy()).as_deref() {
                Some("less") => PagerKind::Less,
                Some("more") => PagerKind::More,
                Some("most") => PagerKind::Most,
                _ => PagerKind::Unknown,
            }
        }
    }
}

/// A pager such as 'less', and from where we got it.
#[derive(Debug)]
pub(crate) struct Pager {
    /// The pager binary
    pub bin: String,

    /// The pager binary arguments (that we might tweak)
    pub args: Vec<String>,

    /// What pager this is
    pub kind: PagerKind,

    /// From where this pager comes
    pub source: PagerSource,
}

impl Pager {
    fn new(bin: &str, args: &[String], kind: PagerKind, source: PagerSource) -> Pager {
        Pager {
            bin: String::from(bin),
            args: args.to_vec(),

Review comment (refactor):
Probably worth simplifying to the following?

```suggestion
        match pager_bin.map(|s| s.to_string_lossy()).as_deref() {
            Some("less") => PagerKind::Less,
            Some("more") => PagerKind::More,
            Some("most") => PagerKind::Most,
            _ if is_current_bin_pager => PagerKind::Bat,
            _ => PagerKind::Unknown,
        }
```

Code after:

impl PagerKind {
    fn from_bin(bin: &str) -> PagerKind {
        use std::path::Path;

        // Set to `less` by default on most Linux distros.
        let pager_bin = Path::new(bin).file_stem();

        // The name of the current running binary. Normally `bat` but sometimes
        // `batcat` for compatibility reasons.
        let current_bin = env::args_os().next();

        // Check if the current running binary is set to be our pager.
        let is_current_bin_pager = current_bin
            .map(|s| Path::new(&s).file_stem() == pager_bin)
            .unwrap_or(false);

        match pager_bin.map(|s| s.to_string_lossy()).as_deref() {
            Some("less") => PagerKind::Less,
            Some("more") => PagerKind::More,
            Some("most") => PagerKind::Most,
            _ if is_current_bin_pager => PagerKind::Bat,
            _ => PagerKind::Unknown,
        }
    }
}

/// A pager such as 'less', and from where we got it.
#[derive(Debug)]
pub(crate) struct Pager {
    /// The pager binary
    pub bin: String,

    /// The pager binary arguments (that we might tweak)
    pub args: Vec<String>,

    /// What pager this is
    pub kind: PagerKind,

    /// From where this pager comes
    pub source: PagerSource,
}

impl Pager {
    fn new(bin: &str, args: &[String], kind: PagerKind, source: PagerSource) -> Pager {
        Pager {
            bin: String::from(bin),
            args: args.to_vec(),
            kind,
            source,
        }