diff --git a/hledger-lib/Hledger/Utils/IO.hs b/hledger-lib/Hledger/Utils/IO.hs index 63da0da7a..908ec3e03 100644 --- a/hledger-lib/Hledger/Utils/IO.hs +++ b/hledger-lib/Hledger/Utils/IO.hs @@ -19,6 +19,9 @@ module Hledger.Utils.IO ( pprint, pprint', + -- * Viewing with pager + pager, + -- * Command line arguments progArgs, outputFileOption, @@ -57,7 +60,7 @@ import Control.Monad (when) import Data.FileEmbed (makeRelativeToProject, embedStringFile) import Data.List hiding (uncons) import Data.Maybe (isJust) -import Data.Text (Text) +import Data.Text (Text, pack) import qualified Data.Text.IO as T import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Builder as TB @@ -74,6 +77,7 @@ import System.IO (Handle, IOMode (..), hGetEncoding, hSetEncoding, hSetNewlineMode, openFile, stdin, stdout, stderr, universalNewlineMode, utf8_bom) import System.IO.Unsafe (unsafePerformIO) +import System.Pager import Text.Pretty.Simple (CheckColorTty(CheckColorTty), OutputOptions(..), defaultOutputOptionsDarkBg, defaultOutputOptionsNoColor, pShowOpt, pPrintOpt) @@ -114,6 +118,11 @@ pprint' = pPrintOpt CheckColorTty prettyopts' -- "Avoid using pshow, pprint, dbg* in the code below to prevent infinite loops." (?) +-- | 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. +pager :: String -> IO () +pager s = printOrPage $ pack s + -- Command line arguments -- | The command line arguments that were used at program startup. diff --git a/hledger-lib/package.yaml b/hledger-lib/package.yaml index e17ac3156..18c594ad6 100644 --- a/hledger-lib/package.yaml +++ b/hledger-lib/package.yaml @@ -58,6 +58,7 @@ dependencies: - microlens >=0.4 - microlens-th >=0.4 - mtl >=2.2.1 +- pager >=0.1.1.0 - parser-combinators >=0.4.0 - pretty-simple >4 && <5 - regex-tdfa diff --git a/hledger/Hledger/Cli/Commands.hs b/hledger/Hledger/Cli/Commands.hs index d155bc8ca..1a8b8ac67 100644 --- a/hledger/Hledger/Cli/Commands.hs +++ b/hledger/Hledger/Cli/Commands.hs @@ -289,7 +289,7 @@ knownCommands = sort . commandsFromCommandsList . drop 1 $ commandsList progname -- extra addons will be added under Misc. printCommandsList :: String -> [String] -> IO () printCommandsList progversion addonsFound = - putStr . unlines . concatMap adjustline $ + pager . unlines . concatMap adjustline $ commandsList progversion (map ('+':) unknownCommandsFound) where commandsFound = map (' ':) builtinCommandNames ++ map ('+':) addonsFound diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index 95b9dcbeb..23af4c16a 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -140,13 +140,13 @@ main = do isExternalCommand = not (null cmd) && cmd `elem` addons -- probably isBadCommand = not (null rawcmd) && null cmd hasVersion = ("--version" `elem`) - printUsage = putStr $ showModeUsage $ mainmode addons + printUsage = pager $ showModeUsage $ mainmode addons badCommandError = error' ("command "++rawcmd++" is not recognized, run with no command to see a list") >> exitFailure -- PARTIAL: hasHelpFlag args1 = any (`elem` args1) ["-h","--help"] hasManFlag args1 = (`elem` args1) "--man" hasInfoFlag args1 = (`elem` args1) "--info" f `orShowHelp` mode1 - | hasHelpFlag args = putStr $ showModeUsage mode1 + | hasHelpFlag args = pager $ showModeUsage mode1 | hasInfoFlag args = runInfoForTopic "hledger" (headMay $ modeNames mode1) | hasManFlag args = runManForTopic "hledger" (headMay $ modeNames mode1) | otherwise = f