-
Notifications
You must be signed in to change notification settings - Fork 4
/
cff.nim
206 lines (187 loc) · 5.36 KB
/
cff.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#
# cff -- complete the fucking filename tool
# (c) Copyright 2017 Andreas Rumpf
#
# See the file "license.txt", included in this
# distribution, for details about the copyright.
import os, strutils, parseutils
from osproc import quoteShell
type
PrefixMatch* {.pure.} = enum
None, ## no prefix detected
Abbrev ## prefix is an abbreviation of the symbol
Substr, ## prefix is a substring of the symbol
Prefix, ## prefix does match the symbol
Exact, ## exact match
proc prefixMatch*(p, s: string): PrefixMatch =
template eq(a, b): bool = a.toLowerAscii == b.toLowerAscii
if p.len > s.len: return PrefixMatch.None
var i = 0
let L = s.len
# check for prefix/contains:
while i < L:
if s[i] == '_': inc i
if eq(s[i], p[0]):
var ii = i+1
var jj = 1
while ii < L and jj < p.len:
if p[jj] == '_': inc jj
if s[ii] == '_': inc ii
if not eq(s[ii], p[jj]): break
inc ii
inc jj
if jj >= p.len:
if i == 0:
if ii >= s.len: return PrefixMatch.Exact
return PrefixMatch.Prefix
else: return PrefixMatch.Substr
inc i
# check for abbrev:
if eq(s[0], p[0]) and p[0] in {'A'..'Z', '0'..'9'}:
i = 1
var j = 1
while i < s.len:
if s[i] in {'_', '-'} and i < s.len-1:
if j < p.len and eq(p[j], s[i+1]): inc j
else: return PrefixMatch.None
if s[i] in {'A'..'Z'} and s[i-1] notin {'A'..'Z'}:
if j < p.len and eq(p[j], s[i]): inc j
else: return PrefixMatch.None
inc i
if j >= p.len:
return PrefixMatch.Abbrev
else:
return PrefixMatch.None
return PrefixMatch.None
when defined(testing):
import macros
macro check(val, body: untyped): untyped =
result = newStmtList()
expectKind body, nnkStmtList
for b in body:
expectKind b, nnkPar
expectLen b, 2
let p = b[0]
let s = b[1]
result.add quote do:
echo prefixMatch(`p`, `s`) == `val`
check PrefixMatch.Exact:
("abc", "abc")
("abc", "a_b_c")
check PrefixMatch.Prefix:
("a", "abc")
("xyz", "X_yzzzZe")
check PrefixMatch.Substr:
("b", "abc")
("abc", "fooabcabc")
("abC", "foo_AB_c")
check PrefixMatch.Abbrev:
("ABC", "AxxxBxxxCxxx")
("Xyz", "X_yabcZe")
("Abc", "Axxx-bwana-clak")
check PrefixMatch.None:
("foobar", "afkslfjd_as")
("xyz", "X_yuuZuuZe")
("ru", "remotes")
("abc", "Axxx-bwana-clak")
type
Cand = object
m: PrefixMatch
d: int
p: string
Keyw = object
name, subdir: string
parts: seq[string]
proc search(path: string; k: var Keyw; results: var seq[Cand]; partsLen: int;
first: bool; depth=0) =
for pc, f in os.walkDir(path, relative=true):
let match = prefixMatch(k.name, f)
if match != PrefixMatch.None and partsLen == 0:
if first and pc in {pcDir, pcLinkToDir}:
discard "first command line part cannot be a directory"
else:
results.add(Cand(m: match, d: depth, p: path / f))
case pc
of pcFile, pcLinkToFile:
discard
of pcDir, pcLinkToDir:
let idx = find(k.parts, f)
if idx >= 0:
k.parts[idx] = "" # disable for recursion
search(path / f, k, results, partsLen-1, first, depth+1)
k.parts[idx] = f
else:
search(path / f, k, results, partsLen, first, depth+1)
proc pickBest*[T](x: openArray[T]; cmp: proc(a, b: T): int): int =
## add to algorithm stdlib?
result = 0
for i in 1..high(x):
if cmp(x[i], x[result]) < 0:
result = i
let curdir = getCurrentDir()
proc splitKeyw(s: string): Keyw =
result = Keyw(name: s, subdir: "", parts: @[])
var a = s.split({'/', '\\'})
var start = 0
while start < a.len:
if not a[start].startsWith"..": break
var x: int = 0
discard a[start].parseInt(x, 2)
if x > 0:
for i in 1..x:
if result.subdir.len > 0: result.subdir.add DirSep
result.subdir.add ".."
else:
if result.subdir.len > 0: result.subdir.add DirSep
result.subdir.add a[start]
inc start
if start == 0: result.subdir = curdir
for i in start..a.len-2:
result.parts.add a[i]
result.name = a[^1]
proc complete(s: string; first: bool): string =
if not s.endsWith("_"): return quoteShell(s)
var cands: seq[Cand] = @[]
var k = splitKeyw(s.substr(0, s.len-2))
search(k.subdir, k, cands, k.parts.len, first)
if cands.len == 0:
quit "cannot expand: " & s
let best = pickBest(cands, proc (a, b: Cand): int =
result = ord(b.m) - ord(a.m)
if result != 0: return result
result = a.d - b.d
if result != 0: return result
result = a.p.len - b.p.len
if result != 0: return result
)
result = cands[best].p
if result.startsWith(curdir & DirSep):
result = result.substr(curdir.len+1)
return quoteShell(result)
proc main =
let cnt = os.paramCount()
if cnt == 0:
quit "Usage: cff [--confirm/-c] <command to complete>"
var i = 1
var w = 0
var confirm = false
if paramStr(1) in ["--confirm", "-c"]:
confirm = true
inc i
var cmd = ""
while i <= cnt:
cmd.add ' '
cmd.add complete(paramStr(i), w == 0)
inc i
inc w
if confirm:
echo "[cff] exec: ", cmd, " (y/n?)"
confirm = stdin.readline() == "y"
else:
echo "[cff] ", cmd
confirm = true
if confirm:
let exitCode = execShellCmd(cmd)
echo "[cff] exitcode: ", exitCode
when not defined(testing):
main()