diff --git a/src/HCat.hs b/src/HCat.hs index fa93ec5..673edd2 100644 --- a/src/HCat.hs +++ b/src/HCat.hs @@ -4,20 +4,34 @@ module HCat ( ) where import System.Environment qualified as Env -import System.IO (hPutStrLn, stderr) +import System.IO import Control.Exception qualified as Exception import System.IO.Error qualified as IOError import Data.Text.IO qualified as TextIO -import HCat.Internal (parseArgs) +import HCat.Internal runHCat :: IO () runHCat = do fileNameOrError <- parseArgs <$> Env.getArgs fileName <- eitherToError fileNameOrError - TextIO.readFile fileName >>= TextIO.putStr + content <- TextIO.readFile fileName + screen <- getTerminalSize + let contentPane = screen{screenRows = screenRows screen - 1} + showPages $ paginate contentPane content + +showPages :: Pages -> IO () +showPages [] = pure () +showPages (p : ps) = do + TextIO.putStr p + putStr ">>> (n)ext page or (q)uit?" + hFlush stdout + cmd <- getUserCommand + case cmd of + Quit -> pure () + NextPage -> putStrLn "" >> showPages ps eitherToError :: Show a => Either a b -> IO b eitherToError = either (Exception.throwIO . IOError.userError . show) return diff --git a/src/HCat/Internal.hs b/src/HCat/Internal.hs index 483049b..e443d2c 100644 --- a/src/HCat/Internal.hs +++ b/src/HCat/Internal.hs @@ -76,13 +76,18 @@ getTerminalSize :: IO ScreenDimensions getTerminalSize = case SysInfo.os of "linux" -> tputScreenDimensions "darwin" -> tputScreenDimensions - _ -> pure $ defaultScreenDimensions{screenRows = 25, screenColumns = 80} + _ -> pure defaultScreenDimensions defaultScreenDimensions :: ScreenDimensions -defaultScreenDimensions = ScreenDimensions{screenRows = 25, screenColumns = 80} +defaultScreenDimensions = + ScreenDimensions + { screenRows = 25 + , screenColumns = 80 + } tputScreenDimensions :: IO ScreenDimensions -tputScreenDimensions = ScreenDimensions <$> tput "lines" <*> tput "cols" +tputScreenDimensions = + ScreenDimensions <$> tput "lines" <*> tput "cols" where tput cmd = read <$> P.readProcess "tput" [cmd] "" @@ -94,6 +99,6 @@ getUserCommand = do hSetEcho stdin False input <- getChar case input of - ' ' -> pure NextPage + 'n' -> pure NextPage 'q' -> pure Quit _ -> getUserCommand