refactor: `trashCmd` config
parent
1ca65d9c56
commit
4bfd8aec89
|
|
@ -58,7 +58,7 @@ require("genghis").setup {
|
||||||
enabled = true,
|
enabled = true,
|
||||||
blend = 50,
|
blend = 50,
|
||||||
},
|
},
|
||||||
-- cli name, default is `trash` on Mac and Windows, and `gio trash` on Linux
|
-- default is ` "trash" ` on Mac/Windows, and `{ "gio", "trash" }` on Linux
|
||||||
trashCmd = "trash",
|
trashCmd = "trash",
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
@ -86,7 +86,7 @@ Or you can use the ex command `:Genghis` with the respective sub-command:
|
||||||
|
|
||||||
## Available commands
|
## Available commands
|
||||||
|
|
||||||
### File Operations
|
### File operations
|
||||||
- `.createNewFile`: Create a new file.
|
- `.createNewFile`: Create a new file.
|
||||||
- `.duplicateFile`: Duplicate the current file.
|
- `.duplicateFile`: Duplicate the current file.
|
||||||
- `.moveSelectionToNewFile`: Prompts for a new file name
|
- `.moveSelectionToNewFile`: Prompts for a new file name
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,17 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
---@return string|string[]
|
||||||
|
local function setDefaultTrashCmd()
|
||||||
|
local osTrashCmd
|
||||||
|
local system = vim.uv.os_uname().sysname:lower()
|
||||||
|
if system == "darwin" then osTrashCmd = "trash" end
|
||||||
|
if system:find("windows") then osTrashCmd = "trash" end
|
||||||
|
if system:find("linux") then osTrashCmd = { "gio", "trash" } end
|
||||||
|
assert(osTrashCmd, "Unknown operating system. Please provide a custom `trashCmd`.")
|
||||||
|
return osTrashCmd
|
||||||
|
end
|
||||||
|
|
||||||
---@class Genghis.config
|
---@class Genghis.config
|
||||||
local defaultConfig = {
|
local defaultConfig = {
|
||||||
backdrop = {
|
backdrop = {
|
||||||
|
|
@ -8,7 +19,7 @@ local defaultConfig = {
|
||||||
blend = 50,
|
blend = 50,
|
||||||
},
|
},
|
||||||
-- cli name, default is `trash` on Mac and Windows, and `gio trash` on Linux
|
-- cli name, default is `trash` on Mac and Windows, and `gio trash` on Linux
|
||||||
trashCmd = nil,
|
trashCmd = setDefaultTrashCmd(),
|
||||||
}
|
}
|
||||||
|
|
||||||
M.config = defaultConfig
|
M.config = defaultConfig
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,9 @@ function M.chmodx()
|
||||||
local perm = vim.fn.getfperm(filename)
|
local perm = vim.fn.getfperm(filename)
|
||||||
perm = perm:gsub("r(.)%-", "r%1x") -- add x to every group that has r
|
perm = perm:gsub("r(.)%-", "r%1x") -- add x to every group that has r
|
||||||
vim.fn.setfperm(filename, perm)
|
vim.fn.setfperm(filename, perm)
|
||||||
|
|
||||||
u.notify("Execution Permission granted.")
|
u.notify("Execution Permission granted.")
|
||||||
vim.cmd.edit()
|
vim.cmd.edit() -- reload the file
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.trashFile(opts)
|
function M.trashFile(opts)
|
||||||
|
|
@ -19,25 +20,17 @@ function M.trashFile(opts)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- user-provided trashCmd or os-specific default
|
|
||||||
local trashCmd = require("genghis.config").config.trashCmd
|
|
||||||
if not trashCmd then
|
|
||||||
local system = vim.uv.os_uname().sysname:lower()
|
|
||||||
local defaultCmd
|
|
||||||
if system == "darwin" then defaultCmd = "trash" end
|
|
||||||
if system:find("windows") then defaultCmd = "trash" end
|
|
||||||
if system:find("linux") then defaultCmd = "gio trash" end
|
|
||||||
assert(defaultCmd, "Unknown operating system. Please provide a custom `trashCmd`.")
|
|
||||||
trashCmd = defaultCmd
|
|
||||||
end
|
|
||||||
|
|
||||||
local trashArgs = vim.split(trashCmd, " ")
|
|
||||||
local oldFilePath = vim.api.nvim_buf_get_name(0)
|
|
||||||
table.insert(trashArgs, oldFilePath)
|
|
||||||
|
|
||||||
vim.cmd("silent! update")
|
vim.cmd("silent! update")
|
||||||
|
local oldFilePath = vim.api.nvim_buf_get_name(0)
|
||||||
local oldName = vim.fs.basename(oldFilePath)
|
local oldName = vim.fs.basename(oldFilePath)
|
||||||
local result = vim.system(trashArgs):wait()
|
|
||||||
|
-- execute the trash command
|
||||||
|
local trashCmd = require("genghis.config").config.trashCmd
|
||||||
|
if type(trashCmd) == "string" then trashCmd = { trashCmd } end
|
||||||
|
table.insert(trashCmd, oldFilePath)
|
||||||
|
local result = vim.system(trashCmd):wait()
|
||||||
|
|
||||||
|
-- handle the result
|
||||||
if result.code == 0 then
|
if result.code == 0 then
|
||||||
u.bwipeout()
|
u.bwipeout()
|
||||||
u.notify(("%q deleted."):format(oldName))
|
u.notify(("%q deleted."):format(oldName))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue