import os import osproc import strutils import sequtils import random import ../common import ../parser import ../output var UNSPLASH_KEY = "" var BG_DIR = "/tmp/" var LAST_FILE = "" var LAST = "" var GET_FROM_UNSPLASH = false const UNSPLASH_URL = "https://api.unsplash.com/photos/random?query=$QUERY&orientation=landscape" type Note* = object urgency*: Urgency title*: string content*: string timeout*: int Urgency* = enum Normal = "normal" Low = "low" Urgent = "urgent" Critical = "critical" proc newNote*(): Note = return Note(urgency: Normal, title: "Notification", content: "Hello, I am a notifications", timeout: 2000) proc `$`*(n: Note): string = let str = "notify-send -u $U $T $C -t $N" .replace("$U", $n.urgency) .replace("$T", n.title.escape) .replace("$C", n.content.escape) .replace("$N", $n.timeout) return str proc send(n: Note) = discard execCmdEx($n) proc getFromUnsplash(q: var string): string = createDir(BG_DIR & "/unsplash") echo "Getting from Unsplash" q = q.replace(" ","%20") let uri = UNSPLASH_URL.replace("$QUERY",q) proc getFiles(dir: string): seq[string] = var files: seq[string] = @[] for file in walkDir(dir): if file.path.endsWith(".jpg"): files.add(file.path) elif file.kind == pcDir: files = files.concat(getFiles(file.path)) return files proc getLast() = LAST = readFile(LAST_FILE).strip() proc getImageFromDir(): string = echo "Getting Random file from " & BG_DIR var img_files = getFiles(BG_DIR).filter(proc(f: string): bool = f != LAST) img_files.shuffle() let img_file = img_files[0] echo "Found : ", img_file proc setLast() = var n: Note = newNote() n.title = "Setting Background to Last" n.content = LAST n.send() let feh = "feh --bg-fill " & LAST.escape echo feh discard execCmdEx(feh) proc getDesign(): Info = var data = newInfo("Wallpapurr") return data proc queryPrompt(): string = let data = getDesign() let output = data.outputData() return output proc go*() = var args = parseWallpapurrArgs() UNSPLASH_KEY = myConfig.unsplash_key BG_DIR = myConfig.bg_dir LAST_FILE = BG_DIR & "/last.txt" getLast() var img = "" if args.query != "" or args.from_unsplash: if args.query == "": args.query = queryPrompt() echo "Query: ", args.query img = getFromUnsplash(args.query) elif args.last: setLast() else: img = getImageFromDir() echo img