Add local file provider.
This commit is contained in:
29
src/fileAccess.nim
Normal file
29
src/fileAccess.nim
Normal 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
|
||||
Reference in New Issue
Block a user