feat: configurable keymaps + commands, README with examples

This commit is contained in:
QQ 2026-06-11 21:13:58 +08:00
parent a169b3139c
commit 0f036b6b58
2 changed files with 99 additions and 12 deletions

View File

@ -10,13 +10,65 @@
-- lazy.nvim -- lazy.nvim
{ {
'newbie/QQdock.nvim', 'newbie/QQdock.nvim',
url = 'https://git.qyhhh.top/newbie/QQdock.nvim',
dependencies = { 'akinsho/toggleterm.nvim' }, dependencies = { 'akinsho/toggleterm.nvim' },
config = function() config = function()
-- QQdock 无全局配置,直接用 require('QQdock').setup({
-- 可选:自定义尺寸
size = {
horizontal = 10,
vertical = 40,
},
-- 可选:自定义快捷键(不传则无默认,需自己手动注册)
keymaps = {
shell = { 'n', '<c-t>' },
shell_i = { 'i', '<c-t>' },
reasonix = { 'n', '<C-i>' },
lazygit = { 'n', '<leader>gg' },
},
})
end, end,
} }
``` ```
## 自定义快捷键
`keymaps` 表里的每个字段格式是 `{ mode, lhs }`。想换键就改,不想用某个功能就不传那个字段。例如只用 shell 和 lazygit不要 Reasonix
```lua
require('QQdock').setup({
keymaps = {
shell = { 'n', '<c-t>' },
shell_i = { 'i', '<c-t>' },
lazygit = { 'n', '<leader>gg' },
-- reasonix 不写 → 不注册快捷键
},
})
```
想用 `<leader>ft` 开终端:
```lua
require('QQdock').setup({
keymaps = {
shell = { 'n', '<leader>ft' },
-- …
},
})
```
**不传 `keymaps` 则不注册任何快捷键**,你可以完全手动绑定:
```lua
local Q = require('QQdock')
vim.keymap.set('n', '<leader>s', Q.shell, { noremap = true })
vim.keymap.set('n', '<leader>r', function() Q.open('reasonix') end)
```
## 终端内隐藏键
所有终端内按 **`<C-\><C-\>`**(双击 Ctrl+\)隐藏回代码,不影响 TUI 程序。
## 用法 ## 用法
```lua ```lua
@ -29,26 +81,32 @@ Q.open('btm') -- 系统监控
Q.open('yazi') -- 文件管理器 Q.open('yazi') -- 文件管理器
``` ```
## 推荐键位
```lua
vim.keymap.set({ 'n', 'i' }, '<c-t>', Q.shell, { noremap = true })
vim.keymap.set('n', '<C-i>', function() Q.open('reasonix') end)
vim.keymap.set('n', '<leader>gg', function() Q.open('lazygit') end)
```
## API ## API
| 函数 | 参数 | 作用 | | 函数 | 参数 | 作用 |
|------|------|------| |------|------|------|
| `Q.setup(opts)` | opts | 配置尺寸、快捷键、命令映射 |
| `Q.shell()` | — | 打开/关闭持久 shell | | `Q.shell()` | — | 打开/关闭持久 shell |
| `Q.open(cmd)` | cmd | 打开/关闭指定命令的持久终端 | | `Q.open(cmd)` | cmd | 打开/关闭指定命令的持久终端 |
### setup 参数
| 字段 | 类型 | 默认 | 说明 |
|------|------|------|------|
| `size.horizontal` | number | nil | 竖屏下方终端高度 |
| `size.vertical` | number | nil | 横屏右侧终端宽度 |
| `keymaps.shell` | { mode, key } | — | 普通终端快捷键 |
| `keymaps.shell_i` | { mode, key } | — | 插入模式开终端 |
| `keymaps.reasonix` | { mode, key } | — | Reasonix 快捷键 |
| `keymaps.lazygit` | { mode, key } | — | lazygit 快捷键 |
| `commands.reasonix` | string | `"reasonix"` | Reasonix 启动命令 |
| `commands.lazygit` | string | `"lazygit"` | lazygit 启动命令 |
## 特性 ## 特性
- **持久化** — toggle 显隐,终端状态保留 - **持久化** — toggle 显隐,终端状态保留
- **自适应** — 横屏右侧分屏竖屏下方分屏toggleterm 默认尺寸) - **自适应** — 横屏右侧分屏,竖屏下方分屏
- **轻量** — 仅依赖 toggleterm.nvim无其他依赖 - **轻量** — 仅依赖 toggleterm.nvim
## TODO ## TODO

View File

@ -8,6 +8,12 @@
-- horizontal = 10, -- 竖屏下方终端高度(行数) -- horizontal = 10, -- 竖屏下方终端高度(行数)
-- vertical = 40, -- 横屏右侧终端宽度(列数) -- vertical = 40, -- 横屏右侧终端宽度(列数)
-- }, -- },
-- keymaps = {
-- shell = { 'n', '<c-t>' }, -- 普通终端
-- shell_i = { 'i', '<c-t>' }, -- 插入模式也开终端
-- reasonix = { 'n', '<C-i>' }, -- Reasonix
-- lazygit = { 'n', '<leader>gg' }, -- lazygit
-- },
-- }) -- })
-- --
-- 用法: -- 用法:
@ -23,13 +29,36 @@ local config = {
horizontal = nil, -- nil = toggleterm 默认值 horizontal = nil, -- nil = toggleterm 默认值
vertical = nil, vertical = nil,
}, },
keymaps = {},
commands = {
reasonix = 'reasonix',
lazygit = 'lazygit',
},
} }
local terms = {} -- 缓存终端实例key 是命令名nil = 普通 shell local terms = {} -- 缓存终端实例key 是命令名nil = 普通 shell
---@param opts { size?: { horizontal?: integer, vertical?: integer } } ---@param opts { size?: { horizontal?: integer, vertical?: integer }, keymaps?: table, commands?: table }
function M.setup(opts) function M.setup(opts)
config = vim.tbl_deep_extend('force', config, opts or {}) config = vim.tbl_deep_extend('force', config, opts or {})
-- 注册键位
local km = config.keymaps
local function safe_map(mode, lhs, fn)
if mode and lhs and fn then
vim.keymap.set(mode, lhs, fn, { noremap = true })
end
end
if km.shell then safe_map(km.shell[1], km.shell[2], M.shell) end
if km.shell_i then safe_map(km.shell_i[1], km.shell_i[2], M.shell) end
if km.reasonix then safe_map(km.reasonix[1], km.reasonix[2], function()
local cmd = config.commands.reasonix
M.open(type(cmd) == 'function' and cmd() or cmd)
end) end
if km.lazygit then safe_map(km.lazygit[1], km.lazygit[2], function()
local cmd = config.commands.lazygit
M.open(type(cmd) == 'function' and cmd() or cmd)
end) end
end end
function M.open(cmd) function M.open(cmd)