summaryrefslogtreecommitdiff
path: root/state.lua
diff options
context:
space:
mode:
Diffstat (limited to 'state.lua')
-rw-r--r--state.lua52
1 files changed, 23 insertions, 29 deletions
diff --git a/state.lua b/state.lua
index f7f2234..4db4837 100644
--- a/state.lua
+++ b/state.lua
@@ -213,6 +213,24 @@ function menu_mt:set_search_text(str, pos, term, filter)
self:update_search_matches()
end
+function menu_mt:_match_fields(v, fields, case_sensitive)
+ local nm = fields.name and
+ util.find_matches(
+ v.name, self.search_term, case_sensitive)
+ local im = fields.info and
+ util.find_matches(
+ v.info, self.search_term, case_sensitive)
+
+ if nm or im then
+ -- search options may contain dynamic data that is updated on
+ -- redraw. using a proxy table instead of copying prevents
+ -- potential updates on every change of search text.
+ return setmetatable(
+ {matches = nm, info_matches = im}, {__index = v})
+ end
+end
+
+local F_NAME = {name = true}
function menu_mt:update_search_matches()
if not self.search_filter and #self.search_term == 0 then
self.options = self.search_options
@@ -224,35 +242,11 @@ function menu_mt:update_search_matches()
local options = {}
for _, v in ipairs(self.search_options) do
- if not self.search_filter or self.search_filter(v) then
- local matches
-
- local name = v.name
- if not case_sensitive then
- name = name:lower()
- end
-
- local i, j = 0, 0
- while true do
- i, j = name:find(self.search_term, j + 1, true)
- -- j < i avoids infinite loop on empty term
- if not i or j < i then
- break
- end
- matches = matches or {}
- matches[#matches+1] = {start = i, stop = j}
- end
-
- if matches then
- -- search options may contain dynamic data that
- -- is updated on redraw. using a proxy table
- -- instead of copying prevents potential
- -- updates on every change of search text.
- options[#options+1] = setmetatable(
- {matches = matches}, {__index = v})
- elseif i then
- options[#options+1] = v
- end
+ local fields = not self.search_filter and F_NAME or
+ self.search_filter(v)
+ if fields then
+ options[#options+1] = self.search_term == '' and v or
+ self:_match_fields(v, fields, case_sensitive)
end
end