big refactoring

This commit is contained in:
Manuel Simon Hirsig
2017-03-26 17:22:59 +02:00
parent 711308074a
commit 2dc7352581
6 changed files with 34 additions and 59 deletions
+2 -2
View File
@@ -415,7 +415,7 @@ function M.filter_string(components)
local parameter = component[3]
if parameter then
if filter_util.filters[component[2]].input_type == 'money' then
parameter = money.to_string(money.from_string(parameter), nil, true, nil, nil, true)
parameter = money.to_string(money.from_string(parameter), nil, true, nil, true)
end
query_builder.append(parameter)
end
@@ -450,7 +450,7 @@ function prettified_filter_string(filter)
prettified.append(info.display_name(cache.item_id(parameter)) or color.label.enabled('[' .. parameter .. ']'))
else
if filters[component[2]].input_type == 'money' then
prettified.append(money.to_string(money.from_string(parameter), nil, true, nil, color.label.enabled))
prettified.append(money.to_string(money.from_string(parameter), nil, true, color.label.enabled))
else
prettified.append(color.label.enabled(parameter))
end
+13 -39
View File
@@ -33,9 +33,8 @@ function M.to_string2(money, exact, color)
local PART = color .. '.|cff%s%02d' .. FONT_COLOR_CODE_CLOSE
local NONE = '|cffa0a0a0' .. TEXT_NONE .. FONT_COLOR_CODE_CLOSE
if not exact and money >= 10000 then
-- Round to nearest silver
money = floor(money / 100 + .5) * 100
if not exact and money >= COPPER_PER_GOLD then
money = floor(money / COPPER_PER_SILVER + .5) * COPPER_PER_SILVER
end
local g, s, c = to_gsc(money)
@@ -59,57 +58,45 @@ function M.to_string2(money, exact, color)
return str
end
--function M.to_string(params) -- TODO
-- local gold, silver, copper = to_gsc(params.copper or 0 + )
-- local settings = set(unpack(arg))
--end
function M.to_string(money, pad, trim, decimal_points, color, no_color)
function M.to_string(money, pad, trim, number_color, no_coin_color)
local is_negative = money < 0
money = abs(money)
local gold, silver, copper = to_gsc(money)
-- rounding
if decimal_points then
copper = tonumber(format('%.' .. decimal_points .. 'f', copper))
end
local gold_text, silver_text, copper_text
if no_color then
if no_coin_color then
gold_text, silver_text, copper_text = 'g', 's', 'c'
else
gold_text, silver_text, copper_text = GOLD_TEXT, SILVER_TEXT, COPPER_TEXT
end
number_color = number_color or color.none
local text
if trim then
local parts = temp-T
if gold > 0 then
tinsert(parts, format_number(gold, false, nil, color) .. gold_text)
tinsert(parts, number_color(gold) .. gold_text)
end
if silver > 0 then
tinsert(parts, format_number(silver, pad, nil, color) .. silver_text)
tinsert(parts, number_color(silver) .. silver_text)
end
if copper > 0 or gold == 0 and silver == 0 then
tinsert(parts, format_number(copper, pad, decimal_points, color) .. copper_text)
tinsert(parts, number_color(copper) .. copper_text)
end
text = join(parts, ' ')
else
if gold > 0 then
text = format_number(gold, false, nil, color) .. gold_text .. ' ' .. format_number(silver, pad, nil, color) .. silver_text .. ' ' .. format_number(copper, pad, decimal_points, color) .. copper_text
text = number_color(gold) .. gold_text .. ' ' .. number_color(silver) .. silver_text .. ' ' .. number_color(copper) .. copper_text
elseif silver > 0 then
text = format_number(silver, false, nil, color) .. silver_text .. ' ' .. format_number(copper, pad, decimal_points, color) .. copper_text
text = number_color(silver) .. silver_text .. ' ' .. number_color(copper) .. copper_text
else
text = format_number(copper, false, decimal_points, color) .. copper_text
text = number_color(copper) .. copper_text
end
end
if is_negative then
if color then
text = color .. '-' .. FONT_COLOR_CODE_CLOSE .. text
else
text = '-' .. text
end
text = number_color'-' .. text
end
return text
@@ -121,30 +108,17 @@ function M.from_string(value)
return number * COPPER_PER_GOLD
end
-- remove any colors
value = gsub(gsub(strlower(value), '|c%x%x%x%x%x%x%x%x', ''), FONT_COLOR_CODE_CLOSE, '')
-- extract gold/silver/copper values
local gold = tonumber(select(3, strfind(value, '(%d*%.?%d+)g')))
local silver = tonumber(select(3, strfind(value, '(%d*%.?%d+)s')))
local copper = tonumber(select(3, strfind(value, '(%d*%.?%d+)c')))
if not gold and not silver and not copper then return end
-- test that there are no extra characters (other than spaces)
value = gsub(value, '%d*%.?%d+g', '', 1)
value = gsub(value, '%d*%.?%d+s', '', 1)
value = gsub(value, '%d*%.?%d+c', '', 1)
if strfind(value, '%S') then return end
return from_gsc(gold or 0, silver or 0, copper or 0)
end
function M.format_number(num, pad, decimal_padding, color)
local padding = pad and 2 + (decimal_padding and decimal_padding + 1 or 0) or 0
num = format('%0' .. padding .. '.0' .. (decimal_padding or 0) .. 'f', num)
if color then
return color .. num .. FONT_COLOR_CODE_CLOSE
else
return num
end
end