nitter/src/prefs_impl.nim

145 lines
3.8 KiB
Nim
Raw Normal View History

2019-08-17 19:49:41 +00:00
import macros, tables, strutils, xmltree
type
PrefKind* = enum
checkbox, select, input
Pref* = object
name*: string
label*: string
2019-10-29 20:48:27 +00:00
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"
2019-08-17 19:49:41 +00:00
iterator allPrefs*(): Pref =
2019-08-17 19:49:41 +00:00
for k, v in prefList:
for pref in v:
yield pref
macro genDefaultPrefs*(): untyped =
result = nnkObjConstr.newTree(ident("Prefs"))
for pref in allPrefs():
let default =
case pref.kind
of checkbox: newLit(pref.defaultState)
of select: newLit(pref.defaultOption)
of input: newLit(pref.defaultInput)
result.add nnkExprColonExpr.newTree(ident(pref.name), default)
macro genUpdatePrefs*(): untyped =
result = nnkStmtList.newTree()
for pref in allPrefs():
let ident = ident(pref.name)
let value = nnkPrefix.newTree(ident("@"), newLit(pref.name))
case pref.kind
of checkbox:
result.add quote do: prefs.`ident` = `value` == "on"
of input:
result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`))
of select:
2019-10-23 09:48:08 +00:00
let name = pref.name
2019-08-17 19:49:41 +00:00
let options = pref.options
let default = pref.defaultOption
result.add quote do:
2019-10-23 09:48:08 +00:00
if `name` == "theme": prefs.`ident` = `value`
elif `value` in `options`: prefs.`ident` = `value`
2019-08-17 19:49:41 +00:00
else: prefs.`ident` = `default`
result.add quote do:
cache(prefs)
2019-09-08 11:01:20 +00:00
macro genPrefsType*(): untyped =
let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
result = quote do:
type `name` = object
id* {.pk, ro.}: int
for pref in allPrefs():
result[0][2][2].add nnkIdentDefs.newTree(
nnkPostfix.newTree(ident("*"), ident(pref.name)),
(case pref.kind
of checkbox: ident("bool")
of input, select: ident("string")),
newEmptyNode())