prefs_impl.nim 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # SPDX-License-Identifier: AGPL-3.0-only
  2. import macros, tables, strutils, xmltree
  3. type
  4. PrefKind* = enum
  5. checkbox, select, input
  6. Pref* = object
  7. name*: string
  8. label*: string
  9. kind*: PrefKind
  10. # checkbox
  11. defaultState*: bool
  12. # select
  13. defaultOption*: string
  14. options*: seq[string]
  15. # input
  16. defaultInput*: string
  17. placeholder*: string
  18. PrefList* = OrderedTable[string, seq[Pref]]
  19. macro genPrefs*(prefDsl: untyped) =
  20. var table = nnkTableConstr.newTree()
  21. for category in prefDsl:
  22. table.add nnkExprColonExpr.newTree(newLit($category[0]))
  23. table[^1].add nnkPrefix.newTree(newIdentNode("@"), nnkBracket.newTree())
  24. for pref in category[1]:
  25. let
  26. name = newLit($pref[0])
  27. kind = pref[1]
  28. label = pref[3][0]
  29. default = pref[2]
  30. defaultField =
  31. case parseEnum[PrefKind]($kind)
  32. of checkbox: ident("defaultState")
  33. of select: ident("defaultOption")
  34. of input: ident("defaultInput")
  35. var newPref = quote do:
  36. Pref(kind: `kind`, name: `name`, label: `label`, `defaultField`: `default`)
  37. for node in pref[3]:
  38. if node.kind == nnkCall:
  39. newPref.add nnkExprColonExpr.newTree(node[0], node[1][0])
  40. table[^1][1][1].add newPref
  41. let name = ident("prefList")
  42. result = quote do:
  43. const `name`*: PrefList = toOrderedTable(`table`)
  44. genPrefs:
  45. Display:
  46. theme(select, "Nitter"):
  47. "Theme"
  48. infiniteScroll(checkbox, false):
  49. "Infinite scrolling (experimental, requires JavaScript)"
  50. stickyProfile(checkbox, true):
  51. "Make profile sidebar stick to top"
  52. stickyNav(checkbox, true):
  53. "Keep navbar fixed to top"
  54. bidiSupport(checkbox, false):
  55. "Support bidirectional text (makes clicking on tweets harder)"
  56. hideTweetStats(checkbox, false):
  57. "Hide tweet stats (replies, retweets, likes)"
  58. hideBanner(checkbox, false):
  59. "Hide profile banner"
  60. hidePins(checkbox, false):
  61. "Hide pinned tweets"
  62. hideReplies(checkbox, false):
  63. "Hide tweet replies"
  64. hideRelated(checkbox, true):
  65. "Hide related tweets under replies"
  66. hideCommunityNotes(checkbox, false):
  67. "Hide community notes"
  68. squareAvatars(checkbox, false):
  69. "Square profile pictures"
  70. Media:
  71. mp4Playback(checkbox, true):
  72. "Enable mp4 video playback (only for gifs)"
  73. hlsPlayback(checkbox, false):
  74. "Enable HLS video streaming (requires JavaScript)"
  75. proxyVideos(checkbox, true):
  76. "Proxy video streaming through the server (might be slow)"
  77. muteVideos(checkbox, false):
  78. "Mute videos by default"
  79. autoplayGifs(checkbox, true):
  80. "Autoplay gifs"
  81. compactGallery(checkbox, false):
  82. "Compact media gallery (no profile info or text)"
  83. gallerySize(select, "Medium"):
  84. "Gallery column size"
  85. options: @["Small", "Medium", "Large"]
  86. mediaView(select, "Timeline"):
  87. "Default media view"
  88. options: @["Timeline", "Grid", "Gallery"]
  89. "Link replacements (blank to disable)":
  90. replaceTwitter(input, ""):
  91. "Twitter -> Nitter"
  92. placeholder: "Nitter hostname"
  93. replaceYouTube(input, ""):
  94. "YouTube -> Piped/Invidious"
  95. placeholder: "Piped hostname"
  96. replaceReddit(input, ""):
  97. "Reddit -> Teddit/Libreddit"
  98. placeholder: "Teddit hostname"
  99. iterator allPrefs*(): Pref =
  100. for k, v in prefList:
  101. for pref in v:
  102. yield pref
  103. macro genDefaultPrefs*(): untyped =
  104. result = nnkStmtList.newTree()
  105. for pref in allPrefs():
  106. let
  107. ident = ident(pref.name)
  108. name = newLit(pref.name)
  109. default =
  110. case pref.kind
  111. of checkbox: newLit(pref.defaultState)
  112. of select: newLit(pref.defaultOption)
  113. of input: newLit(pref.defaultInput)
  114. result.add quote do:
  115. defaultPrefs.`ident` = cfg.get("Preferences", `name`, `default`)
  116. macro genParsePrefs*(prefs): untyped =
  117. result = nnkStmtList.newTree()
  118. for pref in allPrefs():
  119. let
  120. name = pref.name
  121. ident = ident(pref.name)
  122. kind = newLit(pref.kind)
  123. options = pref.options
  124. result.add quote do:
  125. if `name` in `prefs`:
  126. when `kind` == input or `name` == "theme":
  127. result.`ident` = `prefs`[`name`]
  128. elif `kind` == checkbox:
  129. result.`ident` = `prefs`[`name`] == "on" or
  130. `prefs`[`name`] == "true" or
  131. `prefs`[`name`] == "1"
  132. else:
  133. let value = `prefs`[`name`]
  134. if value in `options`: result.`ident` = value
  135. macro genUpdatePrefs*(): untyped =
  136. result = nnkStmtList.newTree()
  137. let req = ident("request")
  138. for pref in allPrefs():
  139. let
  140. name = newLit(pref.name)
  141. kind = newLit(pref.kind)
  142. options = newLit(pref.options)
  143. default = nnkDotExpr.newTree(ident("defaultPrefs"), ident(pref.name))
  144. result.add quote do:
  145. let val = @`name`
  146. let isDefault =
  147. when `kind` == input or `name` == "theme":
  148. if `default`.len != val.len: false
  149. else: val == `default`
  150. elif `kind` == checkbox:
  151. (val == "on") == `default`
  152. else:
  153. val notin `options` or val == `default`
  154. if isDefault:
  155. savePref(`name`, "", `req`, expire=true)
  156. else:
  157. savePref(`name`, val, `req`)
  158. macro genResetPrefs*(): untyped =
  159. result = nnkStmtList.newTree()
  160. let req = ident("request")
  161. for pref in allPrefs():
  162. let name = newLit(pref.name)
  163. result.add quote do:
  164. savePref(`name`, "", `req`, expire=true)
  165. macro genEncodePrefs*(prefs): untyped =
  166. result = nnkStmtList.newTree()
  167. for pref in allPrefs():
  168. let
  169. name = newLit(pref.name)
  170. ident = ident(pref.name)
  171. kind = newLit(pref.kind)
  172. defaultIdent = nnkDotExpr.newTree(ident("defaultPrefs"), ident(pref.name))
  173. result.add quote do:
  174. when `kind` == checkbox:
  175. if `prefs`.`ident` != `defaultIdent`:
  176. if `prefs`.`ident`:
  177. encPairs.add `name` & "=on"
  178. else:
  179. encPairs.add `name` & "="
  180. else:
  181. if `prefs`.`ident` != `defaultIdent`:
  182. encPairs.add `name` & "=" & `prefs`.`ident`
  183. macro genApplyPrefs*(params, req): untyped =
  184. result = nnkStmtList.newTree()
  185. for pref in allPrefs():
  186. let name = newLit(pref.name)
  187. result.add quote do:
  188. if `name` in `params`:
  189. savePref(`name`, `params`[`name`], `req`)
  190. else:
  191. savePref(`name`, "", `req`, expire=true)
  192. macro genPrefsType*(): untyped =
  193. let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
  194. result = quote do:
  195. type `name` = object
  196. discard
  197. for pref in allPrefs():
  198. result[0][2][2].add nnkIdentDefs.newTree(
  199. nnkPostfix.newTree(ident("*"), ident(pref.name)),
  200. (case pref.kind
  201. of checkbox: ident("bool")
  202. of input, select: ident("string")),
  203. newEmptyNode())