module Hpp.Config where
import Data.Functor.Identity
import Data.Time.Clock (getCurrentTime, UTCTime)
import Data.Time.Format
newtype TimeString = TimeString { getTimeString :: String }
deriving (Eq, Ord, Show)
newtype DateString = DateString { getDateString :: String }
deriving (Eq, Ord, Show)
data ConfigF f = Config { curFileNameF :: f FilePath
, includePathsF :: f [FilePath]
, spliceLongLinesF :: f Bool
, eraseCCommentsF :: f Bool
, inhibitLinemarkersF :: f Bool
, replaceTrigraphsF :: f Bool
, prepDateF :: f DateString
, prepTimeF :: f TimeString }
type Config = ConfigF Identity
realizeConfig :: ConfigF Maybe -> Maybe Config
realizeConfig (Config (Just fileName)
(Just paths)
(Just spliceLines)
(Just comments)
(Just inhibitLines)
(Just trigraphs)
(Just pdate)
(Just ptime)) =
Just (Config (pure fileName) (pure paths) (pure spliceLines) (pure comments)
(pure inhibitLines) (pure trigraphs) (pure pdate) (pure ptime))
realizeConfig _ = Nothing
curFileName :: Config -> FilePath
curFileName = runIdentity . curFileNameF
includePaths :: Config -> [FilePath]
includePaths = runIdentity . includePathsF
spliceLongLines :: Config -> Bool
spliceLongLines = runIdentity . spliceLongLinesF
eraseCComments :: Config -> Bool
eraseCComments = runIdentity . eraseCCommentsF
inhibitLinemarkers :: Config -> Bool
inhibitLinemarkers = runIdentity . inhibitLinemarkersF
replaceTrigraphs :: Config -> Bool
replaceTrigraphs = runIdentity . replaceTrigraphsF
prepDate :: Config -> DateString
prepDate = runIdentity . prepDateF
prepTime :: Config -> TimeString
prepTime = runIdentity . prepTimeF
defaultConfigF :: ConfigF Maybe
defaultConfigF = Config Nothing (Just [])
(Just True) (Just True) (Just True) (Just False)
(Just (DateString "??? ?? ????"))
(Just (TimeString "??:??:??"))
formatPrepDate :: UTCTime -> DateString
formatPrepDate = DateString . formatTime defaultTimeLocale "%b %e %Y"
formatPrepTime :: UTCTime -> TimeString
formatPrepTime = TimeString . formatTime defaultTimeLocale "%T"
defaultConfigFNow :: IO (ConfigF Maybe)
defaultConfigFNow = do now <- getCurrentTime
let d = formatPrepDate now
t = formatPrepTime now
return $ defaultConfigF { prepDateF = Just d
, prepTimeF = Just t }