diff --git a/README.md b/README.md index 9fc1c55..3555361 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/genghis/config.lua b/lua/genghis/config.lua index cec7bec..6163cf1 100644 --- a/lua/genghis/config.lua +++ b/lua/genghis/config.lua @@ -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 diff --git a/lua/genghis/operations/other.lua b/lua/genghis/operations/other.lua index d78a826..3016379 100644 --- a/lua/genghis/operations/other.lua +++ b/lua/genghis/operations/other.lua @@ -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))