2025-07-29 17:41:41 +02:00
|
|
|
local get_hovered_url = ya.sync(function(state)
|
2025-07-30 10:39:14 +02:00
|
|
|
return Url(cx.active.current.hovered.url)
|
2025-07-29 17:41:41 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
local get_selected_files = ya.sync(function(state)
|
|
|
|
|
local selected = {}
|
|
|
|
|
for _, url in pairs(cx.active.selected) do
|
|
|
|
|
selected[#selected + 1] = url
|
|
|
|
|
end
|
|
|
|
|
return selected
|
|
|
|
|
end)
|
|
|
|
|
|
2025-07-30 10:39:14 +02:00
|
|
|
local get_wanted_dir_name = function(hovered_url)
|
|
|
|
|
local url = get_selected_files()[0] or hovered_url
|
2025-07-29 17:41:41 +02:00
|
|
|
local name = url.stem
|
|
|
|
|
local edited, event = ya.input {
|
|
|
|
|
title = "Dir name:",
|
|
|
|
|
value = name,
|
|
|
|
|
position = {"hovered", x = 0, y = 0, w = 200, h = 3}
|
|
|
|
|
}
|
|
|
|
|
ya.dbg("got dir name from user", edited, "with confirm", event)
|
|
|
|
|
if event == 1 then -- did user confirm?
|
|
|
|
|
return Url(edited)
|
|
|
|
|
else
|
|
|
|
|
return undefined
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-07-30 10:39:14 +02:00
|
|
|
local get_files_to_move = function(hovered_url)
|
2025-07-29 17:41:41 +02:00
|
|
|
local files_to_move = get_selected_files()
|
|
|
|
|
if #files_to_move == 0 then
|
2025-07-30 10:39:14 +02:00
|
|
|
files_to_move[1] = hovered_url
|
2025-07-29 17:41:41 +02:00
|
|
|
end
|
|
|
|
|
return files_to_move
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
entry = function(state, job)
|
2025-07-30 10:39:14 +02:00
|
|
|
local hovered_url = get_hovered_url()
|
|
|
|
|
local wanted_dir_name = get_wanted_dir_name(hovered_url)
|
2025-07-29 17:41:41 +02:00
|
|
|
if not wanted_dir_name then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
local ok, err = fs.create("dir", wanted_dir_name)
|
|
|
|
|
if not ok then
|
2025-07-30 10:39:14 +02:00
|
|
|
ya.notify { title = "Failed to create directory", content = tostring(err), timeout = 6.5, level = "error" }
|
2025-07-29 17:41:41 +02:00
|
|
|
return
|
|
|
|
|
end
|
2025-07-30 10:39:14 +02:00
|
|
|
local files_to_move = get_files_to_move(hovered_url)
|
2025-07-29 17:41:41 +02:00
|
|
|
ya.dbg("files to move", files_to_move)
|
|
|
|
|
for i, file in pairs(files_to_move) do
|
|
|
|
|
local dest = wanted_dir_name:join(file.name)
|
|
|
|
|
ya.dbg("moving file", i, "of", #files_to_move, file, "to", dest)
|
|
|
|
|
os.rename(tostring(file), tostring(dest))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
}
|