prefs_impl.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import macros, tables, strutils, xmltree
  2. const hostname {.strdefine.} = "nitter.net"
  3. type
  4. PrefKind* = enum
  5. checkbox, select, input
  6. Pref* = object
  7. name*: string
  8. label*: string
  9. case kind*: PrefKind
  10. of checkbox:
  11. defaultState*: bool
  12. of select:
  13. defaultOption*: string
  14. options*: seq[string]
  15. of input:
  16. defaultInput*: string
  17. placeholder*: string
  18. # TODO: write DSL to simplify this
  19. const prefList*: Table[string, seq[Pref]] = {
  20. "Privacy": @[
  21. Pref(kind: input, name: "replaceTwitter",
  22. label: "Replace Twitter links with Nitter (blank to disable)",
  23. defaultInput: hostname, placeholder: "Nitter hostname"),
  24. Pref(kind: input, name: "replaceYouTube",
  25. label: "Replace YouTube links with Invidious (blank to disable)",
  26. defaultInput: "invidio.us", placeholder: "Invidious hostname")
  27. ],
  28. "Media": @[
  29. Pref(kind: checkbox, name: "mp4Playback",
  30. label: "Enable mp4 video playback",
  31. defaultState: true),
  32. Pref(kind: checkbox, name: "hlsPlayback",
  33. label: "Enable hls video streaming (requires JavaScript)",
  34. defaultState: false),
  35. Pref(kind: checkbox, name: "muteVideos",
  36. label: "Mute videos by default",
  37. defaultState: false),
  38. Pref(kind: checkbox, name: "autoplayGifs", label: "Autoplay gifs",
  39. defaultState: true)
  40. ],
  41. "Display": @[
  42. Pref(kind: checkbox, name: "hideTweetStats",
  43. label: "Hide tweet stats (replies, retweets, likes)",
  44. defaultState: false),
  45. Pref(kind: checkbox, name: "hideBanner", label: "Hide profile banner",
  46. defaultState: false),
  47. Pref(kind: checkbox, name: "stickyProfile",
  48. label: "Make profile sidebar stick to top",
  49. defaultState: true)
  50. ]
  51. }.toTable
  52. iterator allPrefs*(): Pref =
  53. for k, v in prefList:
  54. for pref in v:
  55. yield pref
  56. macro genDefaultPrefs*(): untyped =
  57. result = nnkObjConstr.newTree(ident("Prefs"))
  58. for pref in allPrefs():
  59. let default =
  60. case pref.kind
  61. of checkbox: newLit(pref.defaultState)
  62. of select: newLit(pref.defaultOption)
  63. of input: newLit(pref.defaultInput)
  64. result.add nnkExprColonExpr.newTree(ident(pref.name), default)
  65. macro genUpdatePrefs*(): untyped =
  66. result = nnkStmtList.newTree()
  67. for pref in allPrefs():
  68. let ident = ident(pref.name)
  69. let value = nnkPrefix.newTree(ident("@"), newLit(pref.name))
  70. case pref.kind
  71. of checkbox:
  72. result.add quote do: prefs.`ident` = `value` == "on"
  73. of input:
  74. result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`))
  75. of select:
  76. let options = pref.options
  77. let default = pref.defaultOption
  78. result.add quote do:
  79. if `value` in `options`: prefs.`ident` = `value`
  80. else: prefs.`ident` = `default`
  81. result.add quote do:
  82. cache(prefs)