refactor: `chmodx` and `trashFile` as separate module
parent
41463d4eb0
commit
a788d44c97
36
README.md
36
README.md
|
|
@ -13,9 +13,9 @@ Lightweight and quick file operations without being a full-blown file manager.
|
|||
- [Configuration](#configuration)
|
||||
- [Usage](#usage)
|
||||
- [Available Commands](#available-commands)
|
||||
* [File Operation Commands](#file-operation-commands)
|
||||
* [Utility Commands](#utility-commands)
|
||||
* [Path Copying Commands](#path-copying-commands)
|
||||
* [File Operations](#file-operations)
|
||||
* [Path Copying](#path-copying)
|
||||
* [Other operations](#other-operations)
|
||||
* [Disable Ex-Commands](#disable-ex-commands)
|
||||
- [How is this different from `vim.eunuch`?](#how-is-this-different-from-vimeunuch)
|
||||
- [Why that Name](#why-that-name)
|
||||
|
|
@ -73,7 +73,7 @@ keymap("x", "<leader>x", function() require("genghis").moveSelectionToNewFile()
|
|||
|
||||
## Available Commands
|
||||
|
||||
### File Operation Commands
|
||||
### File Operations
|
||||
- `.createNewFile` or `:New`: Create a new file.
|
||||
- `.duplicateFile` or `:Duplicate`: Duplicate the current file.
|
||||
- `.moveSelectionToNewFile` or `:NewFromSelection`: Prompts for a new file name
|
||||
|
|
@ -93,20 +93,7 @@ The following applies to all commands above:
|
|||
3. All movement and renaming commands update `import` statements to the renamed
|
||||
file (if the LSP supports `workspace/willRenameFiles`).
|
||||
|
||||
### Utility Commands
|
||||
- `.chmodx` or `:Chmodx`: Makes current file executable. Equivalent to `chmod
|
||||
+x`.
|
||||
- `.trashFile` or `:Trash`: Move the current file
|
||||
to the trash location.
|
||||
* Defaults to `gio trash` on *Linux*, `trash` on *Mac* and *Windows*.
|
||||
* If [bufdelete.nvim](https://github.com/famiu/bufdelete.nvim) is available,
|
||||
`require'bufdelete.nvim'.bufwipeout` would be used to keep window layout
|
||||
intact instead of `vim.cmd.bwipeout`.
|
||||
|
||||
> [!NOTE]
|
||||
> The trash CLIs are usually not available by default, and must be installed.
|
||||
|
||||
### Path Copying Commands
|
||||
### Path Copying
|
||||
- `.copyFilename` or `:CopyFilename`: Copy the file name.
|
||||
- `.copyFilepath` or `:CopyFilepath`: Copy the absolute file path.
|
||||
- `.copyFilepathWithTilde` or `:CopyFilepathWithTilde`: Copy the absolute file
|
||||
|
|
@ -119,6 +106,19 @@ to the trash location.
|
|||
|
||||
All commands use the system clipboard.
|
||||
|
||||
### Other operations
|
||||
- `.chmodx` or `:Chmodx`: Makes current file executable. Equivalent to `chmod
|
||||
+x`.
|
||||
- `.trashFile` or `:Trash`: Move the current file
|
||||
to the trash location.
|
||||
* Defaults to `gio trash` on *Linux*, `trash` on *Mac* and *Windows*.
|
||||
* If [bufdelete.nvim](https://github.com/famiu/bufdelete.nvim) is available,
|
||||
`require'bufdelete.nvim'.bufwipeout` would be used to keep window layout
|
||||
intact instead of `vim.cmd.bwipeout`.
|
||||
|
||||
> [!NOTE]
|
||||
> The trash CLIs are usually not available by default, and must be installed.
|
||||
|
||||
### Disable Ex-Commands
|
||||
|
||||
```lua
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ end
|
|||
|
||||
local M = {}
|
||||
|
||||
-- redirect to `operations.copy` or `operations.file`
|
||||
-- redirect to to the correct module
|
||||
setmetatable(M, {
|
||||
__index = function(_, key)
|
||||
return function(...)
|
||||
|
|
@ -17,6 +17,7 @@ setmetatable(M, {
|
|||
end
|
||||
|
||||
local module = vim.startswith(key, "copy") and "copy" or "file"
|
||||
if key == "chmodx" or key == "trashFile" then module = "other" end
|
||||
require("genghis.operations." .. module)[key](...)
|
||||
end
|
||||
end,
|
||||
|
|
|
|||
|
|
@ -180,50 +180,6 @@ end
|
|||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
---Makes current file executable
|
||||
function M.chmodx()
|
||||
local filename = vim.api.nvim_buf_get_name(0)
|
||||
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()
|
||||
end
|
||||
|
||||
function M.trashFile(opts)
|
||||
---DEPRECATION
|
||||
if opts then
|
||||
u.notify("The `trashCmd` option has been moved to the setup call.", "warn")
|
||||
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 oldName = vim.fs.basename(oldFilePath)
|
||||
local result = vim.system(trashArgs):wait()
|
||||
if result.code == 0 then
|
||||
u.bwipeout()
|
||||
u.notify(("%q deleted."):format(oldName))
|
||||
else
|
||||
local outmsg = (result.stdout or "") .. (result.stderr or "")
|
||||
u.notify(("Trashing %q failed: " .. outmsg):format(oldName), "error")
|
||||
end
|
||||
end
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
return M
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
local M = {}
|
||||
local u = require("genghis.support.utils")
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
---Makes current file executable
|
||||
function M.chmodx()
|
||||
local filename = vim.api.nvim_buf_get_name(0)
|
||||
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()
|
||||
end
|
||||
|
||||
function M.trashFile(opts)
|
||||
---DEPRECATION
|
||||
if opts then
|
||||
u.notify("The `trashCmd` option has been moved to the setup call.", "warn")
|
||||
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 oldName = vim.fs.basename(oldFilePath)
|
||||
local result = vim.system(trashArgs):wait()
|
||||
if result.code == 0 then
|
||||
u.bwipeout()
|
||||
u.notify(("%q deleted."):format(oldName))
|
||||
else
|
||||
local outmsg = (result.stdout or "") .. (result.stderr or "")
|
||||
u.notify(("Trashing %q failed: " .. outmsg):format(oldName), "error")
|
||||
end
|
||||
end
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
return M
|
||||
Loading…
Reference in New Issue