Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Junker committed Jul 7, 2022
1 parent b589e4e commit 42dc51f
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 40 deletions.
64 changes: 47 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# xplr plugin template
# Nuke: xplr file opener

Use this template to [write your own xplr plugin](https://arijitbasu.in/xplr/en/writing-plugins.html).
Plugin for [xplr](https://github.com/sayanarijit/xplr): open files in apps by file type or mime

> **NOTE:** The `src` directory is a symlink to `.` for compatibility reasons.
> It may be removed in the future.
inspired by [nnn](https://github.com/jarun/nnn) file manager [nuke plugin](https://github.com/jarun/nnn/blob/master/plugins/nuke).

## Requirements

- Some tool
| File type | Program |
|:-------------|:-----------------------|
| Image | viu/timg/chafa/img2txt |
| Video | mpv/mplayer |
| Audio | mpv/mplayer |
| Archive | atool/dtrx/ouch |
| OpenDocument | odt2txt |
| HTML | w3m/lynx/elinks |
| PDF | termpdf/pdftotext |
| DJVU | termpdf |
| Executable | dialog |

*recomended terminal emulator with support of [Terminal graphics protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/)*

## Installation

Expand All @@ -29,24 +40,43 @@ Use this template to [write your own xplr plugin](https://arijitbasu.in/xplr/en/
```bash
mkdir -p ~/.config/xplr/plugins

git clone https://github.com/{username}/{plugin}.xplr ~/.config/xplr/plugins/{plugin}
git clone https://github.com/Junker/nuke.xplr ~/.config/xplr/plugins/nuke
```

### Install with [xpm](https://github.com/dtomvan/xpm.xplr)

- Require the module in `~/.config/xplr/init.lua`
```lua
require("xpm").setup({
plugins = {
'dtomvan/xpm.xplr',
'Junker/nuke.xplr'
}
})
```

### Usage

```lua
require("{plugin}").setup()
require("nuke").setup()

-- Or

require("{plugin}").setup{
mode = "action",
key = ":",
require("nuke").setup{
pager = "more", -- default: less -R
run_executables = false, -- default: true
custom = {
{extension = "jpg", command = "sxiv {}"},
{extension = "so", command = "ldd -r {} | less"},
{mime = "video/mp4", command = "vlc {}"},
{mime_regex = "^video/.*", command = "smplayer {}"}
{mime_regex = ".*", command = "xdg-open {}"}
}
}

xplr.config.modes.builtin.default.key_bindings.on_key["enter"] = {
help = "nuke",
messages = {
{CallLuaSilently = "custom.nuke_handle_node"}
}
}

-- Type `::` and enjoy.
```

## Features

- Some cool feature
237 changes: 230 additions & 7 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,236 @@
---@diagnostic disable
local xplr = xplr
---@diagnostic enable
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 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 {{ BashExec = 'viu "' .. node.absolute_path .. '" && read -n 1 -s && read -k' }}
end
if program_exists("timg") then
return {{ BashExec = 'timg "' .. node.absolute_path .. '" && read -n 1 -s && read -k' }}
end
if program_exists("chafa") then
return {{ BashExec = 'chafa "' .. node.absolute_path .. '" && read -n 1 -s && read -k' }}
end
if program_exists("img2txt") then
return {{ BashExec = 'img2txt --gamma=0.6 -- "' .. node.absolute_path .. '" | ' .. pager }}
end
end

local function handle_video(node)
if program_exists("mpv") then
return {{ Call = {command = "mpv", args = {"--vo=tct", "--quiet", node.absolute_path}} }}
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 {{ Call = {command = "mpv", args = {node.absolute_path}} }}
elseif program_exists("mplayer") then
return {{ Call = {command = "mplayer", args = {node.absolute_path}} }}
end
end

local function handle_archive(node)
if program_exists("atool") then
return {{ BashExec = 'atool --list -- "' .. node.absolute_path .. '" | ' .. pager }}
end
if program_exists("dtrx") then
return {{ BashExec = 'dtrx -l "' .. node.absolute_path .. '" | ' .. pager }}
end
if program_exists("ouch") then
return {{ BashExec = 'ouch list "' .. node.absolute_path .. '" | ' .. pager }}
end
end

local function handle_pdf(node)
if program_exists("termpdf") and os.getenv("TERM") == "xterm-kitty" then
return {{ Call = {command = "termpdf", args = {node.absolute_path}} }}
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 {{ Call = {command = "termpdf", args = {node.absolute_path}} }}
end
end

local function handle_text(node)
if program_exists(os.getenv("EDITOR")) then
return {{ Call = {command = os.getenv("EDITOR"), args = {node.absolute_path}} }}
end
end

local function handle_open_document(node)
if program_exists("odt2txt") then
return {{ BashExec = 'odt2txt "' .. node.absolute_path .. '" | ' .. pager }}
end
end

local function handle_html(node)
if program_exists("w3m") then
return {{ Call = {command = "w3m", args = {"-dump", node.absolute_path}} }}
elseif program_exists("lynx") then
return {{ Call = {command = "lynx", args = {"-dump", "--", node.absolute_path}} }}
elseif program_exists("elinks") then
return {{ Call = {command = "elinks", args = {"-dump", node.absolute_path}} }}
end
end


local function handle_markdown(node)
if program_exists("glow") then
return {{ BashExec = 'glow -sdark "' .. node.absolute_path .. '" | ' .. pager }}
elseif program_exists("lowdown") then
return {{ BashExec = 'lowdown -Tterm "' .. node.absolute_path .. '" | ' .. pager }}
end
end

local function handle_executable(node)
if program_exists("dialog") then
return {{ BashExec = 'dialog --defaultno --yesno "Run executable?" 15 40 && ' .. node.absolute_path .. " ; (read -n 1 -s && read -k)" }}
end
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 run_executables and node.permissions.user_execute or node.permissions.group_execute or node.permissions.other_execute then
return handle_executable(node)
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
end
end

local util = require("plugin-template1.lib.util")

local function setup(args)
print(util.dump(args))
print(util.dump(xplr))
io.read()
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 }
16 changes: 0 additions & 16 deletions lib/util.lua

This file was deleted.

0 comments on commit 42dc51f

Please sign in to comment.