Implement preference DSL

This commit is contained in:
Zed 2019-10-29 21:48:27 +01:00
parent 68cf8216b7
commit 4d2c68e9f5
1 changed files with 67 additions and 54 deletions

View File

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