Add argument parsing.

This commit is contained in:
2020-05-14 17:19:48 +02:00
parent 62bd31b9e6
commit 55bc9f3211
3 changed files with 78 additions and 30 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
bin/
bin/
.vscode/

View File

@@ -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"

View File

@@ -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()
discard run(app)