app-settings 0.1.0.0 → 0.1.0.1
raw patch · 3 files changed
+77/−10 lines, 3 files
Files
- Data/AppSettings.hs +8/−6
- app-settings.cabal +67/−2
- tests/Tests.hs +2/−2
Data/AppSettings.hs view
@@ -106,7 +106,7 @@ -- -- @ -- getSetting \<setting\>--- setSetting \<setting\> \<value\>+-- setSetting \<conf\> \<setting\> \<value\> -- @ -- -- and so on.@@ -134,7 +134,7 @@ setting :: (Show a) => Setting a -> State Conf () setting (Setting nameV defaultV) = do soFar <- get- put $ M.insert nameV (SettingInfo { value = show defaultV, userSet = False }) soFar+ put $ M.insert nameV SettingInfo { value = show defaultV, userSet = False } soFar -- | Used in combination with 'setting' to register settings. -- Registering settings is optional, see 'DefaultConfig'.@@ -146,8 +146,7 @@ -- setting \<setting2\> -- @ getDefaultConfig :: State Conf () -> DefaultConfig-getDefaultConfig actions = do- DefaultConfig $ execState actions M.empty+getDefaultConfig actions = DefaultConfig $ execState actions M.empty -- | Where to look for or store the configuration file. data FileLocation = AutoFromAppName String@@ -179,6 +178,9 @@ -- option simply by giving that option (without that callback -- you'd have to call getSetting settings \<setting\>, so -- the callback lets you save a parameter).+-- There is no such shortcut for 'setSetting' though, as it's+-- normally used less often and in other contexts, it is probably+-- OK to have that extra parameter for the setSetting. -- -- Example of use: --@@ -204,7 +206,7 @@ saveSettings :: DefaultConfig -> FileLocation -> Conf -> IO () saveSettings (DefaultConfig defaults) location conf = do filePath <- getPathForLocation location- writeConfigFile filePath (M.union conf defaults)+ writeConfigFile filePath (conf `M.union` defaults) -- TODO maybe another getSetting that'll tell you -- if the setting is invalid in the config file instead of silently@@ -220,7 +222,7 @@ -- | Change the value of a setting. You'll have to call -- 'saveSettings' so that the change is written to disk. setSetting :: (Show a) => Conf -> Setting a -> a -> Conf-setSetting conf (Setting key _) v = M.insert key (SettingInfo { value = show v, userSet=True }) conf+setSetting conf (Setting key _) v = M.insert key SettingInfo { value = show v, userSet=True } conf getSettingsFolder :: String -> IO FilePath getSettingsFolder appName = do
app-settings.cabal view
@@ -2,9 +2,74 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: app-settings-version: 0.1.0.0+version: 0.1.0.1 synopsis: A library to manage application settings (INI file-like)--- description: +description: + A library to deal with application settings.+ This library deals with read-write application settings.+ You will have to specify the settings that your application+ uses, their name, types and default values.+ Setting types must implement the 'Read' and 'Show' typeclasses. + + The settings are saved in a file in an INI-like key-value format+ (without sections).+ + Reading and updating settings is done in pure code, the IO+ monad is only used to load settings and save them to disk.+ It is advised for the user to create a module in your project+ holding settings handling.+ + You can then declare settings:+ + > fontSize :: Setting Double+ > fontSize = Setting "fontSize" 14+ > + > dateFormat :: Setting String+ > dateFormat = Setting "dateFormat" "%x"+ > + > backgroundColor :: Setting (Int, Int, Int)+ > backgroundColor = Setting "backcolor" (255, 0, 0)+ + Optionally you can declare the list of all your settings:+ + > defaultConfig :: DefaultConfig+ > defaultConfig = getDefaultConfig $ do+ > setting fontSize+ > setting dateFormat+ > setting backgroundColor+ + If you do it, 'saveSettings' will also save settings+ which have not been modified, which are still at their+ default value in the configuration file, in a commented+ form, as a documentation to the user who may open the+ configuration file.+ So for instance if you declare this default configuration+ and have set the font size to 16 but left the other+ settings untouched, the configuration file which will be+ saved will be:+ + > fontSize=16+ > # dateFormat="%x"+ > # backcolor=(255,0,0)+ + If you did not specify the list of settings, only the+ first line would be present in the configuration file.+ + Once we declared the settings, we can read the configuration+ from disk (and your settings module should export your wrapper+ around the function offered by this library):+ + > readResult <- try $ readSettings (AutoFromAppName "test")+ > case readResult of+ > Right (conf, GetSetting getSetting) -> do+ > let textSize = getSetting textSizeFromWidth+ > saveSettings getDefaultConfig (AutoFromAppName "test") conf+ > Left (x :: SomeException) -> error "Error reading the config file!"+ + 'AutoFromAppName' specifies where to save the configuration file.+ And we've already covered the getSetting in this snippet, see + the 'readSettings' documentation for further information.+ homepage: https://github.com/emmanueltouzery/app-settings license: BSD3 license-file: LICENSE
tests/Tests.hs view
@@ -113,8 +113,8 @@ Right (conf, _) -> do copyFile "tests/partial.config" "p.config" saveSettings defaultConfig (Path "p.config") conf- doesFileExist "p.config.bak" >>= (flip shouldBe) True- doesFileExist "p.config" >>= (flip shouldBe) True+ doesFileExist "p.config.bak" >>= flip shouldBe True+ doesFileExist "p.config" >>= flip shouldBe True removeFile "p.config" removeFile "p.config.bak" Left (x :: SomeException) -> assertBool (show x) False