Add argument parsing.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
bin/
|
bin/
|
||||||
|
.vscode/
|
||||||
@@ -9,10 +9,13 @@ srcDir = "src"
|
|||||||
bin = @["randopics"]
|
bin = @["randopics"]
|
||||||
|
|
||||||
# Dependencies
|
# 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":
|
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":
|
task release, "Compile release version":
|
||||||
exec fmt"nim c -d:release --out:bin/{version}/randopics src/randopics.nim"
|
exec fmt"nim c -d:release --out:bin/{version}/randopics src/randopics.nim"
|
||||||
|
|||||||
@@ -1,15 +1,28 @@
|
|||||||
import httpClient, json
|
import httpClient, json, os, options, strformat
|
||||||
import gintro/[gtk, glib, gobject, gio, gdkpixbuf]
|
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
|
var
|
||||||
client = newHttpClient()
|
client = newHttpClient()
|
||||||
window: ApplicationWindow
|
window: ApplicationWindow
|
||||||
|
imageWidget: Image
|
||||||
fullscreen = true
|
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
|
proc downloadFox(): Pixbuf =
|
||||||
floofUrl = "https://randomfox.ca/floof/"
|
|
||||||
|
|
||||||
proc downloadImage(): Pixbuf =
|
|
||||||
let urlData = client.getContent(floofUrl)
|
let urlData = client.getContent(floofUrl)
|
||||||
let info = parseJson(urlData)
|
let info = parseJson(urlData)
|
||||||
let imageData = client.getContent(info["image"].getStr)
|
let imageData = client.getContent(info["image"].getStr)
|
||||||
@@ -30,10 +43,19 @@ proc resizeImage(pixbuf: Pixbuf): Pixbuf =
|
|||||||
|
|
||||||
pixbuf.scaleSimple(width, height, InterpType.bilinear)
|
pixbuf.scaleSimple(width, height, InterpType.bilinear)
|
||||||
|
|
||||||
proc updateImage(action: SimpleAction; parameter: Variant; widget: Image) =
|
proc getImage(): Option[Pixbuf] =
|
||||||
var pixbuf = downloadImage()
|
if mode.isSome:
|
||||||
pixbuf = pixbuf.resizeImage()
|
case mode.get
|
||||||
widget.setFromPixbuf(pixbuf)
|
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) =
|
proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: ApplicationWindow) =
|
||||||
if fullscreen:
|
if fullscreen:
|
||||||
@@ -45,22 +67,15 @@ proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: Applicat
|
|||||||
proc quit(action: SimpleAction; parameter: Variant; app: Application) =
|
proc quit(action: SimpleAction; parameter: Variant; app: Application) =
|
||||||
app.quit()
|
app.quit()
|
||||||
|
|
||||||
proc appActivate(app: Application) =
|
proc applyStyle(window: Window) =
|
||||||
window = newApplicationWindow(app)
|
|
||||||
window.title = "Randopics"
|
|
||||||
window.setKeepAbove(true)
|
|
||||||
let cssProvider = newCssProvider()
|
let cssProvider = newCssProvider()
|
||||||
let data = "window { background: black; }"
|
let data = "window { background: black; }"
|
||||||
discard cssProvider.loadFromData(data)
|
discard cssProvider.loadFromData(data)
|
||||||
let styleContext = window.getStyleContext()
|
let styleContext = window.getStyleContext()
|
||||||
styleContext.addProvider(cssProvider, STYLE_PROVIDER_PRIORITY_USER)
|
styleContext.addProvider(cssProvider, STYLE_PROVIDER_PRIORITY_USER)
|
||||||
|
|
||||||
let imageWidget = newImage()
|
proc connectSignals(app: Application) =
|
||||||
window.add(imageWidget)
|
## Connect th GTK signals to the procs
|
||||||
|
|
||||||
if fullscreen:
|
|
||||||
window.fullscreen
|
|
||||||
|
|
||||||
let fullscreenAction = newSimpleAction("fullscreen")
|
let fullscreenAction = newSimpleAction("fullscreen")
|
||||||
discard fullscreenAction.connect("activate", toggleFullscreen, window)
|
discard fullscreenAction.connect("activate", toggleFullscreen, window)
|
||||||
app.setAccelsForAction("win.fullscreen", "F")
|
app.setAccelsForAction("win.fullscreen", "F")
|
||||||
@@ -72,16 +87,45 @@ proc appActivate(app: Application) =
|
|||||||
window.actionMap.addAction(quitAction)
|
window.actionMap.addAction(quitAction)
|
||||||
|
|
||||||
let updateImageAction = newSimpleAction("update")
|
let updateImageAction = newSimpleAction("update")
|
||||||
discard updateImageAction.connect("activate", updateImage, imageWidget)
|
discard updateImageAction.connect("activate", updateImage)
|
||||||
app.setAccelsForAction("win.update", "U")
|
app.setAccelsForAction("win.update", "U")
|
||||||
window.actionMap.addAction(updateImageAction)
|
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
|
window.showAll
|
||||||
|
|
||||||
proc main =
|
when isMainModule:
|
||||||
let app = newApplication("org.gtk.example")
|
let app = newApplication("org.gtk.example")
|
||||||
connect(app, "activate", appActivate)
|
connect(app, "activate", appActivate)
|
||||||
discard run(app)
|
discard run(app)
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
main()
|
|
||||||
Reference in New Issue
Block a user