Skip to content

Commit

Permalink
feat: begining of UI
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed Nov 29, 2023
1 parent a46ea79 commit 0d5d8bb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lua/hawtkeys/score.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,17 @@ local function process_string(str)
local find_mapping = function(maps, lhs)
for _, value in ipairs(maps) do
if value.lhs == lhs then
return true
return value.rhs
end
end
return false
end

for i = #sortedScores, 1, -1 do
if find_mapping(already_used_keys, config.leader .. sortedScores[i].combo) then
table.remove(sortedScores, i)
-- table.remove(sortedScores, i)
local mapping = find_mapping(already_used_keys, config.leader .. sortedScores[i].combo)
sortedScores[i].already_mapped = mapping
end
end
end
Expand Down Expand Up @@ -149,6 +151,17 @@ local function score(str)
print()
end

local function scoreTable(str)
local results = top5(process_string(str))
local resultTable = {}
for _, data in ipairs(results)
do
table.insert(resultTable, "Key: " .. highlight_desc(str, data.combo) .. "(<leader>" .. data.combo .. "), Key score " .. data.score .. ", Already mapped: " .. tostring(data.already_mapped))
end
return resultTable
end

return {
Score = score,
ScoreTable = scoreTable
}
58 changes: 57 additions & 1 deletion lua/hawtkeys/ui.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
M = {}
Hawtkeys = require('hawtkeys.score')
M.search = function(text)
--remove % from start of text
text = string.gsub(text, "^%%", "")
-- vim.api.nvim_buf_set_lines(ResultBuf, 0, -1, false, Hawtkeys.ScoreTable(text))
ok, msg = pcall(vim.api.nvim_buf_set_lines, ResultBuf, 0, -1, false, Hawtkeys.ScoreTable(text))
end

M.show = function()
print("show")
ResultBuf = vim.api.nvim_create_buf(false, true)
local ui = vim.api.nvim_list_uis()[1]
local width = 100
local height = 30
ResultWin = vim.api.nvim_open_win(ResultBuf, true, {
relative = "editor",
width = width,
height = height,
col = (ui.width/2) - (width/2),
row = (ui.height/2) - (height/2),
anchor = "NW",
footer = "Suggested Keybindings",
footer_pos = "center",
border = "single",
noautocmd = true,
})
local searchBuf = vim.api.nvim_create_buf(false, true)
SearchWin = vim.api.nvim_open_win(searchBuf, true, {
relative = "editor",
width = width,
height = 1,
col = (ui.width/2) - (width/2),
row = (ui.height/2) - (height/2) - 2,
anchor = "NW",
border = "single",
style = "minimal",
title = "Enter Command Description",
title_pos = "center",

})
--vim.api.nvim_set_option_value("buftype", "prompt", {buf = searchBuf})
-- when text is entered set the same value in the results buffer
-- set escape to close the window
vim.api.nvim_buf_set_keymap(searchBuf, "i", "<esc>", "<cmd>lua require('hawtkeys.ui').hide()<cr>", {noremap = true, silent = true})
vim.api.nvim_buf_set_keymap(searchBuf, "n", "<esc>", "<cmd>lua require('hawtkeys.ui').hide()<cr>", {noremap = true, silent = true})
-- set enter to go to normal modes
vim.api.nvim_buf_set_keymap(searchBuf, "i", "<cr>", "<cmd>lua require('hawtkeys.ui').search(vim.api.nvim_buf_get_lines("..searchBuf..", 0, 1, false)[1])<cr>", {noremap = true, silent = true})
-- subscribe to changed text in searchBuf
vim.api.nvim_buf_attach(searchBuf, false, {
on_lines = function()
end
})
--
vim.api.nvim_set_current_win(SearchWin)
vim.cmd("startinsert")
end

M.hide = function()
vim.api.nvim_win_close(ResultWin, true)
vim.api.nvim_win_close(SearchWin, true)
end

return M

0 comments on commit 0d5d8bb

Please sign in to comment.