Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not parse arguments in PAGER #454

Merged
merged 2 commits into from
Feb 7, 2019
Merged

Conversation

majecty
Copy link
Contributor

@majecty majecty commented Dec 17, 2018

This fixes #430

@majecty
Copy link
Contributor Author

majecty commented Dec 17, 2018

@sharkdp I am struggling with fixing the test case pager_basic.
Could you give me some hints?

fn pager_basic() {

@majecty
Copy link
Contributor Author

majecty commented Dec 17, 2018

I tried by creating pager_output.sh file, but the windows test failed. :(

@sharkdp
Copy link
Owner

sharkdp commented Dec 18, 2018

Thank you very much for looking into this!

Maybe we could do the following (this would also fix the echo-based tests, I think):

  • Split the PAGER environment variable into arguments with shell_words::split.
  • Check if the (basename of) the first component is less
  • If yes, call the given version of less with our fixed set of arguments (-FRX).
  • If no, call the pager as specified with all of its arguments.

This way, if someone wants to use my_special_pager with a given set of command-line arguments, it can still be done via PAGER.

@majecty
Copy link
Contributor Author

majecty commented Dec 19, 2018

Thanks. I'll try it.

src/output.rs Outdated
@@ -26,7 +26,8 @@ impl OutputType {

/// 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_from_env =
env::var("BAT_PAGER").or_else(|_| env::var("PAGER").map(add_default_flags_to_less));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole point of this was to have a different handling of PAGER and BAT_PAGER. We do not want to modify anything for BAT_PAGER.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env::var("BAT_PAGER") is not modified. Only env::var("pager") is mapped by the function add_default_flags_to_less

If I am misunderstanding your comment, please say freely about it.

src/output.rs Outdated
// BAT_PAGER.
fn add_default_flags_to_less(pager: String) -> String {
if let Ok(flags) = shell_words::split(&pager) {
if flags.first() == Some(&String::from("less")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work if the first argument is /usr/bin/less, for example. See the is_less variable example in the same file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I missed it. I'll fix it now!

@majecty
Copy link
Contributor Author

majecty commented Dec 31, 2018

When I run the same test in my repository, the test succeeded. I don't know why the test failed. Could you help me?
@sharkdp

@majecty
Copy link
Contributor Author

majecty commented Jan 4, 2019

@sharkdp Thanks! the test passed. Could you review this PR again?

@sharkdp
Copy link
Owner

sharkdp commented Jan 9, 2019

@sharkdp Thanks! the test passed. Could you review this PR again?

Yes, sorry for the delay. I'll do a final review soon.

@sharkdp
Copy link
Owner

sharkdp commented Jan 13, 2019

I don't really like the duplication of the logic here. We already have a piece of code that adds the default arguments in a specific way:

bat/src/output.rs

Lines 52 to 57 in abf0229

if args.is_empty() {
p.args(vec!["--RAW-CONTROL-CHARS", "--no-init"]);
if quit_if_one_screen {
p.arg("--quit-if-one-screen");
}
}

We should strive to reuse that part of the code.

@majecty
Copy link
Contributor Author

majecty commented Jan 23, 2019

@sharkdp Sorry for late.

Then how about this?

        let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
            (Ok(bat_pager), _) => Some(bat_pager),
            (_, Ok(pager)) => {
                if PathBuf::from(pager.clone()).file_stem() == Some(&OsString::from("less")) {
                    Some(String::from("less"))
                } else {
                    Some(pager)
                }
            }
            _ => None,
        };

If the pager_from_env value is set to "less", the default arguments will be added later.

@sharkdp
Copy link
Owner

sharkdp commented Feb 7, 2019

Then how about this?

This wouldn't work if PAGER="less -F", for example, because we need to split off the argument first, before checking the file_stem. I have added an implementation which should hopefully work. It is an adapted version of your latest proposal.

In any case, thank you very much for your work and your patience.

@sharkdp sharkdp merged commit 6e8fca5 into sharkdp:master Feb 7, 2019
@majecty majecty deleted the f/parse_pager branch February 19, 2019 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do *not* parse arguments in PAGER
2 participants