prefs_impl.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. infiniteScroll(checkbox, false):
  62. "Infinite scrolling (requires JavaScript, experimental!)"
  63. stickyProfile(checkbox, true):
  64. "Make profile sidebar stick to top"
  65. hideTweetStats(checkbox, false):
  66. "Hide tweet stats (replies, retweets, likes)"
  67. hideBanner(checkbox, false):
  68. "Hide profile banner"
  69. hidePins(checkbox, false):
  70. "Hide pinned tweets"
  71. hideReplies(checkbox, false):
  72. "Hide tweet replies"
  73. iterator allPrefs*(): Pref =
  74. for k, v in prefList:
  75. for pref in v:
  76. yield pref
  77. macro genDefaultPrefs*(): untyped =
  78. result = nnkObjConstr.newTree(ident("Prefs"))
  79. for pref in allPrefs():
  80. let default =
  81. case pref.kind
  82. of checkbox: newLit(pref.defaultState)
  83. of select: newLit(pref.defaultOption)
  84. of input: newLit(pref.defaultInput)
  85. result.add nnkExprColonExpr.newTree(ident(pref.name), default)
  86. macro genUpdatePrefs*(): untyped =
  87. result = nnkStmtList.newTree()
  88. for pref in allPrefs():
  89. let ident = ident(pref.name)
  90. let value = nnkPrefix.newTree(ident("@"), newLit(pref.name))
  91. case pref.kind
  92. of checkbox:
  93. result.add quote do: prefs.`ident` = `value` == "on"
  94. of input:
  95. result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`))
  96. of select:
  97. let name = pref.name
  98. let options = pref.options
  99. let default = pref.defaultOption
  100. result.add quote do:
  101. if `name` == "theme": prefs.`ident` = `value`
  102. elif `value` in `options`: prefs.`ident` = `value`
  103. else: prefs.`ident` = `default`
  104. result.add quote do:
  105. cache(prefs)
  106. macro genPrefsType*(): untyped =
  107. let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
  108. result = quote do:
  109. type `name` = object
  110. id* {.pk, ro.}: int
  111. for pref in allPrefs():
  112. result[0][2][2].add nnkIdentDefs.newTree(
  113. nnkPostfix.newTree(ident("*"), ident(pref.name)),
  114. (case pref.kind
  115. of checkbox: ident("bool")
  116. of input, select: ident("string")),
  117. newEmptyNode())