console.lua: implement history search

This commit is contained in:
Guido Cella 2024-08-14 18:18:48 +02:00 committed by Kacper Michajłow
parent 3cc3c9ced3
commit ebc14a58ae
2 changed files with 40 additions and 0 deletions

View File

@ -76,6 +76,9 @@ PGUP
PGDN
Stop navigating the command history.
Ctrl+r
Search the command history.
INSERT
Toggle insert mode.

View File

@ -89,6 +89,7 @@ local id = default_id
local histories = {[id] = {}}
local history = histories[id]
local history_pos = 1
local searching_history = false
local log_buffers = {[id] = {}}
local key_bindings = {}
local global_margins = { t = 0, b = 0 }
@ -769,6 +770,16 @@ end
-- Run the current command and clear the line (Enter)
local function handle_enter()
if searching_history then
searching_history = false
selectable_items = nil
line = #matches > 0 and matches[selected_match].text or ''
cursor = #line + 1
log_buffers[id] = {}
update()
return
end
if line == '' and input_caller == nil then
return
end
@ -878,6 +889,25 @@ local function handle_pgdown()
go_history(#history + 1)
end
local function search_history()
if selectable_items or #history == 0 then
return
end
searching_history = true
selectable_items = {}
matches = {}
selected_match = 1
first_match_to_print = 1
for i = 1, #history do
selectable_items[i] = history[#history + 1 - i]
matches[i] = { index = i, text = history[#history + 1 - i] }
end
update()
end
local function page_up_or_prev_char()
if selectable_items then
handle_pgup()
@ -1532,6 +1562,7 @@ local function get_bindings()
{ 'end', go_end },
{ 'pgup', handle_pgup },
{ 'pgdwn', handle_pgdown },
{ 'ctrl+r', search_history },
{ 'ctrl+c', clear },
{ 'ctrl+d', maybe_exit },
{ 'ctrl+k', del_to_eol },
@ -1591,6 +1622,12 @@ set_active = function (active)
history_pos = #history + 1
mp.enable_messages('terminal-default')
end
elseif searching_history then
searching_history = false
line = ''
cursor = 1
selectable_items = nil
log_buffers[id] = {}
else
repl_active = false
suggestion_buffer = {}