Implement utility functions to deal with the user

- getTerminalSize -> returns the dimensions of the terminal
- getUserCommand -> returns input from the user. We currently support
- ``NextPage`` and `Quit`.

Implement getTerminalSize via 'tput'
This commit is contained in:
Alexander Kobjolke 2023-08-29 00:08:02 +02:00
parent 5c113326e7
commit 8b1755425c

View file

@ -4,6 +4,10 @@ module HCat.Internal where
import Data.Text (Text)
import Data.Text qualified as T
import System.IO
import System.Info qualified as SysInfo
import System.Process qualified as P
-- | @parseArgs@ takes a list of strings and returns a single FilePath if there was exactly one element.
--
-- >>> parseArgs ["foo"]
@ -67,3 +71,29 @@ paginate (ScreenDimensions rows cols) =
type Pages = [Page]
type Page = Text
getTerminalSize :: IO ScreenDimensions
getTerminalSize = case SysInfo.os of
"linux" -> tputScreenDimensions
"darwin" -> tputScreenDimensions
_ -> pure $ defaultScreenDimensions{screenRows = 25, screenColumns = 80}
defaultScreenDimensions :: ScreenDimensions
defaultScreenDimensions = ScreenDimensions{screenRows = 25, screenColumns = 80}
tputScreenDimensions :: IO ScreenDimensions
tputScreenDimensions = ScreenDimensions <$> tput "lines" <*> tput "cols"
where
tput cmd = read <$> P.readProcess "tput" [cmd] ""
data UserCommand = NextPage | Quit deriving (Show)
getUserCommand :: IO UserCommand
getUserCommand = do
hSetBuffering stdin NoBuffering
hSetEcho stdin False
input <- getChar
case input of
' ' -> pure NextPage
'q' -> pure Quit
_ -> getUserCommand