You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

135 lines
2.8 KiB

local M = {}
local function get_yanked_info()
return ya.sync(function()
local cwd = cx.active.current.cwd
local cut = cx.yanked.is_cut
local yanked = {}
for _, f in pairs(cx.yanked) do
yanked[#yanked + 1] = { url = f.url, name = tostring(f.url:name()) }
end
return cwd, cut, yanked
end)
end
local function rename_default(name)
local suffix = os.date("-%y%m%d")
local stem = name:match("^(.+)%..-$") or name
local ext = name:match("%.(.+)$") or ""
if ext ~= "" then
return stem .. suffix .. "." .. ext
else
return name .. suffix
end
end
function M:entry(_, job)
local cwd, cut, yanked = get_yanked_info()
if #yanked == 0 then
return
end
local follow = job.args.follow or false
local resolved = {}
-- Partition: check each yanked item for conflicts
local conflicts = {}
for _, item in ipairs(yanked) do
local dest = cwd:join(item.name)
local cha = fs.cha(dest)
if cha ~= nil then
conflicts[#conflicts + 1] = { url = item.url, dest = dest, name = item.name }
else
resolved[#resolved + 1] = {
from = tostring(item.url),
to = tostring(dest),
overwrite = false,
}
end
end
-- Process conflicts
local sync_all = false
for _, item in ipairs(conflicts) do
if sync_all then
resolved[#resolved + 1] = {
from = tostring(item.url),
to = tostring(item.dest),
overwrite = true,
}
else
local choice = ya.pick({
title = "Paste conflict: " .. item.name,
items = {
"Sync (merge/overwrite)",
"Sync all remaining",
"Rename",
"Skip",
"Cancel",
},
})
-- ya.pick returns 0-based index or nil
if choice == nil or choice == 4 then
-- Cancel: paste what we have so far, then abort
if #resolved > 0 then
ya.emit("paste_resolved", {
cut = cut,
follow = follow,
items = resolved,
})
end
return
elseif choice == 0 then
-- Sync current
resolved[#resolved + 1] = {
from = tostring(item.url),
to = tostring(item.dest),
overwrite = true,
}
elseif choice == 1 then
-- Sync all remaining
sync_all = true
resolved[#resolved + 1] = {
from = tostring(item.url),
to = tostring(item.dest),
overwrite = true,
}
elseif choice == 2 then
-- Rename
local default = rename_default(item.name)
local value, event = ya.input({
title = "New name",
value = default,
})
if event == 1 then
local new_dest = cwd:join(value)
resolved[#resolved + 1] = {
from = tostring(item.url),
to = tostring(new_dest),
overwrite = false,
}
end
elseif choice == 3 then
-- Skip: do nothing
end
end
end
-- Dispatch all resolved items
if #resolved > 0 then
ya.emit("paste_resolved", {
cut = cut,
follow = follow,
items = resolved,
})
end
end
return M