Improve search filter accuracy

This commit is contained in:
Jean-Patrick Guerrero 2019-10-25 01:42:04 +02:00
parent 35b2ecfdc1
commit 8b4ef8a4b7

@ -1083,6 +1083,16 @@ craftguide.register_craft_type("digging_chance", {
icon = "default_tool_mesepick.png", icon = "default_tool_mesepick.png",
}) })
local function sfind(str, filter)
if filter == "" then
return 0
end
if find(str, filter, 1, true) then
return #str - #filter
end
end
local function search(data) local function search(data)
local filter = data.filter local filter = data.filter
@ -1091,7 +1101,7 @@ local function search(data)
return return
end end
local opt = "^(.-)%+([%w_]*)=*([%w_,]*)" local opt = "^(.-)%+([%w_]+)=([%w_,]+)"
local search_filter = next(search_filters) and match(filter, opt) local search_filter = next(search_filters) and match(filter, opt)
local filters = {} local filters = {}
@ -1104,30 +1114,28 @@ local function search(data)
end end
end end
local filtered_list, c = {}, 0 local filtered_list, order, c = {}, {}, 0
for i = 1, #data.items_raw do for i = 1, #data.items_raw do
local item = data.items_raw[i] local item = data.items_raw[i]
local def = reg_items[item] local def = reg_items[item]
local desc = (def and def.description) and lower(def.description) or "" local desc = (def and def.description) and lower(def.description) or ""
local search_in = fmt("%s %s", item, desc)
local to_add local to_add
if search_filter then if search_filter then
for filter_name, values in pairs(filters) do for filter_name, values in pairs(filters) do
if values then
local func = search_filters[filter_name] local func = search_filters[filter_name]
to_add = func(item, values) and (search_filter == "" or to_add = func(item, values) and (search_filter == "" or
find(search_in, search_filter, 1, true)) (sfind(item, search_filter) or sfind(desc, search_filter)))
end
end end
else else
to_add = find(search_in, filter, 1, true) to_add = sfind(item, filter) or sfind(desc, filter)
end end
if to_add then if to_add then
c = c + 1 c = c + 1
filtered_list[c] = item filtered_list[c] = item
order[item] = to_add
end end
end end
@ -1140,6 +1148,10 @@ local function search(data)
end end
end end
sort(filtered_list, function(a, b)
return order[a] < order[b]
end)
data.items = filtered_list data.items = filtered_list
end end