-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathff.nim
More file actions
237 lines (192 loc) · 7.01 KB
/
ff.nim
File metadata and controls
237 lines (192 loc) · 7.01 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import commandeer, os, osproc, parsecfg, streams, strscans, strutils, tables
const
VERSION {.strdefine.} = ""
DOC = """
ff $#
Windows wrapper for fzf
Usage:
ff [options] [<short1>] [<short2>]
Options:
-d <dir> Directory to index or shortcut from config file
-s <select> Content to populate the fzf menu
file, dir or *.xyz shortcuts
or command to execute
-a <action> Action to execute or shortcut from config file
-q <query> Prefill query for fzf
-c <config> Configuration file
-h This help menu
Shortcut arguments:
short1 dir shortcut if matches
action shortcut if matches
dir if directory exists
else action
short1 short2 short1 = dir shortcut or dir if exists
short2 = action shortcut or action
Explicit -d or -a will override any shortcut arguments
""" % VERSION
proc parseConfig(help=false): string
commandline:
arguments(SHORTCUT, string, false)
option(DIR, string, "dir", "d", getCurrentDir())
option(SELECT, string, "select", "s", "")
option(ACTION, string, "action", "a", "{}")
option(QUERY, string, "query", "q", "")
option(CONFIGFILE, string, "config", "c", getAppDir()/"ff.cfg")
option(TEST, bool, "test", "t", false)
exitoption("help", "h", parseConfig(true))
var CONFIG: Config
proc fzf() =
var act = ACTION
if not ("{}" in ACTION):
act &= " {}"
var args = @["--bind", "enter:execute(start \"\" /I $#)+abort" % act]
if QUERY != "":
args.add("--query")
args.add(QUERY)
if not TEST:
var
line: string
pin: Process # FZF
pout: Process # SELECT
sin: Stream
sout: Stream
if SELECT.len() != 0:
pin = startProcess("fzf", DIR, args, options={poUsePath})
sin = pin.inputStream()
pout = startProcess(SELECT, DIR, options={poUsePath, poEvalCommand, poStdErrToStdOut})
sout = pout.outputStream()
try:
while true:
if pout.running():
line = sout.readLine()
else:
break
if pin.running():
sin.writeLine(line)
else:
break
except IOError, OSError:
discard
sout.close()
sin.close()
if pout.running():
pout.kill()
discard pout.waitForExit()
else:
pin = startProcess("fzf", DIR, args, options={poUsePath, poParentStreams})
discard pin.waitForExit()
else:
stdout.write DIR & " : "
for arg in args:
stdout.write arg & " : "
echo SELECT.replace("cmd /c ", "")
proc replaceEnv(str: var string): string =
var env: string
while "$" in str:
let pos = str.find("$")
if str[pos..<str.len].scanf("$$$w", env):
if existsEnv(env):
str = str.replace("$" & env, getEnv(env))
else:
echo "Missing environment variable: $" & env
quit(1)
return str.replace("/", $DirSep).replace("\\", $DirSep)
proc dirFind(dir: string, fail = false): string =
var dirout = dir
# Expand directory shortcut
if CONFIG.hasKey("directories") and CONFIG["directories"].hasKey(dirout):
dirout = CONFIG["directories"][dirout].replaceEnv()
if not dirExists(dirout):
if fail:
echo "Directory doesn't exist: " & dirout
quit(1)
return ""
return dirout
proc actionFind(action, select: string): tuple[actionout, selectout: string] =
var actionout = action
var selectout = select
# Expand action shortcut
if CONFIG.hasKey("actions") and CONFIG["actions"].hasKey(actionout & ".action"):
if selectout == "" and CONFIG["actions"].hasKey(actionout & ".select"):
selectout = CONFIG["actions"][actionout & ".select"]
actionout = CONFIG["actions"][actionout & ".action"]
# Expand SELECT shortcuts
if selectout != "":
if selectout == "file":
selectout = "cmd /c dir /s/b/a-d"
elif selectout == "dir":
selectout = "cmd /c dir /s/b/ad"
elif selectout.find("*") != -1 and selectout.find(" ") == -1:
selectout = "cmd /c dir /s/b " & selectout
return (actionout, selectout)
proc parseConfig(help=false): string =
# Load config file
if fileExists(CONFIGFILE):
CONFIG = loadConfig(CONFIGFILE)
else:
CONFIG = newConfig()
# List configuration if help
if help or len(SHORTCUT) > 2:
var helpout = DOC
if fileExists(CONFIGFILE):
helpout &= "Configuration file: \n " & CONFIGFILE & "\n\n"
if CONFIG.hasKey("directories"):
helpout &= "Directory shortcuts:\n"
for i in CONFIG["directories"].keys():
helpout &= " " & i & " = " & CONFIG["directories"][i].replaceEnv() & "\n"
helpout &= "\n"
if CONFIG.hasKey("actions"):
helpout &= "Action shortcuts:"
for i in CONFIG["actions"].keys():
var nv = i.split(".")
if len(nv) != 2:
echo "Bad configuration key: " & i
quit(1)
if nv[1] == "name":
helpout &= "\n " & nv[0] & " = " & CONFIG["actions"][i]
elif nv[1] == "select":
helpout &= "\n Select: " & CONFIG["actions"][i]
elif nv[1] == "action":
helpout &= "\n Action: " & CONFIG["actions"][i]
if not ("{}" in CONFIG["actions"][i]):
helpout &= " {}"
return helpout
# Expand -d and -a
DIR = dirFind(DIR, true)
(ACTION, SELECT) = actionFind(ACTION, SELECT)
# Analyze shortcuts
var short1 = ""
var short2 = ""
if len(SHORTCUT) > 0:
short1 = SHORTCUT[0]
if len(SHORTCUT) > 1:
short2 = SHORTCUT[1]
if short1 != "":
# short1 has to be directory or dir shortcut
if short2 != "":
# Expand dir if -d not specified
if DIR == getCurrentDir():
DIR = dirFind(short1, true)
# Expand action if -a not specified
if ACTION == "{}":
(ACTION, SELECT) = actionFind(short2, SELECT)
else:
# Could be directory
# Expand dir if -d not specified
if DIR == getCurrentDir():
var d = dirFind(short1)
# Didn't work, maybe action if -a not specified
if d == "" and ACTION == "{}":
(ACTION, SELECT) = actionFind(short1, SELECT)
else:
DIR = d
else:
# -d specified, so must be action if -a not specified
if ACTION == "{}":
(ACTION, SELECT) = actionFind(short1, SELECT)
return ""
var helpout = parseConfig()
if helpout != "":
echo helpout
else:
fzf()