forked from Junker/nuke.xplr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
280 lines (232 loc) · 6.66 KB
/
init.lua
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
local archive_mime_types = {
"application/x-tar",
"application/x-bzip2",
"application/gzip",
"application/x-lzma",
"application/x-xz",
"application/x-compress",
"application/zstd",
"application/x-7z-compressed",
"application/x-gtar",
"application/zip",
"application/x-lzop",
"application/x-lzip",
"application/java-archive",
"application/x-rar-compressed",
"application/x-lzh",
"application/x-alz-compressed",
"application/x-ace-compressed",
"application/x-archive",
"application/x-arj",
"application/x-freearc",
"application/x-rpm",
"application/vnd.debian.binary-package",
"application/vnd.ms-cab-compressed",
"application/x-cpio"
};
local open_document_mime_types = {
"application/vnd.oasis.opendocument.presentation",
"application/vnd.oasis.opendocument.spreadsheet",
"application/vnd.oasis.opendocument.text"
}
local pager = "less -R"
local custom_commands = {}
local run_executables = true
local function table_to_string(t, delimiter)
local str = ""
for i,v in ipairs(t) do
str = str .. v
if t[i+1] ~= nil then
str = str .. delimiter
end
end
return str
end
local function exec(command, node, ...)
local args = {...}
if args[1] ~= nil then
local t = args
table.insert(t, node.absolute_path)
return {{ Call = {command = command, args = t} }}
else
return {{ Call = {command = command, args = {node.absolute_path}} }}
end
end
local function exec_paging(command, node, ...)
local args = {...}
if args[1] ~= nil then
return {{ BashExec = command .. " " .. table_to_string(args, " ") .. ' "' .. node.absolute_path .. '" | ' .. pager }}
else
return {{ BashExec = command .. ' "' .. node.absolute_path .. '" | ' .. pager }}
end
end
local function exec_waiting(command, node, ...)
local args = {...}
local waiting = 'read -p "[enter to continue]"'
if args[1] ~= nil then
return {{ BashExec = command .. " " .. table_to_string(args, " ") .. ' "' .. node.absolute_path .. '" && ' .. waiting }}
else
return {{ BashExec = command .. ' "' .. node.absolute_path .. '" && ' .. waiting }}
end
end
local function program_exists(p)
return (os.execute("type " .. p .. " > /dev/null 2>&1") == 0)
end
local function get_node_extension(node)
return node.relative_path:match("^.+%.(.+)$")
end
local function handle_image(node)
if program_exists("viu") then
return exec_waiting("viu", node)
end
if program_exists("timg") then
return exec_waiting("timg", node)
end
if program_exists("chafa") then
return exec_waiting("chafa", node)
end
if program_exists("img2txt") then
return exec_paging("img2txt", node, "--gamma=0.6", "--")
end
end
local function handle_video(node)
if program_exists("mpv") then
return exec("mpv", node, "--vo=tct", "--quiet")
end
if program_exists("mplayer") then
return {{ BashExec = 'CACA_DRIVER=ncurses mplayer -vo caca -quiet "' .. node.absolute_path .. '"' }}
end
end
local function handle_audio(node)
if program_exists("mpv") then
return exec("mpv", node)
elseif program_exists("mplayer") then
return exec("mplayer", node, "-vo null")
end
end
local function handle_archive(node)
if program_exists("atool") then
return exec_paging("atool", node, "--list", "--")
end
if program_exists("dtrx") then
return exec_paging("dtrx", node, "-l")
end
if program_exists("ouch") then
return exec_paging("ouch", node, "list")
end
end
local function handle_pdf(node)
if program_exists("termpdf") and os.getenv("TERM") == "xterm-kitty" then
return exec("termpdf", node)
elseif program_exists("pdftotext") then
return {{ BashExec = 'pdftotext -l 10 -nopgbrk -q -- "' .. node.absolute_path .. '" - | ' .. pager }}
end
end
local function handle_djvu(node)
if program_exists("termpdf") and os.getenv("TERM") == "xterm-kitty" then
return exec("termpdf", node)
end
end
local function handle_text(node)
if program_exists(os.getenv("EDITOR")) then
return exec(os.getenv("EDITOR"), node)
end
end
local function handle_open_document(node)
if program_exists("odt2txt") then
return exec_paging("odt2txt", node)
end
end
local function handle_html(node)
if program_exists("w3m") then
return exec_paging("w3m", node, "-dump")
elseif program_exists("lynx") then
return exec_paging("lynx", node, "-dump", "--")
elseif program_exists("elinks") then
return exec_paging("elinks", node, "-dump")
end
end
local function handle_markdown(node)
if program_exists("glow") then
return exec_paging("glow", node, "-sdark")
elseif program_exists("lowdown") then
return exec_paging("lowdown", node, "-Tterm")
end
end
local function handle_executable(node)
return {{ BashExec = node.absolute_path .. ' ; read -p "[enter to continue]"' }}
end
local function handle_custom(node, command)
command = command:gsub("{}", '"' .. node.absolute_path .. '"')
return {{ BashExec = command }}
end
local function handle_node(ctx)
local node = ctx.focused_node
local node_mime = node.mime_essence
if node.is_dir or (node.is_symlink and node.symlink.is_dir) then
return {"Enter"}
end
if node.is_dir == false or (node.is_symlink and node.symlink.is_dir == false) then
for _,entry in ipairs(custom_commands) do
local command = entry["command"]
if command ~= nil then
if get_node_extension(node) == entry["extension"] then
return handle_custom(node, command)
end
if node_mime == entry["mime"] then
return handle_custom(node, command)
end
if entry["mime_regex"] ~= nil and node_mime:match(entry["mime_regex"]) then
return handle_custom(node, command)
end
end
end
if node_mime == "image/vnd.djvu" then
return handle_djvu(node)
end
if node_mime:match("^image/.*") then
return handle_image(node)
end
if node_mime:match("^video/.*") then
return handle_video(node)
end
if node_mime:match("^audio/.*") then
return handle_audio(node)
end
if node_mime == "application/pdf" then
return handle_pdf(node)
end
if node_mime == "text/markdown" then
return handle_markdown(node)
end
if node_mime == "text/html" or node_mime == "application/xhtml+xml" then
return handle_html(node)
end
for _,v in ipairs(archive_mime_types) do
if node_mime == v then
return handle_archive(node)
end
end
for _,v in ipairs(open_document_mime_types) do
if node_mime == v then
return handle_open_document(node)
end
end
if run_executables and node.permissions.user_execute or node.permissions.group_execute or node.permissions.other_execute then
return handle_executable(node)
end
end
end
local function setup(args)
xplr.fn.custom.nuke_handle_node = handle_node
if args.pager then
pager = args.pager
end
if args.custom then
custom_commands = args.custom
end
if args.run_executables then
run_executables = args.run_executables
end
end
return { setup = setup }