refactor: `trashCmd` config

remotes/origin/HEAD
Chris Grieser 2024-06-20 21:07:04 +02:00
parent 1ca65d9c56
commit 4bfd8aec89
3 changed files with 25 additions and 21 deletions

View File

@ -58,7 +58,7 @@ require("genghis").setup {
enabled = true,
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",
}
```
@ -86,7 +86,7 @@ Or you can use the ex command `:Genghis` with the respective sub-command:
## Available commands
### File Operations
### File operations
- `.createNewFile`: Create a new file.
- `.duplicateFile`: Duplicate the current file.
- `.moveSelectionToNewFile`: Prompts for a new file name

View File

@ -1,6 +1,17 @@
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
local defaultConfig = {
backdrop = {
@ -8,7 +19,7 @@ local defaultConfig = {
blend = 50,
},
-- cli name, default is `trash` on Mac and Windows, and `gio trash` on Linux
trashCmd = nil,
trashCmd = setDefaultTrashCmd(),
}
M.config = defaultConfig

View File

@ -8,8 +8,9 @@ function M.chmodx()
local perm = vim.fn.getfperm(filename)
perm = perm:gsub("r(.)%-", "r%1x") -- add x to every group that has r
vim.fn.setfperm(filename, perm)
u.notify("Execution Permission granted.")
vim.cmd.edit()
vim.cmd.edit() -- reload the file
end
function M.trashFile(opts)
@ -19,25 +20,17 @@ function M.trashFile(opts)
return
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")
local oldFilePath = vim.api.nvim_buf_get_name(0)
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
u.bwipeout()
u.notify(("%q deleted."):format(oldName))