forked from Junker/nuke.xplr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
615 lines (510 loc) · 15.7 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
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 open_custom_commands = {}
local smart_custom_commands = {}
local run_executables = true
local show_line_numbers = false
local nuke_mode = {
name = "nuke",
key_bindings = {
on_key = {
["o"] = {
help = "open",
messages = {
{ CallLuaSilently = "custom.nuke_open" },
"PopMode",
},
},
["v"] = {
help = "view",
messages = {
{ CallLuaSilently = "custom.nuke_view" },
"PopMode",
},
},
["s"] = {
help = "smart view",
messages = {
{ CallLuaSilently = "custom.nuke_smart_view" },
"PopMode",
},
},
["h"] = {
help = "hex view",
messages = {
{ CallLuaSilently = "custom.nuke_hex_view" },
"PopMode",
},
},
["i"] = {
help = "info view",
messages = {
{ CallLuaSilently = "custom.nuke_info_view" },
"PopMode",
},
},
esc = {
help = "cancel",
messages = {
"PopMode",
},
},
},
},
}
local function _if(bool, arg1, arg2)
if bool then return arg1 else return arg2 end
end
local function _case(case, cases)
local fn = cases[case]
if (fn) then
return fn()
end
end
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 program_exists(p)
return (os.execute("type " .. p .. " > /dev/null 2>&1") == 0)
end
local function exec(command, node, ...)
return {{ Call = {command = command, args = {node.absolute_path, ...}} }}
end
local function exec_paging(command, node, ...)
local args = {...}
local str_args = ""
str_args = table_to_string(args, " ")
return {{ BashExec = string.format('%s %s "%s" | %s', command, str_args, node.absolute_path, pager) }}
end
local function exec_waiting(command, node, ...)
local args = {...}
local str_args = ""
str_args = table_to_string(args, " ")
return {{ BashExec = string.format('%s %s "%s" && read -p "[enter to continue]"', command, str_args, node.absolute_path) }}
end
local function exec_paging_html(command, node, ...)
local args = {...}
local str_args = ""
local dumper = nil
str_args = table_to_string(args, " ")
if program_exists("w3m") then
dumper = 'w3m -T text/html -dump'
elseif program_exists("elinks") then
dumper = 'elinks -dump'
elseif program_exists("lynx") then
dumper = 'lynx -dump -stdin'
end
if dumper then
return {{ BashExec = string.format('%s %s "%s" | %s | %s', command, str_args, node.absolute_path, dumper, pager) }}
end
end
local function exec_custom(command, node)
command = command:gsub("{}", '"' .. node.absolute_path .. '"')
return {{ BashExec = command }}
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 open_image(node)
if program_exists("viu") then
return exec_waiting("viu", node)
elseif program_exists("timg") then
return exec_waiting("timg", node)
elseif program_exists("chafa") then
return exec_waiting("chafa", node)
elseif program_exists("cacaview") then
return exec_paging("cacaview", node)
end
end
local function open_video(node)
if program_exists("mpv") then
return exec("mpv", node, "--vo=tct", "--quiet")
elseif program_exists("mplayer") then
return {{ BashExec = 'CACA_DRIVER=ncurses mplayer -vo caca -quiet "' .. node.absolute_path .. '"' }}
end
end
local function open_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 open_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 open_djvu(node)
if program_exists("termpdf") and os.getenv("TERM") == "xterm-kitty" then
return exec("termpdf", node)
end
end
local function open_text(node)
if program_exists(os.getenv("EDITOR")) then
return exec(os.getenv("EDITOR"), node)
end
end
local function open_executable(node)
return {{ BashExec = node.absolute_path .. ' ; read -p "[enter to continue]"' }}
end
local function open(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(open_custom_commands) do
local command = entry["command"]
if command ~= nil then
if get_node_extension(node) == entry["extension"] then
return exec_custom(command, node)
end
if node_mime == entry["mime"] then
return exec_custom(command, node)
end
if entry["mime_regex"] ~= nil and node_mime:match(entry["mime_regex"]) then
return exec_custom(command, node)
end
end
end
if node_mime == "image/vnd.djvu" then
return open_djvu(node)
elseif node_mime:match("^image/.*") then
return open_image(node)
elseif node_mime:match("^video/.*") then
return open_video(node)
elseif node_mime:match("^audio/.*") then
return open_audio(node)
elseif node_mime == "application/pdf" then
return open_pdf(node)
end
if run_executables and node.permissions.user_execute or node.permissions.group_execute or node.permissions.other_execute then
return open_executable(node)
end
end
end
local function hex_view(ctx)
local node = ctx.focused_node
if node.is_dir == false or (node.is_symlink and node.symlink.is_dir == false) then
if program_exists("hx") then
return exec_paging("hx", node, "-t 1")
elseif program_exists("hexyl") then
return exec_paging("hexyl", node)
elseif program_exists("huxd") then
return exec_paging("huxd", node, "-C always", "-P never")
elseif program_exists("hxl") then
return exec_paging("hxl", node)
elseif program_exists("hexdump") then
return exec_paging("hexdump", node)
end
end
end
local function view_node(node)
if program_exists("bat") then
return exec_paging("bat", node, "--color always", "--paging never", _if(show_line_numbers, "--style=plain,numbers", "--style=plain"))
end
if program_exists("pygmentize") then
return exec_paging("pygmentize", node, "-g")
end
return exec_paging("less", node, _if(show_line_numbers, "-N", ""))
end
local function view(ctx)
local node = ctx.focused_node
if node.is_dir == false or (node.is_symlink and node.symlink.is_dir == false) then
return view_node(node)
end
end
local function info_view_node(node)
if program_exists("exiftool") then
return exec_paging("exiftool", node)
end
return exec_paging("file", node)
end
local function info_view_image(node)
if program_exists("mediainfo") then
return exec_paging("mediainfo", node)
end
return info_view_node(node)
end
local function info_view_video(node)
if program_exists("mediainfo") then
return exec_paging("mediainfo", node)
elseif program_exists("mplayer") then
return exec_paging("mplayer", node, "-identify", "-vo null", "-ao null", "-frames 0")
end
return info_view_node(node)
end
local function info_view_audio(node)
if program_exists("mediainfo") then
return exec_paging("mediainfo", node)
elseif program_exists("mplayer") then
return exec_paging("mplayer", node, "-identify", "-vo null", "-ao null", "-frames 0")
end
return info_view_node(node)
end
local function info_view_epub(node)
if program_exists("einfo") then
return exec_paging("einfo", node, "-v");
end
end
local function info_view(ctx)
local node = ctx.focused_node
local node_mime = node.mime_essence
if node.is_dir == false or (node.is_symlink and node.symlink.is_dir == false) then
if node_mime:match("^image/.*") then
return info_view_image(node)
end
if node_mime:match("^video/.*") then
return info_view_video(node)
end
if node_mime == "application/epub+zip" then
return info_view_epub(node)
end
return info_view_node(node)
end
end
local function smart_view_archive(node)
if program_exists("atool") then
return exec_paging("atool", node, "--list", "--")
end
end
local function smart_view_open_document(node)
if program_exists("odt2txt") then
return exec_paging("odt2txt", node)
end
end
local function smart_view_man(node)
return {{ BashExec = 'MANROFFOPT=-c MAN_KEEP_FORMATTING=1 man -P cat "' .. node.absolute_path .. '" | ' .. pager }}
end
local function smart_view_pdf(node)
if program_exists("pdftotext") then
return {{ BashExec = 'pdftotext -l 10 -nopgbrk -q -- "' .. node.absolute_path .. '" - | ' .. pager }}
end
end
local function smart_view_ps(node)
if program_exists("ps2ascii") then
return exec_paging("ps2ascii", node);
end
end
local function smart_view_image(node)
if program_exists("viu") then
return exec_paging("viu", node, "-b", "-s");
elseif program_exists("chafa") then
return exec_paging("chafa", node, "--animate=false", "--format=symbols");
elseif program_exists("catimg") then
return exec_paging("catimg", node);
elseif program_exists("img2txt") then
return exec_paging("img2txt", node, "--gamma=0.6", "--")
end
return info_view_image(node)
end
local function smart_view_md(node)
if program_exists("glow") then
return exec_paging("glow", node, "-sdark")
elseif program_exists("lowdown") then
return exec_paging("lowdown", node, "-Tterm")
elseif program_exists("mdless") then
return {{ Call = {command="mdless", args={node.absolute_path}} }}
end
end
local function smart_view_djvu(node)
if program_exists("djvused") then
return exec_paging("djvused", node, "-e print-pure-txt");
end
end
local function smart_view_html(node)
return exec_paging_html("cat", node)
end
local function smart_view_doc(node)
if program_exists("antiword") then
return exec_paging("antiword", node, "-t");
elseif program_exists("catdoc") then
return exec_paging("catdoc", node, "-w");
elseif program_exists("wvWare") then
return exec_paging_html("wvWare", node)
end
end
local function smart_view_docx(node)
if program_exists("pandoc") then
return exec_paging("pandoc", node, "-o -", "-t plain")
elseif program_exists("lowriter") then
return {{ BashExec = 'VIEWRTMP=`mktemp -q -d ${TMPDIR:-/tmp}/xplr-nuke.XXXXXX` && cp "'
.. node.absolute_path
.. '" $VIEWRTMP/temp.docx && cd $VIEWRTMP && libreoffice --headless --convert-to txt $VIEWRTMP/temp.docx > /dev/null 2>&1 && cat $VIEWRTMP/temp.txt | '
.. pager .. ' && rm -rf "$VIEWRTMP"'}}
end
end
local function smart_view_xlsx(node)
if program_exists("lowriter") then
local cmd = 'VIEWRTMP=`mktemp -q -d ${TMPDIR:-/tmp}/xplr-nuke.XXXXXX` && cp "'
.. node.absolute_path
.. '" $VIEWRTMP/temp.xlsx && cd $VIEWRTMP && libreoffice --headless --convert-to html $VIEWRTMP/temp.xlsx > /dev/null 2>&1 '
.. '&& cat $VIEWRTMP/temp.html | %s | ' .. pager
.. ' && rm -rf "$VIEWRTMP"'
if program_exists("w3m") then
return {{ BashExec = string.format(cmd, "w3m -T text/html -dump") }}
elseif program_exists("elinks") then
return {{ BashExec = string.format(cmd, "elinks -dump") }}
elseif program_exists("lynx") then
return {{ BashExec = string.format(cmd, "lynx -dump -stdin") }}
end
end
end
local function smart_view_xls(node)
if program_exists("xlhtml") then
return exec_paging_html("xlhtml", node, "-a")
end
if program_exists("xls2csv") then
return exec_paging("xls2csv", node);
end
end
local function smart_view_epub(node)
if program_exists("pandoc") then
return exec_paging("pandoc", node, "-o -", "-t plain")
end
return info_view_epub(node)
end
local function smart_view_fb2(node)
if program_exists("pandoc") then
return exec_paging("pandoc", node, "-o -", "-t plain")
end
return info_view_epub(node)
end
local function smart_view(ctx)
local node = ctx.focused_node
local node_mime = node.mime_essence
if node.is_dir == false or (node.is_symlink and node.symlink.is_dir == false) then
for _,entry in ipairs(smart_custom_commands) do
local command = entry["command"]
if command ~= nil then
if get_node_extension(node) == entry["extension"] then
return exec_custom(command, node)
end
if node_mime == entry["mime"] then
return exec_custom(command, node)
end
if entry["mime_regex"] ~= nil and node_mime:match(entry["mime_regex"]) then
return exec_custom(command, node)
end
end
end
local cases = {
["application/x-troff-man"] = function() return smart_view_man(node) or view_node(node) end,
["application/pdf"] = function() return smart_view_pdf(node) end,
["application/postscript"] = function() return smart_view_ps(node) end,
["text/markdown"] = function() return smart_view_md(node) or view_node(node) end,
["text/html"] = function() return smart_view_html(node) or view_node(node) end,
["application/xhtml+xml"] = function() return smart_view_html(node) or view_node(node) end,
["image/vnd.djvu"] = function() return smart_view_djvu(node) end,
["application/msword"] = function() return smart_view_doc(node) end,
["application/vnd.openxmlformats-officedocument.wordprocessingml.document"] = function() return smart_view_docx(node) end,
["application/vnd.ms-excel"] = function() return smart_view_xls(node) end,
["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"] = function() return smart_view_xlsx(node) end,
["application/epub+zip"] = function() return smart_view_epub(node) end,
}
local res = _case(node_mime, cases);
if res then return res end
local cases = {
["fb2"] = function() return smart_view_fb2(node) end,
}
local res = _case(get_node_extension(node), cases);
if res then return res end
if node_mime:match("^image/.*") then
return smart_view_image(node)
elseif node_mime:match("^video/.*") then
return info_view_video(node)
elseif node_mime:match("^audio/.*") then
return info_view_audio(node)
elseif node_mime:match("^text/.*") then
return view_node(node)
end
for smart_view_,v in ipairs(archive_mime_types) do
if node_mime == v then
return smart_view_archive(node)
end
end
for smart_view_,v in ipairs(open_document_mime_types) do
if node_mime == v then
return smart_view_open_document(node)
end
end
return info_view_node(node)
end
end
local function setup(args)
xplr.config.modes.custom.nuke = nuke_mode
xplr.fn.custom.nuke_view = view
xplr.fn.custom.nuke_hex_view = hex_view
xplr.fn.custom.nuke_info_view = info_view
xplr.fn.custom.nuke_smart_view = smart_view
xplr.fn.custom.nuke_open = open
if args == nil then
args = {}
end
if args.pager then
pager = args.pager
end
if args.view then
if args.view.show_line_numbers then
show_line_numbers = args.view.show_line_numbers
end
end
if args.open then
if args.open.custom then
open_custom_commands = args.open.custom
end
if args.open.run_executables then
run_executables = args.open.run_executables
end
end
if args.smart_view then
if args.smart_view.custom then
smart_custom_commands = args.smart_view.custom
end
end
end
return { setup = setup }