LibItemSearch

This commit is contained in:
Bunny67
2018-01-12 01:22:57 +03:00
parent 5dd6eccd72
commit 2d223bef31
7 changed files with 48 additions and 37 deletions
@@ -23,6 +23,7 @@ if not Lib then
return return
end end
local lower = string.lower
--[[ Parsing ]]-- --[[ Parsing ]]--
@@ -36,7 +37,7 @@ function Lib:Matches(object, search, filters)
end end
function Lib:MatchAll(search) function Lib:MatchAll(search)
for phrase in self:Clean(search):gmatch('[^&]+') do for phrase in gmatch(self:Clean(search), '[^&]+') do
if not self:MatchAny(phrase) then if not self:MatchAny(phrase) then
return return
end end
@@ -46,7 +47,7 @@ function Lib:MatchAll(search)
end end
function Lib:MatchAny(search) function Lib:MatchAny(search)
for phrase in search:gmatch('[^|]+') do for phrase in gmatch(search, '[^|]+') do
if self:Match(phrase) then if self:Match(phrase) then
return true return true
end end
@@ -54,13 +55,13 @@ function Lib:MatchAny(search)
end end
function Lib:Match(search) function Lib:Match(search)
local tag, rest = search:match('^%s*(%S+):(.*)$') local tag, rest = match(search, '^%s*(%S+):(.*)$')
if tag then if tag then
tag = '^' .. tag tag = '^' .. tag
search = rest search = rest
end end
local words = search:gmatch('%S+') local words = gmatch(search, '%S+')
local failed local failed
for word in words do for word in words do
@@ -72,7 +73,7 @@ function Lib:Match(search)
end end
else else
local negate, rest = word:match('^([!~]=*)(.*)$') local negate, rest = match(word, '^([!~]=*)(.*)$')
if negate or word == self.NOT_MATCH then if negate or word == self.NOT_MATCH then
word = rest and rest ~= '' and rest or words() or '' word = rest and rest ~= '' and rest or words() or ''
negate = -1 negate = -1
@@ -80,7 +81,7 @@ function Lib:Match(search)
negate = 1 negate = 1
end end
local operator, rest = word:match('^(=*[<>]=*)(.*)$') local operator, rest = match(word, '^(=*[<>]=*)(.*)$')
if operator then if operator then
word = rest ~= '' and rest or words() word = rest ~= '' and rest or words()
end end
@@ -106,7 +107,7 @@ function Lib:Filter(tag, operator, search)
if tag then if tag then
for _, filter in pairs(self.filters) do for _, filter in pairs(self.filters) do
for _, value in pairs(filter.tags or {}) do for _, value in pairs(filter.tags or {}) do
if value:find(tag) then if find(value, tag) then
return self:UseFilter(filter, operator, search) return self:UseFilter(filter, operator, search)
end end
end end
@@ -123,7 +124,7 @@ end
function Lib:UseFilter(filter, operator, search) function Lib:UseFilter(filter, operator, search)
local data = {filter:canSearch(operator, search, self.object)} local data = {filter:canSearch(operator, search, self.object)}
if data[1] then if data[1] then
return filter:match(self.object, operator, unpack(data)) return match(filter, self.object, operator, unpack(data))
end end
end end
@@ -131,20 +132,20 @@ end
--[[ Utilities ]]-- --[[ Utilities ]]--
function Lib:Find(search, ...) function Lib:Find(search, ...)
for i = 1, select('#', ...) do for i = 1, getn(arg) do
local text = select(i, ...) local text = arg[i]
if text and self:Clean(text):find(search) then if text and find(self:Clean(text), search) then
return true return true
end end
end end
end end
function Lib:Clean(string) function Lib:Clean(string)
string = string:lower() string = lower(string)
string = string:gsub('[%(%)%.%%%+%-%*%?%[%]%^%$]', function(c) return '%'..c end) string = gsub(string, '[%(%)%.%%%+%-%*%?%[%]%^%$]', function(c) return '%'..c end)
for accent, char in pairs(self.ACCENTS) do for accent, char in pairs(self.ACCENTS) do
string = string:gsub(accent, char) string = gsub(string, accent, char)
end end
return string return string
@@ -152,16 +153,16 @@ end
function Lib:Compare(op, a, b) function Lib:Compare(op, a, b)
if op then if op then
if op:find('<') then if find(op, '<') then
if op:find('=') then if find(op, '=') then
return a <= b return a <= b
end end
return a < b return a < b
end end
if op:find('>')then if find(op, '>')then
if op:find('=') then if find(op, '=') then
return a >= b return a >= b
end end
@@ -0,0 +1,4 @@
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
..\FrameXML\UI.xsd">
<Script file="CustomSearch-1.0.lua"/>
</Ui>
@@ -12,6 +12,8 @@ else
return; return;
end end
local strmatch, lower = string.match, string.lower
--[[ User API ]]-- --[[ User API ]]--
function Lib:Matches(link, search) function Lib:Matches(link, search)
@@ -19,17 +21,17 @@ function Lib:Matches(link, search)
end end
function Lib:Tooltip(link, search) function Lib:Tooltip(link, search)
return link and self.Filters.tip:match(link, nil, search); return link and strmatch(self.Filters.tip, link, nil, search);
end end
function Lib:TooltipPhrase(link, search) function Lib:TooltipPhrase(link, search)
return link and self.Filters.tipPhrases:match(link, nil, search); return link and strmatch(self.Filters.tipPhrases, link, nil, search);
end end
function Lib:InSet(link, search) function Lib:InSet(link, search)
if(IsEquippableItem(link)) then if(IsEquippableItem(link)) then
local id = tonumber(link:match("item:(%-?%d+)")); local id = tonumber(strmatch(link, "item:(%-?%d+)"));
return self:BelongsToSet(id, (search or ""):lower()); return self:BelongsToSet(id, lower(search or ""));
end end
end end
@@ -43,7 +45,7 @@ Lib.Filters.name = {
end, end,
match = function(self, item, _, search) match = function(self, item, _, search)
local name = item:match("%[(.-)%]"); local name = strmatch(item, "%[(.-)%]");
return Search:Find(search, name); return Search:Find(search, name);
end end
}; };
@@ -94,8 +96,8 @@ Lib.Filters.requiredlevel = {
--[[ Quality ]]-- --[[ Quality ]]--
local qualities = {}; local qualities = {};
for i = 0, #ITEM_QUALITY_COLORS do for i = 0, getn(ITEM_QUALITY_COLORS) do
qualities[i] = _G["ITEM_QUALITY" .. i .. "_DESC"]:lower(); qualities[i] = lower(_G["ITEM_QUALITY" .. i .. "_DESC"]);
end end
Lib.Filters.quality = { Lib.Filters.quality = {
@@ -103,7 +105,7 @@ Lib.Filters.quality = {
canSearch = function(self, _, search) canSearch = function(self, _, search)
for i, name in pairs(qualities) do for i, name in pairs(qualities) do
if(name:find(search)) then if(find(namesearch)) then
return i; return i;
end end
end end
@@ -146,7 +148,7 @@ Lib.Filters.tip = {
end, end,
match = function(self, link, _, search) match = function(self, link, _, search)
if(link:find("item:")) then if(find(link, "item:")) then
scanner:SetOwner(UIParent, "ANCHOR_NONE"); scanner:SetOwner(UIParent, "ANCHOR_NONE");
scanner:SetHyperlink(link) scanner:SetHyperlink(link)
@@ -166,7 +168,7 @@ local escapes = {
local function CleanString(str) local function CleanString(str)
for k, v in pairs(escapes) do for k, v in pairs(escapes) do
str = str:gsub(k, v); str = string.gsub(str, k, v);
end end
return str; return str;
end end
@@ -177,7 +179,7 @@ Lib.Filters.tipPhrases = {
end, end,
match = function(self, link, _, search) match = function(self, link, _, search)
local id = link:match("item:(%d+)"); local id = strmatch(link, "item:(%d+)");
if(not id) then if(not id) then
return; return;
end end
@@ -207,13 +209,12 @@ Lib.Filters.tipPhrases = {
cache = setmetatable({}, {__index = function(t, k) local v = {} t[k] = v return v end}), cache = setmetatable({}, {__index = function(t, k) local v = {} t[k] = v return v end}),
keywords = { keywords = {
[ITEM_SOULBOUND:lower()] = ITEM_BIND_ON_PICKUP, [lower(ITEM_SOULBOUND)] = ITEM_BIND_ON_PICKUP,
["bound"] = ITEM_BIND_ON_PICKUP, ["bound"] = ITEM_BIND_ON_PICKUP,
["bop"] = ITEM_BIND_ON_PICKUP, ["bop"] = ITEM_BIND_ON_PICKUP,
["boe"] = ITEM_BIND_ON_EQUIP, ["boe"] = ITEM_BIND_ON_EQUIP,
["bou"] = ITEM_BIND_ON_USE, ["bou"] = ITEM_BIND_ON_USE,
["boa"] = ITEM_BIND_TO_ACCOUNT, ["boa"] = ITEM_BIND_TO_ACCOUNT,
[QUESTS_LABEL:lower()] = ITEM_BIND_QUEST
}; };
}; };
@@ -225,7 +226,7 @@ if IsAddOnLoaded("ItemRack") then
function Lib:BelongsToSet(id, search) function Lib:BelongsToSet(id, search)
for name, set in pairs(ItemRackUser.Sets) do for name, set in pairs(ItemRackUser.Sets) do
if name:sub(1,1) ~= "" and Search:Find(search, name) then if string.sub(name,1,1) ~= "" and Search:Find(search, name) then
for _, item in pairs(set.equip) do for _, item in pairs(set.equip) do
if sameID(id, item) then if sameID(id, item) then
return true return true
@@ -1,6 +1,6 @@
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/ <Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
..\FrameXML\UI.xsd"> ..\FrameXML\UI.xsd">
<Script file="CustomSearch-1.0\CustomSearch-1.0.lua"/> <Include file="CustomSearch-1.0\CustomSearch-1.0.xml"/>
<Script file="Unfit-1.0\Unfit-1.0.lua"/> <Include file="Unfit-1.0\Unfit-1.0.xml"/>
<Script file="LibItemSearch-1.2.lua"/> <Script file="LibItemSearch-1.2.lua"/>
</Ui> </Ui>
@@ -97,9 +97,9 @@ end
--[[ API ]]-- --[[ API ]]--
function Lib:IsItemUnusable(...) function Lib:IsItemUnusable(arg1)
if ... then if arg1 then
local subclass, _, slot = select(7, GetItemInfo(...)) local subclass, _, slot = select(7, GetItemInfo(arg1))
return Lib:IsClassUnusable(subclass, slot) return Lib:IsClassUnusable(subclass, slot)
end end
end end
@@ -0,0 +1,4 @@
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
..\FrameXML\UI.xsd">
<Script file="Unfit-1.0.lua"/>
</Ui>
@@ -21,4 +21,5 @@
<Include file="LibBase64-1.0\LibBase64-1.0.xml"/> <Include file="LibBase64-1.0\LibBase64-1.0.xml"/>
<Include file="LibAnim\LibAnim.xml"/> <Include file="LibAnim\LibAnim.xml"/>
<Include file="LibUIDropDownMenu\LibUIDropDownMenu.xml"/> <Include file="LibUIDropDownMenu\LibUIDropDownMenu.xml"/>
<Include file="LibItemSearch-1.2\LibItemSearch-1.2.xml"/>
</Ui> </Ui>