From 9e63f77e61a5519837b47785ffd3b65a126d30af Mon Sep 17 00:00:00 2001 From: luxick Date: Thu, 14 May 2020 19:01:01 +0200 Subject: [PATCH] Add local file provider. --- src/fileAccess.nim | 29 +++++++++++++++++++++++++++++ src/randopix.nim | 22 ++++++++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/fileAccess.nim diff --git a/src/fileAccess.nim b/src/fileAccess.nim new file mode 100644 index 0000000..de4739a --- /dev/null +++ b/src/fileAccess.nim @@ -0,0 +1,29 @@ +import os, sets, random +type + FileProvider* = ref object + exts: HashSet[string] + path*: string + files*: seq[string] + +proc load*(fp: FileProvider) = + ## Reload the file list + if fp.path == "": + return + + for file in walkDirRec(fp.path): + let split = splitFile(file) + if fp.exts.contains(split.ext): + fp.files.add(file) + + randomize() + shuffle(fp.files) + +proc next*(fp: FileProvider): string = + if fp.files.len < 1: + fp.load + result = fp.files[0] + fp.files.delete(0) + +proc newFileProvider*(path: string): FileProvider = + result = FileProvider(path: path, exts: [".png", ".jpg", ".jpeg"].toHashSet) + result.load \ No newline at end of file diff --git a/src/randopix.nim b/src/randopix.nim index ecb78ef..6e4d043 100644 --- a/src/randopix.nim +++ b/src/randopix.nim @@ -1,6 +1,7 @@ import httpClient, json, os, options, strformat import gintro/[gtk, glib, gobject, gio, gdkpixbuf] import argparse except run +import fileAccess const version = "0.1" @@ -9,7 +10,8 @@ const type Mode {.pure.} = enum Foxes = "foxes" ## Some nice foxes - Inspiro = "inspiro" ## Inspiring nonsense + Inspiro = "inspiro" ## Inspiring nonsense + File = "file" ## Images from a local path var client = newHttpClient() @@ -17,9 +19,11 @@ var imageWidget: Image fullscreen = true mode: Option[Mode] + fileProvider: FileProvider argParser = newParser("randopix"): help(fmt"Version {version} - Display random images from different sources") - option("-m", "--mode", help="foxes, inspiro, inspiro-xmas") + option("-m", "--mode", help="foxes, file, inspiro, inspiro-xmas") + option("-p", "--path", help="('file' mode only) Path to a directory with images") flag("-w", "--windowed", help="Do not start in fullscreen mode") proc downloadFox(): Pixbuf = @@ -30,6 +34,11 @@ proc downloadFox(): Pixbuf = discard loader.write(imageData) loader.getPixbuf() + +proc getLocalImage(): Pixbuf = + ## let the file provider serve another image + fileProvider.next.newPixbufFromFile + proc resizeImage(pixbuf: Pixbuf): Pixbuf = var wWidth, wHeight, width, height: int window.getSize(wWidth, wHeight) @@ -50,6 +59,8 @@ proc getImage(): Option[Pixbuf] = result = some(downloadFox()) of Mode.Inspiro: echo "Not Implemented" + of Mode.File: + result = some(getLocalImage()) proc updateImage(action: SimpleAction; parameter: Variant;) = let data = getImage(); @@ -100,6 +111,9 @@ proc parseArgs(): void = mode = some(parseEnum[Mode](opts.mode)) except ValueError: echo "Invaild image source: ", opts.mode + + if (mode.isSome and mode.get == Mode.File): + fileProvider = newFileProvider(opts.path) proc appActivate(app: Application) = parseArgs() @@ -109,7 +123,7 @@ proc appActivate(app: Application) = return window = newApplicationWindow(app) - window.title = "Randopics" + window.title = "randopix" window.setKeepAbove(true) window.setDefaultSize(600, 600) @@ -126,6 +140,6 @@ proc appActivate(app: Application) = window.showAll when isMainModule: - let app = newApplication("org.gtk.example") + let app = newApplication("org.luxick.randopix") connect(app, "activate", appActivate) discard run(app) \ No newline at end of file