feat: add use_trash option (#394)

This commit is contained in:
dyphire
2022-12-23 22:51:37 +08:00
committed by GitHub
parent 4c706b9303
commit e33db65c1c
3 changed files with 28 additions and 1 deletions

View File

@@ -380,7 +380,29 @@ end
-- `status:number(<0=error), stdout, stderr, error_string, killed_by_us:boolean`
---@param path string
function delete_file(path)
local args = state.os == 'windows' and {'cmd', '/C', 'del', path} or {'rm', path}
if state.os == 'windows' then
if options.use_trash then
local ps_code = [[
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('__path__', 'OnlyErrorDialogs', 'SendToRecycleBin')
]]
local escaped_path = string.gsub(path, "'", "''")
escaped_path = string.gsub(escaped_path, "", "")
escaped_path = string.gsub(escaped_path, "%%", "%%%%")
ps_code = string.gsub(ps_code, "__path__", escaped_path)
args = { 'powershell', '-NoProfile', '-Command', ps_code }
else
args = { 'cmd', '/C', 'del', path }
end
else
if options.use_trash then
--On Linux and Macos the app trash-cli/trash must be installed first.
args = { 'trash', path }
else
args = { 'rm', path }
end
end
return mp.command_native({
name = 'subprocess',
args = args,