on master help sign in
finish the implementation of userConfig
3822f03Corey O'Connor
src/Graphics/Vty/Config.hs+11−6
@@ -2,8 +2,8 @@
2{-# LANGUAGE NoMonomorphismRestriction #-}
2{-# LANGUAGE NoMonomorphismRestriction #-}
3-- | A 'Config' can be provided to mkVty to customize the applications use of vty.
3-- | A 'Config' can be provided to mkVty to customize the applications use of vty.
4--
4--
5−-- The 'Config' provided is mappend'd to 'Config's loaded from $HOME/.config/vty.config and
5+-- The 'Config' provided is mappend'd to 'Config's loaded from 'getAppUserDataDirectory'`/config`
6−-- $VTY_CONFIG_FILE. The $VTY_CONFIG_FILE takes precedence over the input.conf file or the
6+-- and $VTY_CONFIG_FILE. The $VTY_CONFIG_FILE takes precedence over the `config` file or the
7-- application provided 'Config'.
7-- application provided 'Config'.
8--
8--
9-- Each line of the input config is processed individually. Lines that fail to parse are ignored.
9-- 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
35
35
36import Control.Applicative hiding (many)
36import Control.Applicative hiding (many)
37
37
38+import Control.Exception (catch, IOException)
38import Control.Monad (void)
39import Control.Monad (void)
39import Control.Monad.Trans.Class
40import Control.Monad.Trans.Class
40import Control.Monad.Trans.Writer
41import Control.Monad.Trans.Writer
@@ -45,6 +46,8 @@ import Data.Monoid
45
46
46import Graphics.Vty.Input.Events
47import Graphics.Vty.Input.Events
47
48
49+import System.Directory (getAppUserDataDirectory)
50+import System.Environment (lookupEnv)
48import Text.Parsec hiding ((<|>))
51import Text.Parsec hiding ((<|>))
49import Text.Parsec.Token ( GenLanguageDef(..) )
52import Text.Parsec.Token ( GenLanguageDef(..) )
50import qualified Text.Parsec.Token as P
53import qualified Text.Parsec.Token as P
@@ -81,12 +84,14 @@ type ConfigParser s a = ParsecT s () (Writer Config) a
81
84
82userConfig :: IO Config
85userConfig :: IO Config
83userConfig = do
86userConfig = do
84− userGlobalConfig <- parseConfigFile "$HOME/.config/vty.config"
87+ vtyConfig <- (mappend <$> getAppUserDataDirectory "vty" <*> pure "/config") >>= parseConfigFile
85− overrideConfig <- parseConfigFile "$VTY_CONFIG_FILE"
88+ overrideConfig <- lookupEnv "VTY_CONFIG_FILE" >>= maybe (return def) parseConfigFile
86− return $ userGlobalConfig `mappend` overrideConfig
89+ return $ vtyConfig `mappend` overrideConfig
87
90
88parseConfigFile :: FilePath -> IO Config
91parseConfigFile :: FilePath -> IO Config
92+parseConfigFile path = do
89−parseConfigFile path = runParseConfig path <$> BS.readFile path
93+ catch (runParseConfig path <$> BS.readFile path)
94+ (\(e :: IOException) -> return def)
90
95
91runParseConfig :: Stream s (Writer Config) Char => String -> s -> Config
96runParseConfig :: Stream s (Writer Config) Char => String -> s -> Config
92runParseConfig name = execWriter . runParserT parseConfig () name
97runParseConfig name = execWriter . runParserT parseConfig () name
vty.cabal+1
@@ -48,6 +48,7 @@ library
48 containers,
48 containers,
49 data-default >= 0.5.3,
49 data-default >= 0.5.3,
50 deepseq >= 1.1 && < 1.4,
50 deepseq >= 1.1 && < 1.4,
51+ directory,
51 filepath >= 1.0 && < 2.0,
52 filepath >= 1.0 && < 2.0,
52 lens >= 3.9.0.2 && < 5.0,
53 lens >= 3.9.0.2 && < 5.0,
53 -- required for nice installation with yi
54 -- required for nice installation with yi