Zed пре 6 година
родитељ
комит
4d2c68e9f5
1 измењених фајлова са 80 додато и 67 уклоњено
  1. 80 67
      src/prefs_impl.nim

+ 80 - 67
src/prefs_impl.nim

@@ -7,73 +7,86 @@ type
   Pref* = object
     name*: string
     label*: string
-    case kind*: PrefKind
-    of checkbox:
-      defaultState*: bool
-    of select:
-      defaultOption*: string
-      options*: seq[string]
-    of input:
-      defaultInput*: string
-      placeholder*: string
-
-# TODO: write DSL to simplify this
-const prefList*: OrderedTable[string, seq[Pref]] = {
-  "Privacy": @[
-    Pref(kind: input, name: "replaceTwitter",
-         label: "Replace Twitter links with Nitter (blank to disable)",
-         defaultInput: "nitter.net", placeholder: "Nitter hostname"),
-
-    Pref(kind: input, name: "replaceYouTube",
-         label: "Replace YouTube links with Invidious (blank to disable)",
-         defaultInput: "invidio.us", placeholder: "Invidious hostname")
-  ],
-
-  "Media": @[
-    Pref(kind: checkbox, name: "mp4Playback",
-         label: "Enable mp4 video playback",
-         defaultState: true),
-
-    Pref(kind: checkbox, name: "hlsPlayback",
-         label: "Enable hls video streaming (requires JavaScript)",
-         defaultState: false),
-
-    Pref(kind: checkbox, name: "proxyVideos",
-         label: "Proxy video streaming through the server (might be slow)",
-         defaultState: true),
-
-    Pref(kind: checkbox, name: "muteVideos",
-         label: "Mute videos by default",
-         defaultState: false),
-
-    Pref(kind: checkbox, name: "autoplayGifs", label: "Autoplay gifs",
-         defaultState: true)
-  ],
-
-  "Display": @[
-    Pref(kind: select, name: "theme", label: "Theme",
-         defaultOption: "Nitter"),
-
-    Pref(kind: checkbox, name: "hideTweetStats",
-         label: "Hide tweet stats (replies, retweets, likes)",
-         defaultState: false),
-
-    Pref(kind: checkbox, name: "hideBanner", label: "Hide profile banner",
-         defaultState: false),
-
-    Pref(kind: checkbox, name: "stickyProfile",
-         label: "Make profile sidebar stick to top",
-         defaultState: true),
-
-    Pref(kind: checkbox, name: "hidePins",
-         label: "Hide pinned tweets",
-         defaultState: false),
-
-    Pref(kind: checkbox, name: "hideReplies",
-         label: "Hide tweet replies",
-         defaultState: false)
-  ]
-}.toOrderedTable
+    kind*: PrefKind
+    options*: seq[string]
+    placeholder*: string
+    defaultState*: bool
+    defaultOption*: string
+    defaultInput*: string
+
+macro genPrefs(prefDsl: untyped) =
+  var table = nnkTableConstr.newTree()
+  for category in prefDsl:
+    table.add nnkExprColonExpr.newTree(newLit($category[0]))
+    table[^1].add nnkPrefix.newTree(newIdentNode("@"), nnkBracket.newTree())
+    for pref in category[1]:
+      let
+        name = newLit($pref[0])
+        kind = pref[1]
+        label = pref[3][0]
+        default = pref[2]
+        defaultField =
+          case parseEnum[PrefKind]($kind)
+          of checkbox: ident("defaultState")
+          of select: ident("defaultOption")
+          of input: ident("defaultInput")
+
+      var newPref = quote do:
+        Pref(kind: `kind`, name: `name`, label: `label`, `defaultField`: `default`)
+
+      for node in pref[3]:
+        if node.kind == nnkCall:
+          newPref.add nnkExprColonExpr.newTree(node[0], node[1][0])
+      table[^1][1][1].add newPref
+
+  let name = ident("prefList")
+  result = quote do:
+    const `name`* = toOrderedTable(`table`)
+
+genPrefs:
+  Privacy:
+    replaceTwitter(input, "nitter.net"):
+      "Replace Twitter links with Nitter (blank to disable)"
+      placeholder: "Nitter hostname"
+
+    replaceYouTube(input, "invidio.us"):
+      "Replace YouTube links with Invidious (blank to disable)"
+      placeholder: "Invidious hostname"
+
+  Media:
+    mp4Playback(checkbox, true):
+      "Enable mp4 video playback"
+
+    hlsPlayback(checkbox, false):
+      "Enable hls video streaming (requires JavaScript)"
+
+    proxyVideos(checkbox, true):
+      "Proxy video streaming through the server (might be slow)"
+
+    muteVideos(checkbox, false):
+      "Mute videos by default"
+
+    autoplayGifs(checkbox, true):
+      "Autoplay gifs"
+
+  Display:
+    theme(select, "Nitter"):
+      "Theme"
+
+    stickyProfile(checkbox, true):
+      "Make profile sidebar stick to top"
+
+    hideTweetStats(checkbox, false):
+      "Hide tweet stats (replies, retweets, likes)"
+
+    hideBanner(checkbox, false):
+      "Hide profile banner"
+
+    hidePins(checkbox, false):
+      "Hide pinned tweets"
+
+    hideReplies(checkbox, false):
+      "Hide tweet replies"
 
 iterator allPrefs*(): Pref =
   for k, v in prefList: