prefs_impl.nim 3.8 KB

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