Skip to content

Commit

Permalink
uosc: Search by first character of each word
Browse files Browse the repository at this point in the history
  • Loading branch information
dexeonify committed Sep 24, 2023
1 parent a32d3ea commit 3e1f2f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions scripts/uosc/elements/Menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,11 @@ end
function Menu:search_internal(menu)
local query = menu.search.query:lower()
menu.items = query ~= '' and itable_filter(menu.search.source.items, function(item)
return item.title and item.title:lower():find(query, 1, true) or
item.hint and item.hint:lower():find(query, 1, true)
local title = item.title and item.title:lower()
local hint = item.hint and item.hint:lower()
return title and title:find(query, 1, true) or hint and hint:find(query, 1, true) or
title and table.concat(first_word_chars(title)):find(query, 1, true) or
hint and table.concat(first_word_chars(hint)):find(query, 1, true)
end) or menu.search.source.items
self:search_update_items()
end
Expand Down
22 changes: 22 additions & 0 deletions scripts/uosc/lib/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,25 @@ do
return table.concat(lines, '\n'), #lines
end
end

do
local word_separators = {
' ', ' ', '\t', '-', '', '_', ',', '.', '+', '&', '(', ')', '[', ']', '{', '}', '<', '>', '/', '\\',
}

---Get the first character of each words
---@param str string
---@return string[]
function first_word_chars(str)
local first_chars, is_word_start, word_separators = {}, true, word_separators
for _, char in utf8_iter(str) do
if itable_has(word_separators, char) then
is_word_start = true
elseif is_word_start then
first_chars[#first_chars + 1] = char
is_word_start = false
end
end
return first_chars
end
end

0 comments on commit 3e1f2f1

Please sign in to comment.