Skip to content

Commit

Permalink
fix: clipboard behavior for copying or moving.
Browse files Browse the repository at this point in the history
  • Loading branch information
obaland committed Mar 26, 2023
1 parent 701e0f5 commit b658c79
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
11 changes: 4 additions & 7 deletions lua/vfiler/actions/file_operation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function M.copy(vfiler, context, view)
return
end

clipboard.copy(selected)
clipboard.copy_to_clipboard(selected)
if #selected == 1 then
core.message.info('Copy to the clipboard - %s', selected[1].name)
else
Expand All @@ -118,9 +118,7 @@ function M.copy_to_filer(vfiler, context, view)
end

-- Copy to linked filer
local cb = clipboard.copy(selected)
cb:paste(linked:get_root_item())
clipboard.clear()
clipboard.copy(selected, linked:get_root_item())

-- clear selected mark
for _, item in ipairs(selected) do
Expand Down Expand Up @@ -180,7 +178,7 @@ function M.move(vfiler, context, view)
return
end

clipboard.move(selected)
clipboard.move_to_clipboard(selected)
if #selected == 1 then
core.message.info('Move to the clipboard - %s', selected[1].name)
else
Expand All @@ -207,8 +205,7 @@ function M.move_to_filer(vfiler, context, view)
end

-- Move to linked filer
local cb = clipboard.move(selected)
cb:paste(linked:get_root_item())
clipboard.move(selected, linked:get_root_item())
VFiler.foreach(buffer.reload)
end

Expand Down
76 changes: 48 additions & 28 deletions lua/vfiler/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ local current_functor = nil
local PasteFunctor = {}
PasteFunctor.__index = PasteFunctor

function PasteFunctor.new(items)
return setmetatable({ _items = items }, PasteFunctor)
function PasteFunctor.new(items, clear_after_paste)
return setmetatable({
_items = items,
_clear_after_paste = clear_after_paste
}, PasteFunctor)
end

function PasteFunctor:paste(directory)
function PasteFunctor:paste(destdir)
local pasted = {}
for _, item in ipairs(self._items) do
local skipped = false
local destpath = core.path.join(directory.path, item.name)
local destpath = core.path.join(destdir.path, item.name)
if core.path.exists(destpath) then
if cmdline.util.confirm_overwrite(item.name) ~= cmdline.choice.YES then
skipped = true
Expand All @@ -24,14 +27,17 @@ function PasteFunctor:paste(directory)
if not skipped then
local new = self._on_paste(item, destpath)
if new then
directory:add(new)
destdir:add(new)
table.insert(pasted, new)
end
end
end

if #pasted > 0 then
self._on_completed(pasted, directory)
self._on_completed(pasted, destdir)
if self._clear_after_paste then
current_functor = nil
end
else
return false
end
Expand All @@ -40,12 +46,8 @@ end

local M = {}

function M.clear()
current_functor = nil
end

function M.copy(items)
local functor = PasteFunctor.new(items)
local function new_copy_functor(items, clear_after_paste)
local functor = PasteFunctor.new(items, clear_after_paste)

functor._on_paste = function(item, destpath)
local new = item:copy(destpath)
Expand All @@ -56,28 +58,23 @@ function M.copy(items)
return new
end

functor._on_completed = function(pasted_items, directory)
functor._on_completed = function(pasted_items, destdir)
if #pasted_items == 1 then
core.message.info(
'Copied to "%s" - %s',
directory.path,
destdir.path,
pasted_items[1].name
)
elseif #pasted_items > 1 then
core.message.info('Copied to "%s" - %d', directory.path, #pasted_items)
core.message.info('Copied to "%s" - %d', destdir.path, #pasted_items)
end
end

current_functor = functor
return functor
end

function M.get_current()
return current_functor
end

function M.move(items)
local functor = PasteFunctor.new(items)
local function new_move_functor(items, clear_after_paste)
local functor = PasteFunctor.new(items, clear_after_paste)

functor._on_paste = function(item, destpath)
local new = item:move(destpath)
Expand All @@ -88,24 +85,47 @@ function M.move(items)
return new
end

functor._on_completed = function(pasted_items, directory)
functor._on_completed = function(pasted_items, destdir)
if #pasted_items == 1 then
core.message.info(
'Move to "%s" - %s',
directory.path,
destdir.path,
pasted_items[1].name
)
elseif #pasted_items > 1 then
core.message.info('Move to "%s" - %d', directory.path, #pasted_items)
core.message.info('Move to "%s" - %d', destdir.path, #pasted_items)
end
-- NOTE: It will be cut and paste, so clear it.
M.clear()
end

current_functor = functor
return functor
end

function M.clear()
current_functor = nil
end

function M.copy(items, destdir)
local functor = new_copy_functor(items, false)
functor:paste(destdir)
end

function M.copy_to_clipboard(items)
current_functor = new_copy_functor(items, false)
end

function M.get_current()
return current_functor
end

function M.move(items, destdir)
local functor = new_move_functor(items, false)
functor:paste(destdir)
end

function M.move_to_clipboard(items)
current_functor = new_move_functor(items, true)
end

function M.yank(content)
-- for register
vim.fn.setreg('"', content)
Expand Down
4 changes: 2 additions & 2 deletions lua/vfiler/libs/filesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ end

function M.copy_directory(src, dest)
local command = copy_directory_format:format(escape(src), escape(dest))
vim.fn.system(command)
return vim.fn.system(command)
end

function M.copy_file(src, dest)
local command = copy_file_format:format(escape(src), escape(dest))
vim.fn.system(command)
return vim.fn.system(command)
end

function M.execute(path)
Expand Down

0 comments on commit b658c79

Please sign in to comment.