summaryrefslogtreecommitdiff
path: root/lua/mo/oil-util.lua
blob: 1b00fa7ff6210103bd33a60b7ee657ce6f7de170 (plain)
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
local M = {}

local notif = require("mo.notif")

---@param opts { full_path: boolean }
M.copy_to_clipboard = function(opts)
    opts = opts or { full_path = true }

    local oil = require("oil")
    local dir = oil.get_current_dir()
    if not dir then
        notif.err("System clipboard only works for local files")
        return
    end

    local entries = {}
    local mode = vim.api.nvim_get_mode().mode
    if mode == "v" or mode == "V" then
        local start = vim.fn.getpos("v")
        local end_ = vim.fn.getpos(".")
        local start_row = start[2]
        local end_row = end_[2]

        if start_row > end_row then
            start_row, end_row = end_row, start_row
        end

        for i = start_row, end_row do
            table.insert(entries, oil.get_entry_on_line(0, i))
        end

        vim.api.nvim_feedkeys(
            vim.api.nvim_replace_termcodes("<Esc>", true, false, true),
            "n",
            true
        )
    else
        table.insert(entries, oil.get_cursor_entry())
    end

    -- This removes holes in the list-like table
    entries = vim.tbl_values(entries)

    if #entries == 0 then
        vim.notify(
            "Could not find local file under cursor",
            vim.log.levels.WARN
        )
        return
    end
    local paths = {}
    for _, entry in ipairs(entries) do
        if opts.full_path then
            table.insert(paths, dir .. entry.name)
        else
            table.insert(paths, entry.name)
        end
    end
    require("vim.ui.clipboard.osc52").copy("+")(paths)
    if #paths == 1 then
        notif.info(string.format("Copied '%s' to system clipboard", paths[1]))
    else
        notif.info(string.format("Copied %d files to system clipboard", #paths))
    end
end

return M