on master help sign in
finish the implementation of userConfig
commit3822f036b17194f198b3d6125c07cdb778612b25 authorCorey O'Connor <coreyoconnor@gmail.com> dateThu 24 Apr 2014, 09:18 (13 years ago) parent689e8f5
2 files changed+12−6
src/Graphics/Vty/Config.hs+11−6 vty.cabal+1
src/Graphics/Vty/Config.hs+11−6
@@ -2,8 +2,8 @@
22{-# LANGUAGE NoMonomorphismRestriction #-}
33-- | A 'Config' can be provided to mkVty to customize the applications use of vty.
44--
5−-- The 'Config' provided is mappend'd to 'Config's loaded from $HOME/.config/vty.config and
6−-- $VTY_CONFIG_FILE. The $VTY_CONFIG_FILE takes precedence over the input.conf file or the
5+-- The 'Config' provided is mappend'd to 'Config's loaded from 'getAppUserDataDirectory'`/config`
6+-- and $VTY_CONFIG_FILE. The $VTY_CONFIG_FILE takes precedence over the `config` file or the
77-- application provided 'Config'.
88--
99-- Each line of the input config is processed individually. Lines that fail to parse are ignored.
@@ -35,6 +35,7 @@ module Graphics.Vty.Config where
3535
3636import Control.Applicative hiding (many)
3737
38+import Control.Exception (catch, IOException)
3839import Control.Monad (void)
3940import Control.Monad.Trans.Class
4041import Control.Monad.Trans.Writer
@@ -45,6 +46,8 @@ import Data.Monoid
4546
4647import Graphics.Vty.Input.Events
4748
49+import System.Directory (getAppUserDataDirectory)
50+import System.Environment (lookupEnv)
4851import Text.Parsec hiding ((<|>))
4952import Text.Parsec.Token ( GenLanguageDef(..) )
5053import qualified Text.Parsec.Token as P
@@ -81,12 +84,14 @@ type ConfigParser s a = ParsecT s () (Writer Config) a
8184
8285userConfig :: IO Config
8386userConfig = do
84− userGlobalConfig <- parseConfigFile "$HOME/.config/vty.config"
85− overrideConfig <- parseConfigFile "$VTY_CONFIG_FILE"
86− return $ userGlobalConfig `mappend` overrideConfig
87+ vtyConfig <- (mappend <$> getAppUserDataDirectory "vty" <*> pure "/config") >>= parseConfigFile
88+ overrideConfig <- lookupEnv "VTY_CONFIG_FILE" >>= maybe (return def) parseConfigFile
89+ return $ vtyConfig `mappend` overrideConfig
8790
8891parseConfigFile :: FilePath -> IO Config
89−parseConfigFile path = runParseConfig path <$> BS.readFile path
92+parseConfigFile path = do
93+ catch (runParseConfig path <$> BS.readFile path)
94+ (\(e :: IOException) -> return def)
9095
9196runParseConfig :: Stream s (Writer Config) Char => String -> s -> Config
9297runParseConfig name = execWriter . runParserT parseConfig () name
vty.cabal+1
@@ -48,6 +48,7 @@ library
4848 containers,
4949 data-default >= 0.5.3,
5050 deepseq >= 1.1 && < 1.4,
51+ directory,
5152 filepath >= 1.0 && < 2.0,
5253 lens >= 3.9.0.2 && < 5.0,
5354 -- required for nice installation with yi