imp📟 add a --no-pager general flag

This commit is contained in:
Simon Michael 2024-10-17 21:47:11 -10:00
parent db748465a8
commit ba786752d1
3 changed files with 15 additions and 7 deletions

View File

@ -202,6 +202,10 @@ setupPager = do
addR "LESS" addR "LESS"
addR "MORE" 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 -- related: Hledger.Cli.DocFiles.runPagerForTopic
-- | Display the given text on the terminal, using the user's $PAGER if the text is taller -- | 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"; -- than the current terminal and stdout is interactive and TERM is not "dumb";
@ -214,14 +218,14 @@ pager :: String -> IO ()
#ifdef mingw32_HOST_OS #ifdef mingw32_HOST_OS
pager = putStr pager = putStr
#else #else
pager = printOrPage' pager s = do
-- disable pager when --no-pager is specified
printOrPage' s = do let nopager = noPagerFlag
-- disable pager if TERM=dumb (for Emacs shell users) -- disable pager when TERM=dumb (for Emacs shell users)
dumbterm <- (== Just "dumb") <$> lookupEnv "TERM" 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 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" mpagervar <- lookupEnv "PAGER"
badpager <- badpager <-
case mpagervar of case mpagervar of
@ -232,7 +236,7 @@ printOrPage' s = do
Just _ -> return False Just _ -> return False
Nothing -> return True Nothing -> return True
(if dumbterm || singleline || badpager (if nopager || dumbterm || singleline || badpager
then putStr then putStr
else printOrPage . T.pack) else printOrPage . T.pack)
s s

View File

@ -228,6 +228,7 @@ reportflags = [
,flagReq ["commodity-style", "c"] (\s opts -> Right $ setopt "commodity-style" s opts) "S" ,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'" "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 -- 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" ,flagReq ["color","colour"] (\s opts -> Right $ setopt "color" s opts) "YN"
(unlines (unlines
["Use ANSI color codes in text output? Can be" ["Use ANSI color codes in text output? Can be"

View File

@ -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. 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. 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. Here are some notes about the various output formats.
### Text output ### Text output