prefs_impl.nim 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. bidiSupport(checkbox, false):
  53. "Support bidirectional text (makes clicking on tweets harder)"
  54. hideTweetStats(checkbox, false):
  55. "Hide tweet stats (replies, retweets, likes)"
  56. hideBanner(checkbox, false):
  57. "Hide profile banner"
  58. hidePins(checkbox, false):
  59. "Hide pinned tweets"
  60. hideReplies(checkbox, false):
  61. "Hide tweet replies"
  62. squareAvatars(checkbox, false):
  63. "Square profile pictures"
  64. Media:
  65. mp4Playback(checkbox, true):
  66. "Enable mp4 video playback (only for gifs)"
  67. hlsPlayback(checkbox, false):
  68. "Enable HLS video streaming (requires JavaScript)"
  69. proxyVideos(checkbox, true):
  70. "Proxy video streaming through the server (might be slow)"
  71. muteVideos(checkbox, false):
  72. "Mute videos by default"
  73. autoplayGifs(checkbox, true):
  74. "Autoplay gifs"
  75. "Link replacements (blank to disable)":
  76. replaceTwitter(input, ""):
  77. "Twitter -> Nitter"
  78. placeholder: "Nitter hostname"
  79. replaceYouTube(input, ""):
  80. "YouTube -> Piped/Invidious"
  81. placeholder: "Piped hostname"
  82. replaceReddit(input, ""):
  83. "Reddit -> Teddit/Libreddit"
  84. placeholder: "Teddit hostname"
  85. iterator allPrefs*(): Pref =
  86. for k, v in prefList:
  87. for pref in v:
  88. yield pref
  89. macro genDefaultPrefs*(): untyped =
  90. result = nnkStmtList.newTree()
  91. for pref in allPrefs():
  92. let
  93. ident = ident(pref.name)
  94. name = newLit(pref.name)
  95. default =
  96. case pref.kind
  97. of checkbox: newLit(pref.defaultState)
  98. of select: newLit(pref.defaultOption)
  99. of input: newLit(pref.defaultInput)
  100. result.add quote do:
  101. defaultPrefs.`ident` = cfg.get("Preferences", `name`, `default`)
  102. macro genCookiePrefs*(cookies): untyped =
  103. result = nnkStmtList.newTree()
  104. for pref in allPrefs():
  105. let
  106. name = pref.name
  107. ident = ident(pref.name)
  108. kind = newLit(pref.kind)
  109. options = pref.options
  110. result.add quote do:
  111. if `name` in `cookies`:
  112. when `kind` == input or `name` == "theme":
  113. result.`ident` = `cookies`[`name`]
  114. elif `kind` == checkbox:
  115. result.`ident` = `cookies`[`name`] == "on"
  116. else:
  117. let value = `cookies`[`name`]
  118. if value in `options`: result.`ident` = value
  119. macro genCookiePref*(cookies, prefName, res): untyped =
  120. result = nnkStmtList.newTree()
  121. for pref in allPrefs():
  122. let ident = ident(pref.name)
  123. if ident != prefName:
  124. continue
  125. let
  126. name = pref.name
  127. kind = newLit(pref.kind)
  128. options = pref.options
  129. result.add quote do:
  130. if `name` in `cookies`:
  131. when `kind` == input or `name` == "theme":
  132. `res` = `cookies`[`name`]
  133. elif `kind` == checkbox:
  134. `res` = `cookies`[`name`] == "on"
  135. else:
  136. let value = `cookies`[`name`]
  137. if value in `options`: `res` = value
  138. macro genUpdatePrefs*(): untyped =
  139. result = nnkStmtList.newTree()
  140. let req = ident("request")
  141. for pref in allPrefs():
  142. let
  143. name = newLit(pref.name)
  144. kind = newLit(pref.kind)
  145. options = newLit(pref.options)
  146. default = nnkDotExpr.newTree(ident("defaultPrefs"), ident(pref.name))
  147. result.add quote do:
  148. let val = @`name`
  149. let isDefault =
  150. when `kind` == input or `name` == "theme":
  151. if `default`.len != val.len: false
  152. else: val == `default`
  153. elif `kind` == checkbox:
  154. (val == "on") == `default`
  155. else:
  156. val notin `options` or val == `default`
  157. if isDefault:
  158. savePref(`name`, "", `req`, expire=true)
  159. else:
  160. savePref(`name`, val, `req`)
  161. macro genResetPrefs*(): untyped =
  162. result = nnkStmtList.newTree()
  163. let req = ident("request")
  164. for pref in allPrefs():
  165. let name = newLit(pref.name)
  166. result.add quote do:
  167. savePref(`name`, "", `req`, expire=true)
  168. macro genPrefsType*(): untyped =
  169. let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
  170. result = quote do:
  171. type `name` = object
  172. discard
  173. for pref in allPrefs():
  174. result[0][2][2].add nnkIdentDefs.newTree(
  175. nnkPostfix.newTree(ident("*"), ident(pref.name)),
  176. (case pref.kind
  177. of checkbox: ident("bool")
  178. of input, select: ident("string")),
  179. newEmptyNode())