refactor!: the `trashCmd` for `.trashFile()` now set via `setup` call

remotes/origin/HEAD
Chris Grieser 2024-06-20 20:04:18 +02:00
parent ad0830b013
commit 41463d4eb0
4 changed files with 58 additions and 16 deletions

View File

@ -9,7 +9,9 @@ Lightweight and quick file operations without being a full-blown file manager.
<!-- toc --> <!-- toc -->
- [Features](#features) - [Features](#features)
- [Installation and Setup](#installation-and-setup) - [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Available Commands](#available-commands) - [Available Commands](#available-commands)
* [File Operation Commands](#file-operation-commands) * [File Operation Commands](#file-operation-commands)
* [Utility Commands](#utility-commands) * [Utility Commands](#utility-commands)
@ -32,7 +34,7 @@ Lightweight and quick file operations without being a full-blown file manager.
when no extension is given, or the ability to use vim motions in the input when no extension is given, or the ability to use vim motions in the input
field. field.
## Installation and Setup ## Installation
```lua ```lua
-- lazy.nvim -- lazy.nvim
@ -42,8 +44,18 @@ Lightweight and quick file operations without being a full-blown file manager.
use {"chrisgrieser/nvim-genghis", requires = "stevearc/dressing.nvim"} use {"chrisgrieser/nvim-genghis", requires = "stevearc/dressing.nvim"}
``` ```
`nvim-genghis` (and `dressing.nvim`) require no `.setup()` function. Just create ## Configuration
keybindings for the commands you want to use: The `setup` call is optional.
```lua
-- default config
require("genghis").setup {
-- cli name, default is `trash` on Mac and Windows, and `gio trash` on Linux
trashCmd = "trash",
}
```
## Usage
```lua ```lua
local keymap = vim.keymap.set local keymap = vim.keymap.set
@ -84,7 +96,7 @@ The following applies to all commands above:
### Utility Commands ### Utility Commands
- `.chmodx` or `:Chmodx`: Makes current file executable. Equivalent to `chmod - `.chmodx` or `:Chmodx`: Makes current file executable. Equivalent to `chmod
+x`. +x`.
- `.trashFile{trashCmd = "your_cli"}` or `:Trash`: Move the current file - `.trashFile` or `:Trash`: Move the current file
to the trash location. to the trash location.
* Defaults to `gio trash` on *Linux*, `trash` on *Mac* and *Windows*. * Defaults to `gio trash` on *Linux*, `trash` on *Mac* and *Windows*.
* If [bufdelete.nvim](https://github.com/famiu/bufdelete.nvim) is available, * If [bufdelete.nvim](https://github.com/famiu/bufdelete.nvim) is available,
@ -92,7 +104,7 @@ to the trash location.
intact instead of `vim.cmd.bwipeout`. intact instead of `vim.cmd.bwipeout`.
> [!NOTE] > [!NOTE]
> The trash CLIs are not available by default, and must be installed. > The trash CLIs are usually not available by default, and must be installed.
### Path Copying Commands ### Path Copying Commands
- `.copyFilename` or `:CopyFilename`: Copy the file name. - `.copyFilename` or `:CopyFilename`: Copy the file name.

17
lua/genghis/config.lua Normal file
View File

@ -0,0 +1,17 @@
local M = {}
--------------------------------------------------------------------------------
---@class Genghis.config
local defaultConfig = {
trashCmd = nil,
}
M.config = defaultConfig
---@param userConfig? Genghis.config
function M.setup(userConfig)
M.config = vim.tbl_deep_extend("force", defaultConfig, userConfig or {})
end
--------------------------------------------------------------------------------
return M

View File

@ -11,6 +11,11 @@ local M = {}
setmetatable(M, { setmetatable(M, {
__index = function(_, key) __index = function(_, key)
return function(...) return function(...)
if key == "setup" then
require("genghis.config").setup(...)
return
end
local module = vim.startswith(key, "copy") and "copy" or "file" local module = vim.startswith(key, "copy") and "copy" or "file"
require("genghis.operations." .. module)[key](...) require("genghis.operations." .. module)[key](...)
end end

View File

@ -190,16 +190,24 @@ function M.chmodx()
vim.cmd.edit() vim.cmd.edit()
end end
---@param opts? { trashCmd: string }
function M.trashFile(opts) function M.trashFile(opts)
local userCmd = opts and opts.trashCmd ---DEPRECATION
local system = vim.uv.os_uname().sysname:lower() if opts then
local defaultCmd u.notify("The `trashCmd` option has been moved to the setup call.", "warn")
if system == "darwin" then defaultCmd = "trash" end return
if system:find("windows") then defaultCmd = "trash" end end
if system:find("linux") then defaultCmd = "gio trash" end
local trashCmd = userCmd or defaultCmd -- user-provided trashCmd or os-specific default
assert(defaultCmd, "Unknown operating system. Please provide a custom trashCmd.") 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 trashArgs = vim.split(trashCmd, " ")
local oldFilePath = vim.api.nvim_buf_get_name(0) local oldFilePath = vim.api.nvim_buf_get_name(0)
@ -210,7 +218,7 @@ function M.trashFile(opts)
local result = vim.system(trashArgs):wait() local result = vim.system(trashArgs):wait()
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))
else else
local outmsg = (result.stdout or "") .. (result.stderr or "") local outmsg = (result.stdout or "") .. (result.stderr or "")
u.notify(("Trashing %q failed: " .. outmsg):format(oldName), "error") u.notify(("Trashing %q failed: " .. outmsg):format(oldName), "error")