Skip to content

Commit

Permalink
console.lua: pre-filter searched commands if the line is filled
Browse files Browse the repository at this point in the history
If you type something before pressing Ctrl+r, filter the commands
containing the typed line.

fuzzy_find() is modified to not code a separate case when the line is
empty in both handle_edit() or search_history().
  • Loading branch information
guidocella authored and kasper93 committed Aug 24, 2024
1 parent 92f052c commit 64a0e31
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,11 @@ end

local function fuzzy_find(needle, haystacks)
local result = require 'mp.fzy'.filter(needle, haystacks)
table.sort(result, function (i, j)
return i[3] > j[3]
end)
if line ~= '' then -- Prevent table.sort() from reordering the items.
table.sort(result, function (i, j)
return i[3] > j[3]
end)
end
for i, value in ipairs(result) do
result[i] = value[1]
end
Expand Down Expand Up @@ -639,14 +641,8 @@ local function handle_edit()
matches = {}
selected_match = 1

if line == '' then
for i, item in ipairs(selectable_items) do
matches[i] = { index = i, text = item }
end
else
for i, match in ipairs(fuzzy_find(line, selectable_items)) do
matches[i] = { index = match, text = selectable_items[match] }
end
for i, match in ipairs(fuzzy_find(line, selectable_items)) do
matches[i] = { index = match, text = selectable_items[match] }
end
end

Expand Down Expand Up @@ -902,7 +898,10 @@ local function search_history()

for i = 1, #history do
selectable_items[i] = history[#history + 1 - i]
matches[i] = { index = i, text = history[#history + 1 - i] }
end

for i, match in ipairs(fuzzy_find(line, selectable_items)) do
matches[i] = { index = match, text = selectable_items[match] }
end

update()
Expand Down

0 comments on commit 64a0e31

Please sign in to comment.