summaryrefslogtreecommitdiff
path: root/xplr/plugins/dragon/src/init.lua
blob: f0fdc9f06985446cd2aceb600948b15501dd240f (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
local function setup(args)
  local xplr = xplr

  if args == nil then
    args = {}
  end

  if args.mode == nil then
    args.mode = "selection_ops"
  end

  if args.key == nil then
    args.key = "D"
  end

  if args.keep_selection == nil then
    args.keep_selection = false
  end

  if args.drag_args == nil then
    args.drag_args = ""
  end

  if args.drop_args == nil then
    args.drop_args = ""
  end

  xplr.fn.custom.dragon_drag_n_drop = function(app)
    local files = {}
    local count = 0
    local cmd = nil

    for i, node in ipairs(app.selection) do
      table.insert(files, node.absolute_path)
      count = i
    end

    if count == 0 then
      cmd = "(dragon --target " .. args.drop_args .. " 2> /dev/null | xargs -rl curl -sLO) &\ntrue"
    elseif count == 1 then
      cmd = "dragon --and-exit " .. args.drag_args .. " '" .. files[1] .. "' > /dev/null 2>&1 &\ntrue"
    else
      cmd = "dragon " .. args.drag_args .. " '" .. table.concat(files, "' '") .. "' > /dev/null 2>&1 &\ntrue"
    end

    os.execute(cmd)

    if not args.keep_selection then
      return { "ClearSelection" }
    end
  end

  xplr.config.modes.builtin[args.mode].key_bindings.on_key[args.key] = {
    help = "drag & drop",
    messages = {
      { CallLuaSilently = "custom.dragon_drag_n_drop" },
      "PopMode",
    },
  }
end

return { setup = setup }