aboutsummaryrefslogtreecommitdiffstats
path: root/app/Main.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2023-10-16 22:55:26 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2023-10-16 22:55:30 +0200
commit20e95095090d5febce7deb17cfa6eec0a93e0482 (patch)
tree53adba3558542aabbe800b737ebbbcf497ee69aa /app/Main.hs
parent636256a5d4ad27fefcf71541f07d8f939efe14f8 (diff)
paginate output
Diffstat (limited to 'app/Main.hs')
-rw-r--r--app/Main.hs37
1 files changed, 25 insertions, 12 deletions
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