prefs_impl.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import macros, tables, strutils, xmltree
  2. const hostname {.strdefine.} = "nitter.net"
  3. type
  4. PrefKind* = enum
  5. checkbox, select, input
  6. Pref* = object
  7. name*: string
  8. label*: string
  9. case kind*: PrefKind
  10. of checkbox:
  11. defaultState*: bool
  12. of select:
  13. defaultOption*: string
  14. options*: seq[string]
  15. of input:
  16. defaultInput*: string
  17. placeholder*: string
  18. # TODO: write DSL to simplify this
  19. const prefList*: OrderedTable[string, seq[Pref]] = {
  20. "Privacy": @[
  21. Pref(kind: input, name: "replaceTwitter",
  22. label: "Replace Twitter links with Nitter (blank to disable)",
  23. defaultInput: hostname, placeholder: "Nitter hostname"),
  24. Pref(kind: input, name: "replaceYouTube",
  25. label: "Replace YouTube links with Invidious (blank to disable)",
  26. defaultInput: "invidio.us", placeholder: "Invidious hostname")
  27. ],
  28. "Media": @[
  29. Pref(kind: checkbox, name: "mp4Playback",
  30. label: "Enable mp4 video playback",
  31. defaultState: true),
  32. Pref(kind: checkbox, name: "hlsPlayback",
  33. label: "Enable hls video streaming (requires JavaScript)",
  34. defaultState: false),
  35. Pref(kind: checkbox, name: "proxyVideos",
  36. label: "Proxy video streaming through the server (might be slow)",
  37. defaultState: true),
  38. Pref(kind: checkbox, name: "muteVideos",
  39. label: "Mute videos by default",
  40. defaultState: false),
  41. Pref(kind: checkbox, name: "autoplayGifs", label: "Autoplay gifs",
  42. defaultState: true)
  43. ],
  44. "Display": @[
  45. Pref(kind: checkbox, name: "hideTweetStats",
  46. label: "Hide tweet stats (replies, retweets, likes)",
  47. defaultState: false),
  48. Pref(kind: checkbox, name: "hideBanner", label: "Hide profile banner",
  49. defaultState: false),
  50. Pref(kind: checkbox, name: "stickyProfile",
  51. label: "Make profile sidebar stick to top",
  52. defaultState: true)
  53. ]
  54. }.toOrderedTable
  55. iterator allPrefs*(): Pref =
  56. for k, v in prefList:
  57. for pref in v:
  58. yield pref
  59. macro genDefaultPrefs*(): untyped =
  60. result = nnkObjConstr.newTree(ident("Prefs"))
  61. for pref in allPrefs():
  62. let default =
  63. case pref.kind
  64. of checkbox: newLit(pref.defaultState)
  65. of select: newLit(pref.defaultOption)
  66. of input: newLit(pref.defaultInput)
  67. result.add nnkExprColonExpr.newTree(ident(pref.name), default)
  68. macro genUpdatePrefs*(): untyped =
  69. result = nnkStmtList.newTree()
  70. for pref in allPrefs():
  71. let ident = ident(pref.name)
  72. let value = nnkPrefix.newTree(ident("@"), newLit(pref.name))
  73. case pref.kind
  74. of checkbox:
  75. result.add quote do: prefs.`ident` = `value` == "on"
  76. of input:
  77. result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`))
  78. of select:
  79. let options = pref.options
  80. let default = pref.defaultOption
  81. result.add quote do:
  82. if `value` in `options`: prefs.`ident` = `value`
  83. else: prefs.`ident` = `default`
  84. result.add quote do:
  85. cache(prefs)
  86. macro genPrefsType*(): untyped =
  87. let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
  88. result = quote do:
  89. type `name` = object
  90. id* {.pk, ro.}: int
  91. for pref in allPrefs():
  92. result[0][2][2].add nnkIdentDefs.newTree(
  93. nnkPostfix.newTree(ident("*"), ident(pref.name)),
  94. (case pref.kind
  95. of checkbox: ident("bool")
  96. of input, select: ident("string")),
  97. newEmptyNode())