Skip to content

Commit

Permalink
add more filters
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Jan 24, 2023
1 parent 2b970a0 commit c56dded
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 24 deletions.
Empty file added examples/Awesome.txt
Empty file.
Empty file added examples/Bowesome.txt
Empty file.
1 change: 1 addition & 0 deletions examples/Hello.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello World
1 change: 1 addition & 0 deletions examples/Whatever.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum
8 changes: 5 additions & 3 deletions src/find.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
# https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/Finder/Finder.php#method_directories
# https://symfony.com/doc/current/components/finder.html#files-or-directories
import std/[os, tables, times, re, strutils, sequtils,
math, asyncdispatch, asyncftpclient, net]
math, algorithm, asyncdispatch, asyncftpclient, net]
# import std/posix except Time
import pkg/libssh2

export algorithm.SortOrder, times

type
FinderDriverType* {.pure.} = enum
LOCAL, SSH, FTP
Expand Down Expand Up @@ -160,8 +162,8 @@ proc get*(finder: Finder): Results =
result = finder.results

when isMainModule:
let res = finder("./examples/", driver = LOCAL).name("*.txt").size(> 15.bytes, < 20.bytes).get
for f in res.files():
let res = finder("./examples/", driver = LOCAL).name("*.txt").get
for f in files(res.only(1.days).sortBySize()):
echo f.getPath()
echo f.getSize()
# echo f.getInfo()
25 changes: 6 additions & 19 deletions src/find/private/criterias.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ proc mb*(i: float): FSize = i.megabytes
proc gb*(i: float): FSize = i.gigabytes
proc tb*(i: float): FSize = i.terabytes

proc KB*(i: float): FSize = i.kilobytes
proc MB*(i: float): FSize = i.megabytes
proc GB*(i: float): FSize = i.gigabytes
proc TB*(i: float): FSize = i.terabytes

proc size*[F: Finder](finder: F, fs: FSize): F =
finder.criteria.size.min = fs
finder.criteria.bySize = true
Expand All @@ -143,22 +148,4 @@ proc checkFileSize(finder: Finder, file: FileFinder): bool =
of LT: fs < min.bytes
of LTE: fs <= min.bytes
of GT: fs > min.bytes
of GTE: fs >= min.bytes

proc sort*[F: Finder](finder: F, sortType: SortType): F =
## Sorts files and directories

proc sortByName*[F: Finder](finder: F): F =
## Sorts files and directories by name

proc sortByType*[F: Finder](finder: F): F =
## Sorts files and directories by type
##(directories before files), then by name

proc sortByAccessedTime*[F: Finder](finder: F): F =
## Sorts files and directories by the
## last accessed time This is the time that
## the file was last accessed, read or written to

proc sortByModifiedTime*[F: Finder](finder: F): F =
## Sorts files and directories by the last modified time
of GTE: fs >= min.bytes
9 changes: 8 additions & 1 deletion src/find/private/file.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ proc getInfo*(file: FileFinder): FileInfo =
## https://nim-lang.org/docs/os.html#FileInfo
result = file.info

proc getName*(file: FileFinder): string =
result = file.path.extractFilename

proc getPath*(file: FileFinder): string =
## Returns path on disk for given FileFinder
result = file.path
Expand Down Expand Up @@ -37,4 +40,8 @@ proc getLastAccessTime*(file: FileFinder): Time =

proc getLastModificationTime*(file: FileFinder): Time =
## Returns the file's last read or write access time
result = file.info.lastWriteTime
result = file.info.lastWriteTime

proc getCreationTime*(file: FileFinder): Time =
## Returns the creation time
result = file.info.creationTime
63 changes: 62 additions & 1 deletion src/find/private/filters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,65 @@ proc len*(res: Results): int =

proc count*(res: Results): int =
## An alias for `len`
result = res.len
result = res.len

proc sortByName*[R: Results](res: R, order: algorithm.SortOrder = Ascending): R =
## Sorts files and directories by name
let s = proc(x, y: (string, FileFinder)): int =
cmp(x[1].getName, y[1].getName)
res.fileResults.sort(s, order)
result = res

proc sortByType*[R: Results](res: R): R =
## Sorts files and directories by type
##(directories before files), then by name

proc sortByAccessedTime*[R: Results](res: R, order: algorithm.SortOrder = Descending): R =
## Sorts files and directories by the
## last accessed time This is the time that
## the file was last accessed, read or written to
let s = proc(x, y: (string, FileFinder)): int =
cmp(x[1].getLastAccessTime, y[1].getLastAccessTime)
res.fileResults.sort(s, order)
result = res

proc sortByModifiedTime*[R: Results](res: R, order: algorithm.SortOrder = Descending): R =
## Sorts files and directories by the last modified time
let s = proc(x, y: (string, FileFinder)): int =
cmp(x[1].getLastModificationTime, y[1].getLastModificationTime)
res.fileResults.sort(s, order)
result = res

proc sortByCreationTime*[R: Results](res: R, order: algorithm.SortOrder = Descending): R =
## Sorts files and directories by creation time
let s = proc(x, y: (string, FileFinder)): int =
cmp(x[1].getCreationTime, y[1].getCreationTime)
res.fileResults.sort(s, order)
result = res

proc sortBySize*[R: Results](res: R, order: algorithm.SortOrder = Descending): R =
## Sorts files and directories by size
let s = proc(x, y: (string, FileFinder)): int =
cmp(x[1].getSize, y[1].getSize)
res.fileResults.sort(s, order)
result = res

template Today*(): DateTime = now()
template Yesterday*(): DateTime = now() - 1.days

proc only*[R: Results](res: R, dateTime: DateTime): R =
## Refine the current Results table by DateTime.
var i = 0
var paths: seq[string]
let dateFormat = "yyyy-MM-dd"
for p in res.fileResults.keys:
paths.add p
for p in paths:
let createdTime = format(res.fileResults[p].getCreationTime, dateFormat)
let needTime = format(dateTime.toTime, dateFormat)
if createdTime != needTime:
res.fileResults.del(p)
result = res

proc only*[R: Results](res: R, interval: TimeInterval): R =
result = res.only(now() - interval)

0 comments on commit c56dded

Please sign in to comment.