From 20e95095090d5febce7deb17cfa6eec0a93e0482 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Mon, 16 Oct 2023 22:55:26 +0200 Subject: paginate output --- app/Main.hs | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'app/Main.hs') diff --git a/app/Main.hs b/app/Main.hs index 4b4213e..553ca38 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -432,10 +432,15 @@ module Main where +import Data.ByteString.Lazy qualified as LB +import Data.Function ((&)) import Data.List (find) import Data.Maybe (catMaybes, fromMaybe) import Data.String qualified as String import Data.Text qualified as T +import Data.Text.Encoding qualified as T +import Data.Text.Lazy qualified as LT +import Data.Text.Lazy.IO qualified as LT import Data.Time.Clock (UTCTime (utctDay)) import History (listIssues) import Issue (Issue (..)) @@ -452,7 +457,6 @@ import Prettyprinter.Render.Terminal qualified as P import Process (sh_) import System.Console.Terminal.Size qualified as Terminal import System.Exit (ExitCode (ExitFailure), exitWith) -import System.IO (stdout) import System.Process.Typed qualified as P import Text.Printf import TreeGrepper.Match qualified as G @@ -652,16 +656,25 @@ main = do putDoc :: Color -> P.Doc P.AnsiStyle -> IO () putDoc colorize doc = do isTty <- (== 1) <$> c_isatty 1 - columns <- fmap Terminal.width <$> Terminal.size - P.renderIO stdout - $ P.layoutSmart - P.defaultLayoutOptions - { P.layoutPageWidth = maybe P.Unbounded (\n -> P.AvailablePerLine n 1) columns - } - $ ( if colorize == Always || (colorize == Auto && isTty) - then (\x -> x) - else P.unAnnotate - ) - $ doc + term <- Terminal.size + let s = + P.renderLazy + $ P.layoutSmart + P.defaultLayoutOptions + { P.layoutPageWidth = maybe P.Unbounded (\n -> P.AvailablePerLine n 1) (Terminal.width <$> term) + } + $ ( if colorize == Always || (colorize == Auto && isTty) + then (\x -> x) + else P.unAnnotate + ) + $ doc + if maybe False (length (LT.lines s) >) (Terminal.height <$> term) + then + sh_ + ( "${PAGER-less}" + & P.shell + & P.setStdin (P.byteStringInput (LB.fromStrict (T.encodeUtf8 (LT.toStrict s)))) + ) + else LT.putStr s foreign import ccall "unistd.h isatty" c_isatty :: Int -> IO Int -- cgit v1.2.3