nitter/src/prefs_impl.nim

124 lines
3.5 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
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
2019-09-13 09:04:57 +00:00
const prefList*: OrderedTable[string, seq[Pref]] = {
2019-08-17 19:49:41 +00:00
"Privacy": @[
Pref(kind: input, name: "replaceTwitter",
label: "Replace Twitter links with Nitter (blank to disable)",
defaultInput: "nitter.net", placeholder: "Nitter hostname"),
2019-08-17 19:49:41 +00:00
Pref(kind: input, name: "replaceYouTube",
label: "Replace YouTube links with Invidious (blank to disable)",
defaultInput: "invidio.us", placeholder: "Invidious hostname")
],
"Media": @[
2019-08-19 01:28:04 +00:00
Pref(kind: checkbox, name: "mp4Playback",
2019-09-13 09:04:57 +00:00
label: "Enable mp4 video playback",
defaultState: true),
2019-08-19 01:28:04 +00:00
Pref(kind: checkbox, name: "hlsPlayback",
label: "Enable hls video streaming (requires JavaScript)",
defaultState: false),
2019-08-19 18:53:47 +00:00
Pref(kind: checkbox, name: "proxyVideos",
label: "Proxy video streaming through the server (might be slow)",
2019-08-22 22:11:47 +00:00
defaultState: true),
2019-08-19 18:53:47 +00:00
2019-08-19 01:28:04 +00:00
Pref(kind: checkbox, name: "muteVideos",
label: "Mute videos by default",
2019-08-17 19:49:41 +00:00
defaultState: false),
Pref(kind: checkbox, name: "autoplayGifs", label: "Autoplay gifs",
defaultState: true)
],
"Display": @[
2019-10-23 09:48:08 +00:00
Pref(kind: select, name: "theme", label: "Theme",
2019-10-27 12:02:22 +00:00
defaultOption: "Nitter"),
2019-10-23 09:48:08 +00:00
2019-08-17 19:49:41 +00:00
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)
]
2019-09-13 09:04:57 +00:00
}.toOrderedTable
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())