Skip to content

Commit ae6ff4c

Browse files
mpluspclaude
andcommitted
fix: Improve arrow key handling in PackMenu
- Add comprehensive string-based key handling for characters converted from numbers - Support both string and number formats for arrow keys and regular keys - Add test case for arrow key navigation in PackMenu - Include debug line (commented) for troubleshooting key input issues - Ensure consistent behavior across different terminal environments 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent dfb024d commit ae6ff4c

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lua/pack-manager/ui.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ function M.menu()
380380
repeat
381381
vim.cmd('redraw')
382382
key = vim.fn.getchar()
383+
384+
-- Debug: uncomment to see key values
385+
-- vim.notify(string.format("Key type: %s, value: %s", type(key), vim.inspect(key)))
383386

384387
-- Handle different key types
385388
if type(key) == "string" then
@@ -399,8 +402,28 @@ function M.menu()
399402
result = nil
400403
done = true
401404
end
405+
-- Handle single character strings (for regular keys converted from numbers)
406+
if #bytes == 1 then
407+
local char = string.char(bytes[1])
408+
if char == 'j' or char == 'J' then
409+
current_index = math.min(current_index + 1, #menu_options)
410+
update_display()
411+
elseif char == 'k' or char == 'K' then
412+
current_index = math.max(current_index - 1, 1)
413+
update_display()
414+
elseif char >= '1' and char <= '8' then
415+
local selected = string.byte(char) - string.byte('0')
416+
if selected <= #menu_options then
417+
result = menu_options[selected].action
418+
done = true
419+
end
420+
elseif char == 'q' or char == 'Q' then
421+
result = nil
422+
done = true
423+
end
424+
end
402425
elseif type(key) == "number" then
403-
-- Handle regular keys
426+
-- Handle regular keys and special keycodes
404427
if key == string.byte('j') or key == string.byte('J') then
405428
current_index = math.min(current_index + 1, #menu_options)
406429
update_display()

tests/pack-manager/ui_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ describe("pack-manager ui module", function()
275275
local result = ui.menu()
276276
assert.is_nil(result)
277277
end)
278+
279+
it("should handle arrow key navigation in menu", function()
280+
_G.simulate_keys({"\027[B", "\027[B", 13}) -- Down, down, Enter (should select option 3)
281+
local result = ui.menu()
282+
assert.are.equal("update", result)
283+
end)
278284
end)
279285

280286
describe("key type handling tests", function()

0 commit comments

Comments
 (0)