MyNixOS website logo
Description

A library to manage application settings (INI file-like)

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, in that case the application will also save the default values in the configuration file, but commented out:

fontSize=16
# dateFormat="%x"
# backcolor=(255,0,0)

If you do not specify the list of settings, only the first line would be present in the configuration file.

With an ordinary setting, one row in the configuration file means one setting. That setting may of course be a list for instance. This setup works very well for shorter lists like [1,2,3], however if you have a list of more complex items, you will get very long lines and a configuration file very difficult to edit by hand.

For these special cases there is also the ListSetting constructor:

testList :: Setting [String]
testList = ListSetting "testList" ["list1", "list2", "list3"]

Now the configuration file looks like that:

testList_1="list1"
testList_2="list2"
testList_3="list3"

Which is much more handy for big lists. An empty list is represented like so:

testList=

There is also another technique that you can use if you have too long lines: you can put line breaks in the setting values if you start the following lines with a leading space, like so:

testList=["list1",
 "list2", "list3"]

In that case don't use the ListSetting option. Any character after the the leading space in the next lines will go in the setting value. Note that the library will automatically wrap setting values longer than 80 characters when saving.

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 fontSize
      saveSettings emptyDefaultConfig (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.

You can also look at the tests of the library on the github project for sample use.

Metadata

Version

0.2.0.12

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows