Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
347 lines (300 loc) · 10.5 KB
/
Copy pathmain.lua
File metadata and controls
347 lines (300 loc) · 10.5 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
--- @since 25.12.29
local shell = os.getenv("SHELL"):match(".*/(.*)")
local get_cwd = ya.sync(function() return cx.active.current.cwd end)
local fail = function(s, ...)
ya.notify { title = "frank", content = string.format(s, ...), timeout = 5, level = "error" }
end
-- shell compatibility table
local sh_compat_tbl = {
default = {
wrap = function(cmd) return "(" .. cmd .. ")" end,
prompt = function(s) return string.format("[[ ! $FZF_PROMPT =~ %s ]] &&", s) end,
prev = function(s) return string.format("[[ $FZF_PREVIEW_LABEL =~ %s ]] &&", s) end,
mime_check = function(s) return string.format([=[[[ "$(file --mime-type -b \{})" == %s* ]]]=], s) end,
op = "||",
},
fish = {
wrap = function(cmd) return "begin; " .. cmd .. "; end" end,
prompt = function(s) return string.format([[not string match -q "*%s*" $FZF_PROMPT; and]], s) end,
prev = function(s) return string.format([[string match -q "*%s*" $FZF_PREVIEW_LABEL; and]], s) end,
mime_check = function(s) return string.format([[string match -q "%s*" (file --mime-type -b \{})]], s) end,
op = "; or",
},
}
local function get_sh_helper() return sh_compat_tbl[shell] or sh_compat_tbl.default end
-- get custom options from user setup
local fmt_opts = function(opt)
if type(opt) == "string" then
return " " .. opt
elseif type(opt) == "table" then
return " " .. table.concat(opt, " ")
end
return ""
end
-- get extra preview command from user setup
local check_cmd = function(opt)
if type(opt) == "string" and opt ~= "" then
return opt
end
end
local get_user_opts = ya.sync(function(state)
local opts = state.custom_opts or {}
return {
fzf = fmt_opts(opts.fzf),
rg = fmt_opts(opts.rg),
rga = fmt_opts(opts.rga),
fd = fmt_opts(opts.fd),
bat = fmt_opts(opts.bat),
eza = fmt_opts(opts.eza),
eza_meta = fmt_opts(opts.eza_meta),
rga_preview = fmt_opts(opts.rga_preview),
img_preview = check_cmd(opts.img_preview),
}
end)
-- mimic `bat` grid,header style
local ansi_grid_header = function()
-- ANSI
local bold = "\x1b[1m"
local bar_color = "\x1b[38;2;127;132;156m"
local reset = "\x1b[m"
local bar_line = string.rep("─", 80)
local colored_bar = bar_color .. bar_line .. reset
local label = {
default = string.format([[echo -ne "Dir: %s\{}%s";]], bold, reset),
file = string.format([[echo -e "File: %s\{}%s";]], bold, reset),
meta = string.format(
[[test -d \{1} && echo -ne "Dir: %s\{1}%s" || echo -ne "File: %s\{1}%s";]],
bold,
reset,
bold,
reset
),
}
return {
bar = string.format([[echo -e "%s";]], colored_bar),
bar_with_new_line = string.format([[echo -e "\n%s";]], colored_bar),
label = label,
}
end
local function eza_preview(prev_type, opts)
local header = ansi_grid_header()
local extra_flags = {
default = "--oneline " .. opts.eza,
meta = "--git --git-repos --header --long --mounts --no-user --octal-permissions " .. opts.eza_meta,
}
return table.concat({
header.bar,
header.label[prev_type],
[[test -z "$(eza -A \{1})" && echo -e " <EMPTY>" || ]] .. header.bar_with_new_line,
string.format([[eza %s --all --color=always --group-directories-first --icons \{1};]], extra_flags[prev_type]),
header.bar,
}, " ")
end
local rga_preview_with_header = function(user_opts)
local sh = get_sh_helper()
local header = ansi_grid_header()
return [[test -n \{} && ]]
.. sh.wrap(table.concat({
header.bar,
header.label.file,
header.bar,
"rga --context 5 --no-messages --pretty " .. user_opts .. [[ \{q} \{};]],
header.bar,
}, " "))
end
local function img_preview_with_header(cmd)
local header = ansi_grid_header()
return table.concat({
header.bar,
header.label.file,
header.bar,
cmd .. [[ \{};]],
}, " ")
end
-- common `fzf` base cmd
local function build_from_fzf_base(search_cmd, preview_cmd, preview_window, prompt, user_opts, specific_options)
local sh = get_sh_helper()
local toggle_preview = string.format(
[[%s echo '\''change-preview-label(metadata)+change-preview-window(~5)+change-preview:%s'\'' %s ]]
.. [[echo '\''change-preview-label(content)+change-preview-window(~3)+change-preview:%s'\'']],
sh.prev("content"),
eza_preview("meta", user_opts),
sh.op,
preview_cmd
)
local base_tbl = {
"fzf",
"--ansi",
"--delimiter=:",
"--no-multi",
"--reverse",
string.format("--bind='start:reload(%s)+transform:%s'", search_cmd, toggle_preview),
string.format("--bind='change:reload:sleep 0.1; %s || true'", search_cmd),
string.format("--bind='ctrl-r:clear-query+reload:%s || true'", search_cmd),
string.format("--prompt='%s'", prompt),
string.format("--preview='echo loading'"),
string.format("--preview-window='%s'", preview_window),
"--bind='ctrl-]:change-preview-window(80%|66%)'",
"--bind='ctrl-\\:change-preview-window(right|up)'",
string.format([[--bind 'alt-p:transform:%s']], toggle_preview),
}
for _, option in ipairs(specific_options) do
table.insert(base_tbl, option)
end
table.insert(base_tbl, user_opts.fzf) -- user `fzf` options at the end
return base_tbl
end
-- fzf with `rg` or `rga` search
local function build_search_by_content(search_type, user_opts)
local sh = get_sh_helper()
local cmd_tbl = {
rg = {
grep = "rg --color=always --line-number --smart-case" .. user_opts.rg .. " {q}",
prev = string.format([[bat --color=always %s --highlight-line=\{2} \{1}]], user_opts.bat),
prev_window = "~3,+{2}+3/2,up,66%",
prompt = "rg> ",
specific_options = { "--disabled", "--bind='ctrl-o:execute:$EDITOR {1} +{2}'", "--nth=3.." },
fzf_match = function(cmd_grep)
local bind_fzf_match_tmpl = "--bind='ctrl-s:transform:%s "
.. [[echo "rebind(change)+change-prompt(rg> )+disable-search+clear-query+reload:%s || true" %s ]]
.. [[echo "unbind(change)+change-prompt(fzf> )+enable-search+clear-query"']]
return string.format(bind_fzf_match_tmpl, sh.prompt("rg"), cmd_grep, sh.op)
end,
},
rga = {
grep = "rga --color=always --files-with-matches --smart-case" .. user_opts.rga .. " {q}",
prev = rga_preview_with_header(user_opts.rga_preview),
prev_window = "up,66%",
prompt = "rga> ",
specific_options = { "--disabled", "--bind='ctrl-o:execute:$EDITOR {}'" },
},
}
local cmd = cmd_tbl[search_type]
if not cmd then
fail("`%s` is not a valid argument for `content` search.\nUse `rg` or `rga` instead", search_type)
return nil
end
if cmd.fzf_match then
table.insert(cmd.specific_options, cmd.fzf_match(cmd.grep)) -- `fzf` match for `rg`
end
local fzf_tbl = build_from_fzf_base(cmd.grep, cmd.prev, cmd.prev_window, cmd.prompt, user_opts, cmd.specific_options)
return table.concat(fzf_tbl, " ")
end
-- fzf with `fd` search
local function build_search_by_name(search_type, user_opts)
local sh = get_sh_helper()
local cmd_tbl = {
all = sh.wrap(string.format("fd --type=d %s {q}; fd --type=f %s {q}", user_opts.fd, user_opts.fd)),
cwd = sh.wrap(
string.format("fd --max-depth=1 --type=d %s {q}; fd --max-depth=1 --type=f %s {q}", user_opts.fd, user_opts.fd)
),
dir = "fd --type=dir " .. user_opts.fd .. " {q}",
file = "fd --type=file " .. user_opts.fd .. " {q}",
}
local fd_cmd = cmd_tbl[search_type]
if not fd_cmd then
fail("`%s` is not a valid argument for `name` search.\nUse `all`, `cwd`, `dir`, or `file` instead", search_type)
return nil
end
local bat_prev = string.format([[bat --color=always --style=grid,header %s \{}]], user_opts.bat)
local file_prev = bat_prev
-- optional, earlier entries take precedence
local extra_previews = {}
-- image
if user_opts.img_preview then
table.insert(extra_previews, { mime = "image/", cmd = img_preview_with_header(user_opts.img_preview) })
end
for i = #extra_previews, 1, -1 do
local ep = extra_previews[i]
file_prev = string.format([[%s && %s || %s]], sh.mime_check(ep.mime), sh.wrap(ep.cmd), sh.wrap(file_prev))
end
local default_prev =
string.format([[test -d \{} && %s || %s]], sh.wrap(eza_preview("default", user_opts)), sh.wrap(file_prev))
local specific_options = {
"--bind='ctrl-o:execute:$EDITOR {}'",
string.format(
"--bind='ctrl-s:transform:%s "
.. [[echo "rebind(change)+change-prompt(fd> )+clear-query+reload:%s" %s ]]
.. [[echo "unbind(change)+change-prompt(fzf> )+clear-query"']],
sh.prompt("fd"),
fd_cmd,
sh.op
),
}
local fzf_tbl = build_from_fzf_base(fd_cmd, default_prev, "up,66%", "fd> ", user_opts, specific_options)
return table.concat(fzf_tbl, " ")
end
local function entry(_, job)
local _permit = ui.hide()
local user_opts = get_user_opts()
local cwd = get_cwd()
local search_type, search_opt = job.args[1], job.args[2]
local args
if search_type == "content" then
args = build_search_by_content(search_opt or "rg", user_opts) -- fallback to `rg` search
elseif search_type == "name" then
args = build_search_by_name(search_opt or "all", user_opts) -- fallback to `fd` "all" search (dirs and files)
else
return fail("Please specify either 'content' or 'name' as the first argument")
end
if not args then
return -- no valid second argument
end
local child, err = Command(shell)
:arg({ "-c", args })
:cwd(tostring(cwd))
:stdin(Command.INHERIT)
:stdout(Command.PIPED)
:stderr(Command.INHERIT)
:spawn()
if not child then
return fail("Failed to spawn shell, error: %s", err)
end
local output, err = child:wait_with_output()
if not output then
return fail("Cannot read command output, error: %s", err)
end
if output.status.code == 130 then -- interrupted with <ctrl-c> or <esc>
return nil
elseif output.status.code == 1 then -- no match
ya.notify { title = "frank", content = "No match found", timeout = 5 }
return nil
elseif output.status.code ~= 0 then -- anything other than normal exit
fail("`fzf` exited with error code %s", output.status.code)
return nil
end
local target = output.stdout:gsub("\n$", "")
if target ~= "" then
if search_type == "content" then
local colon_pos = string.find(target, ":")
local file_path = colon_pos and string.sub(target, 1, colon_pos - 1) or target
local url = Url(file_path)
if not url.is_absolute then
url = cwd:join(url)
end
ya.emit("reveal", { url })
elseif search_type == "name" then
local is_dir = target:sub(-1) == "/"
local url = Url(target)
if not url.is_absolute then
url = cwd:join(url)
end
ya.emit(is_dir and "cd" or "reveal", { url })
end
end
end
local function setup(state, opts)
opts = opts or {}
state.custom_opts = {
fzf = opts.fzf,
rg = opts.rg,
rga = opts.rga,
fd = opts.fd,
bat = opts.bat,
eza = opts.eza,
eza_meta = opts.eza_meta,
rga_preview = opts.rga_preview,
img_preview = opts.img_preview,
}
end
return { entry = entry, setup = setup }