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 -->
- [Features](#features)
- [Installation and Setup](#installation-and-setup)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Available Commands](#available-commands)
* [File Operation Commands](#file-operation-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
field.
## Installation and Setup
## Installation
```lua
-- 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"}
```
`nvim-genghis` (and `dressing.nvim`) require no `.setup()` function. Just create
keybindings for the commands you want to use:
## Configuration
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
local keymap = vim.keymap.set
@ -84,7 +96,7 @@ The following applies to all commands above:
### Utility Commands
- `.chmodx` or `:Chmodx`: Makes current file executable. Equivalent to `chmod
+x`.
- `.trashFile{trashCmd = "your_cli"}` or `:Trash`: Move the current file
- `.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,
@ -92,7 +104,7 @@ to the trash location.
intact instead of `vim.cmd.bwipeout`.
> [!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
- `.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, {
__index = function(_, key)
return function(...)
if key == "setup" then
require("genghis.config").setup(...)
return
end
local module = vim.startswith(key, "copy") and "copy" or "file"
require("genghis.operations." .. module)[key](...)
end

View File

@ -190,16 +190,24 @@ function M.chmodx()
vim.cmd.edit()
end
---@param opts? { trashCmd: string }
function M.trashFile(opts)
local userCmd = opts and opts.trashCmd
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
local trashCmd = userCmd or defaultCmd
assert(defaultCmd, "Unknown operating system. Please provide a custom trashCmd.")
---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)
@ -210,7 +218,7 @@ function M.trashFile(opts)
local result = vim.system(trashArgs):wait()
if result.code == 0 then
u.bwipeout()
u.notify(("%q deleted"):format(oldName))
u.notify(("%q deleted."):format(oldName))
else
local outmsg = (result.stdout or "") .. (result.stderr or "")
u.notify(("Trashing %q failed: " .. outmsg):format(oldName), "error")