prefs_impl.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. replaceInstagram(input, ""):
  48. "Replace Instagram links with Bibliogram (blank to disable)"
  49. placeholder: "Bibliogram hostname"
  50. Media:
  51. mp4Playback(checkbox, true):
  52. "Enable mp4 video playback"
  53. hlsPlayback(checkbox, false):
  54. "Enable hls video streaming (requires JavaScript)"
  55. proxyVideos(checkbox, true):
  56. "Proxy video streaming through the server (might be slow)"
  57. muteVideos(checkbox, false):
  58. "Mute videos by default"
  59. autoplayGifs(checkbox, true):
  60. "Autoplay gifs"
  61. Display:
  62. theme(select, "Nitter"):
  63. "Theme"
  64. infiniteScroll(checkbox, false):
  65. "Infinite scrolling (requires JavaScript, experimental!)"
  66. stickyProfile(checkbox, true):
  67. "Make profile sidebar stick to top"
  68. hideTweetStats(checkbox, false):
  69. "Hide tweet stats (replies, retweets, likes)"
  70. hideBanner(checkbox, false):
  71. "Hide profile banner"
  72. hidePins(checkbox, false):
  73. "Hide pinned tweets"
  74. hideReplies(checkbox, false):
  75. "Hide tweet replies"
  76. iterator allPrefs*(): Pref =
  77. for k, v in prefList:
  78. for pref in v:
  79. yield pref
  80. macro genDefaultPrefs*(): untyped =
  81. result = nnkObjConstr.newTree(ident("Prefs"))
  82. for pref in allPrefs():
  83. let default =
  84. case pref.kind
  85. of checkbox: newLit(pref.defaultState)
  86. of select: newLit(pref.defaultOption)
  87. of input: newLit(pref.defaultInput)
  88. result.add nnkExprColonExpr.newTree(ident(pref.name), default)
  89. macro genUpdatePrefs*(): untyped =
  90. result = nnkStmtList.newTree()
  91. for pref in allPrefs():
  92. let ident = ident(pref.name)
  93. let value = nnkPrefix.newTree(ident("@"), newLit(pref.name))
  94. case pref.kind
  95. of checkbox:
  96. result.add quote do: prefs.`ident` = `value` == "on"
  97. of input:
  98. result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`))
  99. of select:
  100. let name = pref.name
  101. let options = pref.options
  102. let default = pref.defaultOption
  103. result.add quote do:
  104. if `name` == "theme": prefs.`ident` = `value`
  105. elif `value` in `options`: prefs.`ident` = `value`
  106. else: prefs.`ident` = `default`
  107. result.add quote do:
  108. cache(prefs)
  109. macro genPrefsType*(): untyped =
  110. let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
  111. result = quote do:
  112. type `name` = object
  113. id* {.pk, ro.}: int
  114. for pref in allPrefs():
  115. result[0][2][2].add nnkIdentDefs.newTree(
  116. nnkPostfix.newTree(ident("*"), ident(pref.name)),
  117. (case pref.kind
  118. of checkbox: ident("bool")
  119. of input, select: ident("string")),
  120. newEmptyNode())