From 55bc9f3211d2d83ef2fc8e4a8d003cd84610e5bd Mon Sep 17 00:00:00 2001 From: luxick Date: Thu, 14 May 2020 17:19:48 +0200 Subject: [PATCH] Add argument parsing. --- .gitignore | 3 +- randopics.nimble | 7 +++- src/randopics.nim | 98 ++++++++++++++++++++++++++++++++++------------- 3 files changed, 78 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 6dd29b7..b497ff1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -bin/ \ No newline at end of file +bin/ +.vscode/ \ No newline at end of file diff --git a/randopics.nimble b/randopics.nimble index cbbb788..9b7262a 100644 --- a/randopics.nimble +++ b/randopics.nimble @@ -9,10 +9,13 @@ srcDir = "src" bin = @["randopics"] # Dependencies -requires "nim >= 1.0.0", "gintro <= 0.5.5" +requires "nim >= 1.0.0", "gintro <= 0.5.5", "argparse >=0.10.1" task debug, "Compile debug version": - exec "nim c --out:bin/randopics src/randopics.nim" + exec "nim c -d:debug --debugger:native --out:bin/randopics src/randopics.nim" + +task r, "Compile and run": + exec "nim c -r --out:bin/randopics src/randopics.nim" task release, "Compile release version": exec fmt"nim c -d:release --out:bin/{version}/randopics src/randopics.nim" diff --git a/src/randopics.nim b/src/randopics.nim index 289ee11..fabea77 100644 --- a/src/randopics.nim +++ b/src/randopics.nim @@ -1,15 +1,28 @@ -import httpClient, json +import httpClient, json, os, options, strformat import gintro/[gtk, glib, gobject, gio, gdkpixbuf] +import argparse except run + +const + version = "0.1" + floofUrl = "https://randomfox.ca/floof/" + +type + Mode {.pure.} = enum + Foxes = "foxes" ## Some nice foxes + Inspiro = "inspiro" ## Inspiring nonsense var client = newHttpClient() window: ApplicationWindow + imageWidget: Image fullscreen = true + mode: Option[Mode] + argParser = newParser("randopix"): + help(fmt"Version {version} - Display random images from different sources") + option("-m", "--mode", help="One of these: foxes, inspiro, inspiro-xmas") + flag("-w", "--windowed", help="Do not start in fullscreen mode") -const - floofUrl = "https://randomfox.ca/floof/" - -proc downloadImage(): Pixbuf = +proc downloadFox(): Pixbuf = let urlData = client.getContent(floofUrl) let info = parseJson(urlData) let imageData = client.getContent(info["image"].getStr) @@ -30,10 +43,19 @@ proc resizeImage(pixbuf: Pixbuf): Pixbuf = pixbuf.scaleSimple(width, height, InterpType.bilinear) -proc updateImage(action: SimpleAction; parameter: Variant; widget: Image) = - var pixbuf = downloadImage() - pixbuf = pixbuf.resizeImage() - widget.setFromPixbuf(pixbuf) +proc getImage(): Option[Pixbuf] = + if mode.isSome: + case mode.get + of Mode.Foxes: + result = some(downloadFox()) + of Mode.Inspiro: + echo "Not Implemented" + +proc updateImage(action: SimpleAction; parameter: Variant;) = + let data = getImage(); + if (data.isNone): return + var pixbuf = data.get().resizeImage() + imageWidget.setFromPixbuf(pixbuf) proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: ApplicationWindow) = if fullscreen: @@ -44,23 +66,16 @@ proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: Applicat proc quit(action: SimpleAction; parameter: Variant; app: Application) = app.quit() - -proc appActivate(app: Application) = - window = newApplicationWindow(app) - window.title = "Randopics" - window.setKeepAbove(true) + +proc applyStyle(window: Window) = let cssProvider = newCssProvider() let data = "window { background: black; }" discard cssProvider.loadFromData(data) let styleContext = window.getStyleContext() styleContext.addProvider(cssProvider, STYLE_PROVIDER_PRIORITY_USER) - - let imageWidget = newImage() - window.add(imageWidget) - - if fullscreen: - window.fullscreen - + +proc connectSignals(app: Application) = + ## Connect th GTK signals to the procs let fullscreenAction = newSimpleAction("fullscreen") discard fullscreenAction.connect("activate", toggleFullscreen, window) app.setAccelsForAction("win.fullscreen", "F") @@ -72,16 +87,45 @@ proc appActivate(app: Application) = window.actionMap.addAction(quitAction) let updateImageAction = newSimpleAction("update") - discard updateImageAction.connect("activate", updateImage, imageWidget) + discard updateImageAction.connect("activate", updateImage) app.setAccelsForAction("win.update", "U") window.actionMap.addAction(updateImageAction) +proc parseArgs(): void = + ## Parse and apply options from the command line + let opts = argparser.parse(commandLineParams()) + fullscreen = not opts.windowed + if (opts.mode != ""): + try: + mode = some(parseEnum[Mode](opts.mode)) + except ValueError: + echo "Invaild image source: ", opts.mode + +proc appActivate(app: Application) = + parseArgs() + # No mode was given, exit and display the help text + if (mode.isNone): + echo argParser.help + return + + window = newApplicationWindow(app) + window.title = "Randopics" + window.setKeepAbove(true) + window.setDefaultSize(600, 600) + + # Custom styling for e.g. the background color + window.applyStyle + + imageWidget = newImage() + window.add(imageWidget) + + if fullscreen: + window.fullscreen + + app.connectSignals window.showAll -proc main = +when isMainModule: let app = newApplication("org.gtk.example") connect(app, "activate", appActivate) - discard run(app) - -when isMainModule: - main() \ No newline at end of file + discard run(app) \ No newline at end of file