------------------------------------------------------------------------------
-- |
-- Module      : HR.Monad.Terminal
-- Description : terminal output
-- Copyright   : Copyright (c) 2019-2023 Travis Cardwell
-- License     : MIT
------------------------------------------------------------------------------

module HR.Monad.Terminal
  ( -- * MonadTerminal
    MonadTerminal(..)
  ) where

-- https://hackage.haskell.org/package/terminal-size
import qualified System.Console.Terminal.Size as TS

-- https://hackage.haskell.org/package/text
import Data.Text (Text)
import qualified Data.Text.IO as TIO

------------------------------------------------------------------------------
-- $MonadTerminal

-- | Terminal output
--
-- @since 0.5.0.0
class Monad m => MonadTerminal m where
  -- | Get the width of the terminal, if possible
  getWidth :: m (Maybe Int)

  -- | Write a string to @STDOUT@, appending a newline
  putStrLn :: Text -> m ()

instance MonadTerminal IO where
  getWidth :: IO (Maybe Int)
getWidth = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Window a -> a
TS.width forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall n. Integral n => IO (Maybe (Window n))
TS.size
  {-# INLINE getWidth #-}

  putStrLn :: Text -> IO ()
putStrLn = Text -> IO ()
TIO.putStrLn
  {-# INLINE putStrLn #-}