packages feed

matsuri-0.0.4: Utils.hs

-- | Utils.hs
-- A module for some helper functions.

module Utils (
    getW,
    nowTime,
    withTime,
    utcToZoned,
    take'
) where

import Graphics.Vty
import System.Locale
import Data.Time.LocalTime
import Data.Time.Format


-- | Get vty width.
getW :: Vty -> IO Int
getW vty = do
    DisplayRegion w h <- display_bounds $ terminal vty
    return $ fromIntegral w

-- | Print current time.
nowTime :: IO String
nowTime = getZonedTime >>= format

-- | Simple helper.
withTime :: String -> String -> IO String
withTime p str = do
    time <- nowTime
    return $ time++p++str

-- | Convert UTC string (from jabber:x:delay) to zoned.
utcToZoned :: String -> IO String
utcToZoned t = (utcToLocalZonedTime $
                readTime defaultTimeLocale "%Y%m%dT%H:%M:%S" t) >>= format

format = return . formatTime defaultTimeLocale "%H:%M:%S"

-- | Do defined list width.
take' :: Int -> a -> [a] -> [a]
take' n add lst = if length lst < n
                  then lst ++ replicate (n - length lst) add
                  else take n lst