Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Junker committed Jul 9, 2022
1 parent 72f4513 commit fbbc126
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,29 @@ local function table_to_string(t, delimiter)
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
return {{ Call = {command = command, args = {node.absolute_path, ...}} }}
end

local function exec_paging(command, node, ...)
local args = {...}
local str_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 }}
str_args = table_to_string(args, " ")
end

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 waiting = 'read -p "[enter to continue]"'
local str_args = ""

if args[1] ~= nil then
return {{ BashExec = command .. " " .. table_to_string(args, " ") .. ' "' .. node.absolute_path .. '" && ' .. waiting }}
else
return {{ BashExec = command .. ' "' .. node.absolute_path .. '" && ' .. waiting }}
str_args = table_to_string(args, " ")
end

return {{ BashExec = string.format('%s %s "%s" && read -p "[enter to continue]"', command, str_args, node.absolute_path) }}
end

local function program_exists(p)
Expand All @@ -93,23 +85,19 @@ end
local function handle_image(node)
if program_exists("viu") then
return exec_waiting("viu", node)
end
if program_exists("timg") then
elseif program_exists("timg") then
return exec_waiting("timg", node)
end
if program_exists("chafa") then
elseif program_exists("chafa") then
return exec_waiting("chafa", node)
end
if program_exists("img2txt") then
elseif 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
elseif program_exists("mplayer") then
return {{ BashExec = 'CACA_DRIVER=ncurses mplayer -vo caca -quiet "' .. node.absolute_path .. '"' }}
end
end
Expand All @@ -125,8 +113,7 @@ end
local function handle_archive(node)
if program_exists("atool") then
return exec_paging("atool", node, "--list", "--")
end
if program_exists("dtrx") then
elseif program_exists("dtrx") then
return exec_paging("dtrx", node, "-l")
end
if program_exists("ouch") then
Expand Down

0 comments on commit fbbc126

Please sign in to comment.