--------------------------------------------------------------------------------
module Patat.Size
    ( Size (..)
    , getTerminalSize
    ) where


--------------------------------------------------------------------------------
import           Data.Maybe                   (fromMaybe)
import qualified System.Console.Terminal.Size as Terminal


--------------------------------------------------------------------------------
data Size = Size {Size -> Int
sRows :: Int, Size -> Int
sCols :: Int} deriving (Int -> Size -> ShowS
[Size] -> ShowS
Size -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Size] -> ShowS
$cshowList :: [Size] -> ShowS
show :: Size -> String
$cshow :: Size -> String
showsPrec :: Int -> Size -> ShowS
$cshowsPrec :: Int -> Size -> ShowS
Show)


--------------------------------------------------------------------------------
getTerminalSize :: IO Size
getTerminalSize :: IO Size
getTerminalSize = do
    Maybe (Window Int)
mbWindow <- forall n. Integral n => IO (Maybe (Window n))
Terminal.size
    let rows :: Int
rows = forall a. a -> Maybe a -> a
fromMaybe Int
24 forall a b. (a -> b) -> a -> b
$ forall a. Window a -> a
Terminal.height forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Window Int)
mbWindow
        cols :: Int
cols = forall a. a -> Maybe a -> a
fromMaybe Int
72 forall a b. (a -> b) -> a -> b
$ forall a. Window a -> a
Terminal.width  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Window Int)
mbWindow
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Size {sRows :: Int
sRows = Int
rows, sCols :: Int
sCols = Int
cols}