{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} module Hcompta.CLI.Context where import Control.Monad (liftM) import qualified Data.List import Data.Maybe (catMaybes) import System.Environment (getEnvironment) import System.IO.Memoize (once) data App = App data Context = Context { verbosity :: Verbosity , command :: String , color :: Maybe Bool , langs :: [String] } deriving (Show) context :: IO Context context = do langs <- get_langs return $ Context { verbosity = Verbosity_Info , command = "" , color = Nothing , langs } data Verbosity = Verbosity_Error | Verbosity_Warn | Verbosity_Info | Verbosity_Debug deriving (Bounded, Enum, Eq, Ord, Show) -- TODO: check that this is expected behavior -- and portability issues get_langs :: IO [String] get_langs = do once getEnvironment >>= liftM (\env -> Data.List.concatMap ((\lang -> let short = takeWhile ('_' /=) lang in if short == lang then [lang] else [lang, short]) . Data.List.takeWhile (\c -> c /= '.') ) $ catMaybes [ Data.List.lookup "LC_ALL" env , Data.List.lookup "LC_CTYPE" env , Data.List.lookup "LANG" env , Just "en" ])