Safe Haskell | None |
---|---|
Language | Haskell2010 |
- configFromFile :: MonadIO io => io ConfigMonoid
- data Attr
- data Color
- data Style
Documentation
configFromFile :: MonadIO io => io ConfigMonoid Source #
Reads the configuration from a JSON or YAML file. The file should be located in one of the following places:
~/.vgrep/config.yaml
,~/.vgrep/config.yml
,~/.vgrep/config.json
or~/.vgrep/config
.
When none of these files exist, no error is raised. When a file exists, but cannot be parsed, a warning is written to stderr.
Supported formats are JSON and YAML. The example YAML config given in the
project directory (config.yaml.example
) is equivalent to the default
config:
>>>
import qualified Vgrep.Environment.Config as C
>>>
Right config <- decodeFileEither "config.yaml.example" :: IO (Either ParseException ConfigMonoid)
>>>
C.fromConfigMonoid config == C.defaultConfig
True
Example YAML config file for defaultConfig
:
colors: line-numbers: fore-color: blue line-numbers-hl: fore-color: blue style: bold normal: {} normal-hl: style: bold file-headers: back-color: green selected: style: standout tabstop: 8 editor: "vi"
Example JSON file for the same config:
{ "colors": { "line-numbers" : { "fore-color": "blue" }, "line-numbers-hl": { "fore-color": "blue", "style": "bold" }, "normal": {}, "normal-hl": { "style": "bold" }, "file-headers": { "back-color": "green" }, "selected": { "style": "standout" } }, "tabstop": 8, "editor": "vi" }
The JSON/YAML keys correspond to the lenses in Vgrep.Environment.Config,
the values for Color
and Style
can be obtained from the
corresponding predefined constants in Graphics.Vty.Attributes.
A JSON-parsable data type for Attr
.
JSON example:
>>>
decodeEither "{\"fore-color\": \"black\", \"style\": \"standout\"}" :: Either String Attr
Right (Attr {foreColor = Just Black, backColor = Nothing, style = Just Standout})
JSON example without quotes: >>> decodeEither "{fore-color: black, style: standout}" :: Either String Attr Right (Attr {foreColor = Just Black, backColor = Nothing, style = Just Standout})
YAML example:
>>>
:{
>>>
decodeEither
>>>
$ "fore-color: \"blue\"\n"
>>>
<> "back-color: \"bright-blue\"\n"
>>>
<> "style: \"reverse-video\"\n"
>>>
:: Either String Attr
>>>
:}
Right (Attr {foreColor = Just Blue, backColor = Just BrightBlue, style = Just ReverseVideo})
YAML example without quotes:
>>>
:{
>>>
decodeEither
>>>
$ "fore-color: blue\n"
>>>
<> "back-color: bright-blue\n"
>>>
<> "style: reverse-video\n"
>>>
:: Either String Attr
>>>
:}
Right (Attr {foreColor = Just Blue, backColor = Just BrightBlue, style = Just ReverseVideo})
An empty JSON/YAML object yields the default colors:
>>>
decodeEither "{}" :: Either String Attr
Right (Attr {foreColor = Nothing, backColor = Nothing, style = Nothing})
A JSON-parsable data type for Color
.
>>>
decodeEither "[\"black\",\"red\",\"bright-black\"]" :: Either String [Color]
Right [Black,Red,BrightBlack]
Also works without quotes:
>>>
decodeEither "[black,red,bright-black]" :: Either String [Color]
Right [Black,Red,BrightBlack]
Fails with error message if the Color
cannot be parsed:
>>>
let Left err = decodeEither "foo" :: Either String Color
>>>
"The key \"foo\" was not found" `isInfixOf` err
True
A JSON-parsable data type for Style
.
>>>
decodeEither "[\"standout\", \"underline\", \"bold\"]" :: Either String [Style]
Right [Standout,Underline,Bold]
Also works without quotes:
>>>
decodeEither "[standout, underline, bold]" :: Either String [Style]
Right [Standout,Underline,Bold]
Fails with error message if the Style
cannot be parsed:
>>>
let Left err = decodeEither "foo" :: Either String Style
>>>
"The key \"foo\" was not found" `isInfixOf` err
True