-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfup.nim
60 lines (54 loc) · 1.79 KB
/
fup.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os, terminal
from strutils import contains
const
maxInt32 = high(int32)
template match(s: string, filter: string, exact: bool): bool =
(exact and s == filter) or (not exact and s.contains(filter))
iterator walk(dir: string, kinds = {pcFile, pcDir}): (string, string, string) =
for (kind, path) in walkDir(dir):
if kind in kinds:
yield splitFile(path)
proc fup0(patterns: seq[string],
exact = false, full = false, file = false, dir = false, incl = false,
level = maxInt32, num = maxInt32, workdir = "") =
var
filters = if patterns.len == 0: @[""] else: patterns
kinds = if file: {pcFile} elif dir: {pcDir} else: {pcFile, pcDir}
depth = 0
matches = 0
workdir = if workdir.len == 0: getCurrentDir() else: workdir
for filter in filters:
for dir in parentDirs(workdir, inclusive = incl):
depth.inc
if depth <= level:
for (dir, name, ext) in walk(dir, kinds):
var path = if full: dir / name & ext else: name & ext
if path.match(filter, exact) and matches <= num:
matches.inc
echo dir / name & ext
when isMainModule:
import cligen
dispatch(fup0, "fup",
short = {
"workdir": 'w',
"full": 'F',
"file": 'f',
"dir": 'd',
"incl": 'i',
"num": 'n',
"level": 'l'
},
help = {
"exact": "match name path exactly",
"full": "match full path",
"full": "match full path",
"dir": "only match dirs",
"file": "only match files",
"incl": "include workdir",
"level": "limit directory traversal",
"num": "max results",
"help-syntax": "CLIGEN-NOHELP",
},
doc = " find paths in parent directories",
noHdr = true
)