Add loaf, replace find

This commit is contained in:
Ganesh Viswanathan 2020-10-05 12:17:36 -05:00
commit 13e5ce7e22
3 changed files with 51 additions and 10 deletions

View file

@ -5,7 +5,7 @@ author = "genotrance"
description = "C/C++ interop for Nim"
license = "MIT"
bin = @["nimterop/toast"]
bin = @["nimterop/toast", "nimterop/loaf"]
installDirs = @["nimterop"]
# Dependencies
@ -33,6 +33,9 @@ proc execTest(test: string, flags = "", runDocs = true) =
task buildTimeit, "build timer":
exec "nim c --hints:off -d:danger tests/timeit"
task buildLoaf, "build loaf":
execCmd("nim c --hints:off -d:danger nimterop/loaf.nim")
task buildToast, "build toast":
execCmd("nim c --hints:off -d:danger nimterop/toast.nim")
@ -89,6 +92,7 @@ task test, "Test":
rmFile("tests/timeit.txt")
buildTimeitTask()
buildLoafTask()
buildToastTask()
basicTask()

View file

@ -407,6 +407,9 @@ proc gitTags*(outdir: string): seq[string] =
if tag.len != 0:
result.add tag
proc loafExePath(): string =
currentSourcePath.parentDir.parentDir / ("loaf".addFileExt ExeExt)
proc findFiles*(file: string, dir: string, recurse = true, regex = false): seq[string] =
## Find all matching files in the specified directory
##
@ -414,8 +417,8 @@ proc findFiles*(file: string, dir: string, recurse = true, regex = false): seq[s
##
## Turn off recursive search with `recurse`
var
cmd = "nimgrep --follow --filenames --oneline --nocolor $1 \"$2\" $3"
recursive = if recurse: "--recursive" else: ""
cmd = loafExePath().quoteShell & " find --rexp $1 \"$2\" $3"
recursive = if recurse: "--recurse" else: ""
var
dir = dir
@ -435,13 +438,8 @@ proc findFiles*(file: string, dir: string, recurse = true, regex = false): seq[s
(files, ret) = execAction(cmd, die = false)
if ret == 0:
for line in files.splitLines():
let f =
if ": " in line:
line.split(": ", maxsplit = 1)[1]
else:
""
if f.len != 0:
result.add f
if line.len != 0:
result.add line
proc findFile*(file: string, dir: string, recurse = true, first = false, regex = false): string =
## Find the file in the specified directory

39
nimterop/loaf.nim Normal file
View file

@ -0,0 +1,39 @@
import system except find
import os
import cligen
import strutils except find
import regex except find
proc findRec(dir: string, pattern: string | Regex, recurse: bool) =
for kind, path in walkDir(dir):
if kind in [pcDir, pcLinkToDir]:
if recurse: findRec(path, pattern, recurse)
elif pattern in path:
echo path.absolutePath()
proc find(recurse = false, rexp = false, args: seq[string]) =
var
pat = ""
rpat: Regex
for arg in args:
if not arg.startsWith("-"):
if dirExists(arg):
if rexp:
findRec(arg, rpat, recurse)
else:
findRec(arg, pat, recurse)
else:
pat = arg
if rexp: rpat = re(arg)
when isMainModule:
dispatchMulti([
find, help = {
"recurse": "recursive search",
"rexp": "patterns are regular expressions",
"args": "pattern1 dir1 dir2 pattern2 dir3 ..."
}
])