Skip to content

Commit be9943a

Browse files
authored
Merge pull request #5 from cjodo/dev
feat: custom keymap options
2 parents f22f527 + 4a6ec32 commit be9943a

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ return {
2525
```
2626

2727
## Usage
28+
You can choose you're own custom keys for the ui menu
29+
```lua
30+
config = function()
31+
local convert = require('convert')
32+
-- defaults
33+
convert.setup({
34+
keymaps = {
35+
focus_next = { "j", "<Down>", "<Tab>" },
36+
focus_prev = { "k", "<Up>", "<S-Tab>" },
37+
close = { "<Esc>", "<C-c>", 'qq' },
38+
submit = { "<CR>", "<Space>" },
39+
}
40+
})
41+
42+
vim.keymap.set("n", "<leader>cn", "<cmd>ConvertFindNext<CR>", { desc = "Find next convertable unit" })
43+
vim.keymap.set("n", "<leader>cc", "<cmd>ConvertFindCurrent<CR>", { desc = "Find convertable unit in current line" })
44+
end
45+
46+
```
47+
2848

2949
- There are only two commands, ```:ConvertFindCurrent``` & ```:ConvertFindNext```
3050
- Convert find next will look for the next convertable unit in the file

lua/convert/calculator.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local M = {}
66
---@param from string
77
---@param to string
88
---@param val number
9+
---@return string
910
M.convert = function(from, to, val)
1011
if from == to then
1112
if from == 'hex' then

lua/convert/config.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local config = {}
2+
3+
-- set defaults
4+
config.keymaps = {
5+
focus_next = { "j", "<Down>", "<Tab>" },
6+
focus_prev = { "k", "<Up>", "<S-Tab>" },
7+
close = { "<Esc>", "<C-c>", 'qq' },
8+
submit = { "<CR>", "<Space>" },
9+
}
10+
11+
return config

lua/convert/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
local utils = require("convert.utils")
22
local ui = require("convert.ui.open_popup")
33
local events = require("convert.events")
4+
local config = require("convert.config")
45

56
events.setup()
67

78
local M = {}
89

10+
M.setup = function(opts)
11+
print("hello from setup")
12+
if opts then
13+
config.keymaps = opts.keymaps
14+
end
15+
end
16+
917
M.find_next = function()
1018
local cursor_pos = utils.get_cursor_pos()
1119
local bufnr = vim.api.nvim_get_current_buf()

lua/convert/ui/open_popup.lua

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local Menu = require("nui.menu")
22
local calculator = require("convert.calculator")
33
local utils = require("convert.utils")
4+
local config = require("convert.config")
45

56
local size_units = {
67
'px',
@@ -18,13 +19,13 @@ local color_units = {
1819
'hsl'
1920
}
2021

21-
local color_lines = {
22+
local color_menu = {
2223
Menu.item('rgb'),
2324
Menu.item('hex'),
2425
Menu.item('hsl'),
2526
}
2627

27-
local size_lines = {
28+
local size_menu = {
2829
Menu.item('px'),
2930
Menu.item('rem'),
3031
Menu.item('cm'),
@@ -40,11 +41,11 @@ M.open_win = function(found_unit)
4041
local lines = nil
4142

4243
if utils.contains(color_units, found_unit.unit) then
43-
lines = color_lines
44+
lines = color_menu
4445
end
4546

4647
if utils.contains(size_units, found_unit.unit) then
47-
lines = size_lines
48+
lines = size_menu
4849
end
4950

5051
local popup_opts = {
@@ -76,12 +77,7 @@ M.open_win = function(found_unit)
7677
lines = lines,
7778

7879
max_width = 100,
79-
keymap = {
80-
focus_next = { "j", "<Down>", "<Tab>" },
81-
focus_prev = { "k", "<Up>", "<S-Tab>" },
82-
close = { "<Esc>", "<C-c>", 'qq' },
83-
submit = { "<CR>", "<Space>" },
84-
},
80+
keymap = config.keymaps,
8581
on_submit = function(item)
8682
local from_unit = found_unit.unit
8783
local to_unit = item.text

0 commit comments

Comments
 (0)