~reesmichael1/roman

ref: 3784df6eb213fc0ccd683ed396d42645e1bca718 roman/src/romanpkg/config.nim -rw-r--r-- 1.8 KiB
3784df6eMichael Rees Fix crash when displaying very long link in post 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import parsecfg
import sequtils
import strutils

import errors
import paths

from types import RomanConfig

# Imported from generic function in termask, so the use isn't recognized
when defined(nimHasUsed):
  {.used.}


proc strToChar(config: Config, section: string, key: string): char {.
    raises: [RomanError].} =
  try:
    let s = config.getSectionValue(section, key)
    if s.len != 1:
      raise newException(RomanError, "expected single char for " & section &
          "." & key & ", got '" & s & "'")
    return toSeq(s.items)[0]
  except KeyError:
    raise newException(RomanError,
      "missing config value for " & section & "." & key)


proc strToInt(config: Config, section: string, key: string): int {.
    raises: [RomanError].} =
  try:
    let s = config.getSectionValue(section, key)
    result = parseInt(s)
  except KeyError:
    raise newException(RomanError,
      "missing config value for " & section & "." & key)
  except ValueError:
    raise newException(RomanError,
      "invalid value for " & section & "." & key & ", expected int")


proc mustLoadConfig*(): RomanConfig {.raises: [].} =
  try:
    let path = getConfigFilePath()
    let dict = loadConfig(path)
    result.down = strToChar(dict, "Keyboard", "down")
    result.up = strToChar(dict, "Keyboard", "up")
    result.next = strToChar(dict, "Keyboard", "next")
    result.previous = strToChar(dict, "Keyboard", "previous")
    result.quit = strToChar(dict, "Keyboard", "quit")
    result.goToTop = strToChar(dict, "Keyboard", "top")
    result.goToBottom = strToChar(dict, "Keyboard", "bottom")
    result.postWidth = strToInt(dict, "Posts", "max-width")
    result.extractLInks = strToChar(dict, "Posts", "extract-links")
  except:
    echo "error loading config file: " & getCurrentExceptionMsg()
    quit(1)


var conf* = mustLoadConfig()