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

Code before:
            // 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 = pager.source == PagerSource::EnvVarPager;

            if args.is_empty() || replace_arguments_to_less {
                // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
                // versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
                //
                // See: http://www.greenwoodsoftware.com/less/news.530.html
                //
                // For newer versions (530 or 558 on Windows), we omit '--no-init' as it
                // is not needed anymore.
                let less_version = retrieve_less_version(&pager.bin);
                match less_version {
                    None => {
                        p.arg("--no-init");
                    }
                    Some(LessVersion::Less(version))
                        if (version < 530 || (cfg!(windows) && version < 558)) =>
                    {
                        p.arg("--no-init");
                    }
                    Some(LessVersion::BusyBox) => {
                        // This makes BusyBox less filter out color escapes
                        p.arg("-R");
                    }
                    _ => {}
                }
                if less_version != Some(LessVersion::BusyBox) {
                    p.arg("--RAW-CONTROL-CHARS");
                    if single_screen_action == SingleScreenAction::Quit {
                        p.arg("--quit-if-one-screen");
                    }

                    if wrapping_mode == WrappingMode::NoWrapping(true) {
                        p.arg("--chop-long-lines");
                    }
                }
            } else {
                p.args(args);
            }
            p.env("LESSCHARSET", "UTF-8");
        } else {
            p.args(args);
        };

        Ok(p.stdin(Stdio::piped())
            .spawn()
            .map(OutputType::Pager)

Review comment (refactor):
Do we want to pass this to BusyBox too? Isn't it better to not touch arguments at all if BuxyBox is detected? Right now the diff makes it a bit hard to quickly see if we behave the same when `less` is detected. Don't be afraid of introducing helper functions to improve structure and readability.

Code after:
            // 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 = pager.source == PagerSource::EnvVarPager;

            if args.is_empty() || replace_arguments_to_less {
                p.arg("-R"); // Short version of --RAW-CONTROL-CHARS for maximum compatibility
                if single_screen_action == SingleScreenAction::Quit {
                    p.arg("-F"); // Short version of --quit-if-one-screen for compatibility
                }

                if wrapping_mode == WrappingMode::NoWrapping(true) {
                    p.arg("-S"); // Short version of --chop-long-lines for compatibility
                }

                // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
                // versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
                //
                // See: http://www.greenwoodsoftware.com/less/news.530.html
                //
                // 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");
                    }
                    _ => {}
                }
            } else {
                p.args(args);
            }
            p.env("LESSCHARSET", "UTF-8");
        } else {
            p.args(args);
        };

        Ok(p.stdin(Stdio::piped())
            .spawn()
            .map(OutputType::Pager)
            .unwrap_or_else(|_| OutputType::stdout()))
    }

    pub(crate) fn stdout() -> Self {
        OutputType::Stdout(io::stdout())
    }