diff --git a/milo/MiloRemote.lua b/milo/MiloRemote.lua index 0251bd0..ef1dc40 100644 --- a/milo/MiloRemote.lua +++ b/milo/MiloRemote.lua @@ -1,5 +1,6 @@ local Config = require('config') local Event = require('event') +local fuzzy = require('fuzzyMatch') local Sound = require('sound') local Socket = require('socket') local sync = require('sync').sync @@ -29,7 +30,6 @@ local displayModes = { local page = UI.Page { menuBar = UI.MenuBar { - y = 1, height = 1, buttons = { { text = 'Refresh', @@ -300,21 +300,37 @@ end function page:applyFilter() local function filterItems(t, filter, displayMode) - if filter or displayMode > 0 then + self.grid.sortColumn = context.state.sortColumn or 'count' + self.grid.inverseSort = context.state.inverseSort + + if filter then local r = { } - if filter then - filter = filter:lower() - end + filter = filter:lower() + self.grid.sortColumn = 'score' + self.grid.inverseSort = true + for _,v in pairs(t) do - if not filter or string.find(v.lname, filter, 1, true) then - if filter or --displayMode == 0 or - displayMode == 1 and v.count > 0 then - table.insert(r, v) + v.score = fuzzy(v.lname, filter) + if v.score > 0 then + if v.count > 0 then + v.score = v.score + 1 end + table.insert(r, v) + end + end + return r + + elseif displayMode > 0 then + local r = { } + + for _,v in pairs(t) do + if v.count > 0 then + table.insert(r, v) end end return r end + return t end local t = filterItems(self.items, self.filter, context.state.displayMode) diff --git a/milo/apis/fuzzyMatch.lua b/milo/apis/fuzzyMatch.lua new file mode 100644 index 0000000..bca2dad --- /dev/null +++ b/milo/apis/fuzzyMatch.lua @@ -0,0 +1,22 @@ +-- Based on Squid's fuzzy search +-- https://github.com/SquidDev-CC/artist/blob/vnext/artist/lib/match.lua +-- +-- not very fuzzy anymore + +local SCORE_WEIGHT = 1000 +local LEADING_LETTER_PENALTY = -3 +local LEADING_LETTER_PENALTY_MAX = -9 + +local _find = string.find + +return function(str_lower, ptrn_lower) + local score = 0 + + local start = _find(str_lower, ptrn_lower, 1, true) + if start then + -- All letters before the current one are considered leading, so add them to our penalty + score = SCORE_WEIGHT + math.max(LEADING_LETTER_PENALTY * (start - 1), LEADING_LETTER_PENALTY_MAX) + end + + return score +end