diff --git a/lua/hawtkeys/score.lua b/lua/hawtkeys/score.lua index eb3d0c2..1d1c4e3 100644 --- a/lua/hawtkeys/score.lua +++ b/lua/hawtkeys/score.lua @@ -96,7 +96,7 @@ 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 @@ -104,7 +104,9 @@ local function process_string(str) 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 @@ -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) .. "(" .. data.combo .. "), Key score " .. data.score .. ", Already mapped: " .. tostring(data.already_mapped)) + end + return resultTable + end + return { Score = score, + ScoreTable = scoreTable } diff --git a/lua/hawtkeys/ui.lua b/lua/hawtkeys/ui.lua index 4ea69db..33c9cea 100644 --- a/lua/hawtkeys/ui.lua +++ b/lua/hawtkeys/ui.lua @@ -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", "", "lua require('hawtkeys.ui').hide()", {noremap = true, silent = true}) +vim.api.nvim_buf_set_keymap(searchBuf, "n", "", "lua require('hawtkeys.ui').hide()", {noremap = true, silent = true}) +-- set enter to go to normal modes + vim.api.nvim_buf_set_keymap(searchBuf, "i", "", "lua require('hawtkeys.ui').search(vim.api.nvim_buf_get_lines("..searchBuf..", 0, 1, false)[1])", {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