Add local file provider.

This commit is contained in:
2020-05-14 19:01:01 +02:00
parent 7e07e38ab2
commit 9e63f77e61
2 changed files with 47 additions and 4 deletions

29
src/fileAccess.nim Normal file
View File

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