diff --git a/hledger-lib/Hledger/Utils/IO.hs b/hledger-lib/Hledger/Utils/IO.hs index 72b9cfe27..0a06a0a88 100644 --- a/hledger-lib/Hledger/Utils/IO.hs +++ b/hledger-lib/Hledger/Utils/IO.hs @@ -202,6 +202,10 @@ setupPager = do addR "LESS" addR "MORE" +-- | Check if the --no-pager command line flag was provided at program startup, using unsafePerformIO. +noPagerFlag :: Bool +noPagerFlag = "--no-pager" `elem` progArgs + -- related: Hledger.Cli.DocFiles.runPagerForTopic -- | Display the given text on the terminal, using the user's $PAGER if the text is taller -- than the current terminal and stdout is interactive and TERM is not "dumb"; @@ -214,14 +218,14 @@ pager :: String -> IO () #ifdef mingw32_HOST_OS pager = putStr #else -pager = printOrPage' - -printOrPage' s = do - -- disable pager if TERM=dumb (for Emacs shell users) +pager s = do + -- disable pager when --no-pager is specified + let nopager = noPagerFlag + -- disable pager when TERM=dumb (for Emacs shell users) dumbterm <- (== Just "dumb") <$> lookupEnv "TERM" - -- avoid a pager crash with single-line output, https://github.com/pharpend/pager/issues/2 + -- disable pager with single-line output (https://github.com/pharpend/pager/issues/2) let singleline = not $ '\n' `elem` s - -- avoid a pager crash when PAGER is set to something not in PATH + -- disable pager when PAGER is set to something bad (https://github.com/pharpend/pager/issues/3) mpagervar <- lookupEnv "PAGER" badpager <- case mpagervar of @@ -232,7 +236,7 @@ printOrPage' s = do Just _ -> return False Nothing -> return True - (if dumbterm || singleline || badpager + (if nopager || dumbterm || singleline || badpager then putStr else printOrPage . T.pack) s diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index d79a7cf4c..2aec2914d 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -228,6 +228,7 @@ reportflags = [ ,flagReq ["commodity-style", "c"] (\s opts -> Right $ setopt "commodity-style" s opts) "S" "Override a commodity's display style.\nEg: -c '$1000.' or -c '1.000,00 EUR'" -- This has special support in hledger-lib:colorOption, keep synced + ,flagNone ["no-pager"] (setboolopt "no-pager") "don't use a pager for long output" ,flagReq ["color","colour"] (\s opts -> Right $ setopt "color" s opts) "YN" (unlines ["Use ANSI color codes in text output? Can be" diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 341af0b76..112f72500 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -736,6 +736,9 @@ in `less` (and in its `more` compatibility mode). If you use a different pager, you might need to configure it similarly, to avoid seeing junk on screen. Or you can set the `NO_COLOR` environment variable described below. +You can prevent the use of a pager by using the `--no-pager` flag, +eg in your hledger config file. + Here are some notes about the various output formats. ### Text output