Add loaf, replace find
This commit is contained in:
parent
60cc0d26ef
commit
13e5ce7e22
3 changed files with 51 additions and 10 deletions
|
|
@ -5,7 +5,7 @@ author = "genotrance"
|
||||||
description = "C/C++ interop for Nim"
|
description = "C/C++ interop for Nim"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
bin = @["nimterop/toast"]
|
bin = @["nimterop/toast", "nimterop/loaf"]
|
||||||
installDirs = @["nimterop"]
|
installDirs = @["nimterop"]
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
|
|
@ -33,6 +33,9 @@ proc execTest(test: string, flags = "", runDocs = true) =
|
||||||
task buildTimeit, "build timer":
|
task buildTimeit, "build timer":
|
||||||
exec "nim c --hints:off -d:danger tests/timeit"
|
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":
|
task buildToast, "build toast":
|
||||||
execCmd("nim c --hints:off -d:danger nimterop/toast.nim")
|
execCmd("nim c --hints:off -d:danger nimterop/toast.nim")
|
||||||
|
|
||||||
|
|
@ -89,6 +92,7 @@ task test, "Test":
|
||||||
rmFile("tests/timeit.txt")
|
rmFile("tests/timeit.txt")
|
||||||
|
|
||||||
buildTimeitTask()
|
buildTimeitTask()
|
||||||
|
buildLoafTask()
|
||||||
buildToastTask()
|
buildToastTask()
|
||||||
|
|
||||||
basicTask()
|
basicTask()
|
||||||
|
|
|
||||||
|
|
@ -407,6 +407,9 @@ proc gitTags*(outdir: string): seq[string] =
|
||||||
if tag.len != 0:
|
if tag.len != 0:
|
||||||
result.add tag
|
result.add tag
|
||||||
|
|
||||||
|
proc loafExePath(): string =
|
||||||
|
currentSourcePath.parentDir.parentDir / ("loaf".addFileExt ExeExt)
|
||||||
|
|
||||||
proc findFiles*(file: string, dir: string, recurse = true, regex = false): seq[string] =
|
proc findFiles*(file: string, dir: string, recurse = true, regex = false): seq[string] =
|
||||||
## Find all matching files in the specified directory
|
## 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`
|
## Turn off recursive search with `recurse`
|
||||||
var
|
var
|
||||||
cmd = "nimgrep --follow --filenames --oneline --nocolor $1 \"$2\" $3"
|
cmd = loafExePath().quoteShell & " find --rexp $1 \"$2\" $3"
|
||||||
recursive = if recurse: "--recursive" else: ""
|
recursive = if recurse: "--recurse" else: ""
|
||||||
|
|
||||||
var
|
var
|
||||||
dir = dir
|
dir = dir
|
||||||
|
|
@ -435,13 +438,8 @@ proc findFiles*(file: string, dir: string, recurse = true, regex = false): seq[s
|
||||||
(files, ret) = execAction(cmd, die = false)
|
(files, ret) = execAction(cmd, die = false)
|
||||||
if ret == 0:
|
if ret == 0:
|
||||||
for line in files.splitLines():
|
for line in files.splitLines():
|
||||||
let f =
|
if line.len != 0:
|
||||||
if ": " in line:
|
result.add line
|
||||||
line.split(": ", maxsplit = 1)[1]
|
|
||||||
else:
|
|
||||||
""
|
|
||||||
if f.len != 0:
|
|
||||||
result.add f
|
|
||||||
|
|
||||||
proc findFile*(file: string, dir: string, recurse = true, first = false, regex = false): string =
|
proc findFile*(file: string, dir: string, recurse = true, first = false, regex = false): string =
|
||||||
## Find the file in the specified directory
|
## Find the file in the specified directory
|
||||||
|
|
|
||||||
39
nimterop/loaf.nim
Normal file
39
nimterop/loaf.nim
Normal 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 ..."
|
||||||
|
}
|
||||||
|
])
|
||||||
Loading…
Add table
Add a link
Reference in a new issue