summaryrefslogtreecommitdiffstats
path: root/plugins/hexyl.lua
blob: b98ad28535f7ce69fa7157fd7a53f7ce24a691ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- Note: this plugin depends on the existence of 'inspect' [1] and 'hexyl' [2]
--
-- [1] https://github.com/sharkdp/content_inspector
-- [2] https://github.com/sharkdp/hexyl

function is_binary(path)
    local stream = assert(io.popen("inspect '" .. path .. "'"))
    local output = stream:read('*all')
    stream:close()
    return string.find(output, ": binary\n")
end

function preprocess(path)
    if is_binary(path) then
        tmpfile = os.tmpname()
        os.execute("hexyl --length 1024 --no-position --border=none --no-squeezing '" .. path .. "' > '" .. tmpfile .. "'")
        return tmpfile
    else
        return path
    end
end