vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Name: Abacus-2.0
3 Revision: $Rev: 14735 $
4 Author(s): ckknight (ckknight@gmail.com)
5 Website: http://ckknight.wowinterface.com/
6 Documentation: http://wiki.wowace.com/index.php/Abacus-2.0
7 SVN: http://svn.wowace.com/root/trunk/AbacusLib/Abacus-2.0
8 Description: A library to provide tools for formatting money and time.
9 Dependencies: AceLibrary
10 ]]
11  
12 local MAJOR_VERSION = "Abacus-2.0"
13 local MINOR_VERSION = "$Revision: 14735 $"
14  
15 if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary") end
16 if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end
17  
18 local Abacus = {}
19  
20 local table_setn
21 do
22 local version = GetBuildInfo()
23 if string.find(version, "^2%.") then
24 -- 2.0.0
25 table_setn = function() end
26 else
27 table_setn = table.setn
28 end
29 end
30  
31 local COPPER_ABBR = string.lower(string.sub(COPPER, 1, 1))
32 local SILVER_ABBR = string.lower(string.sub(SILVER, 1, 1))
33 local GOLD_ABBR = string.lower(string.sub(GOLD, 1, 1))
34 if (string.byte(COPPER_ABBR) or 128) > 127 then
35 -- non-western
36 COPPER_ABBR = COPPER
37 SILVER_ABBR = SILVER
38 GOLD_ABBR = GOLD
39 end
40  
41 local COLOR_WHITE = "ffffff"
42 local COLOR_GREEN = "00ff00"
43 local COLOR_RED = "ff0000"
44 local COLOR_COPPER = "eda55f"
45 local COLOR_SILVER = "c7c7cf"
46 local COLOR_GOLD = "ffd700"
47  
48 local L_DAY_ONELETTER_ABBR = string.sub(DAY_ONELETTER_ABBR, 4)
49 local L_HOUR_ONELETTER_ABBR = string.sub(HOUR_ONELETTER_ABBR, 4)
50 local L_MINUTE_ONELETTER_ABBR = string.sub(MINUTE_ONELETTER_ABBR, 4)
51 local L_SECOND_ONELETTER_ABBR = string.sub(SECOND_ONELETTER_ABBR, 4)
52  
53 local L_UNDETERMINED = "Undetermined"
54  
55 if ( GetLocale() =="koKR" ) then
56 L_DAY_ONELETTER_ABBR = string.sub(DAY_ONELETTER_ABBR, 3)
57 L_HOUR_ONELETTER_ABBR = string.sub(HOUR_ONELETTER_ABBR, 3)
58 L_MINUTE_ONELETTER_ABBR = string.sub(MINUTE_ONELETTER_ABBR, 3)
59 L_SECOND_ONELETTER_ABBR = string.sub(SECOND_ONELETTER_ABBR, 3)
60  
61 L_UNDETERMINED = "측정불가"
62 end
63  
64 local inf = 1/0
65  
66 function Abacus:FormatMoneyExtended(value, colorize, textColor)
67 self:argCheck(value, 2, "number")
68 local gold = abs(value / 10000)
69 local silver = abs(mod(value / 100, 100))
70 local copper = abs(mod(value, 100))
71  
72 local negl = ""
73 local color = COLOR_WHITE
74 if value > 0 then
75 if textColor then
76 color = COLOR_GREEN
77 end
78 elseif value < 0 then
79 negl = "-"
80 if textColor then
81 color = COLOR_RED
82 end
83 end
84 if colorize then
85 if value == inf or value == -inf then
86 return format("|cff%s%s|r", color, value)
87 elseif value ~= value then
88 return format("|cff%s0|r|cff%s %s|r", COLOR_WHITE, COLOR_COPPER, COPPER)
89 elseif value >= 10000 or value <= -10000 then
90 return format("|cff%s%s%d|r|cff%s %s|r |cff%s%d|r|cff%s %s|r |cff%s%d|r|cff%s %s|r", color, negl, gold, COLOR_GOLD, GOLD, color, silver, COLOR_SILVER, SILVER, color, copper, COLOR_COPPER, COPPER)
91 elseif value >= 100 or value <= -100 then
92 return format("|cff%s%s%d|r|cff%s %s|r |cff%s%d|r|cff%s %s|r", color, negl, silver, COLOR_SILVER, SILVER, color, copper, COLOR_COPPER, COPPER)
93 else
94 return format("|cff%s%s%d|r|cff%s %s|r", color, negl, copper, COLOR_COPPER, COPPER)
95 end
96 else
97 if value == inf or value == -inf then
98 return format("%s", value)
99 elseif value ~= value then
100 return format("0 %s", COPPER)
101 elseif value >= 10000 or value <= -10000 then
102 return format("%s%d %s %d %s %d %s", negl, gold, GOLD, silver, SILVER, copper, COPPER)
103 elseif value >= 100 or value <= -100 then
104 return format("%s%d %s %d %s", negl, silver, SILVER, copper, COPPER)
105 else
106 return format("%s%d %s", negl, copper, COPPER)
107 end
108 end
109 end
110  
111 function Abacus:FormatMoneyFull(value, colorize, textColor)
112 self:argCheck(value, 2, "number")
113 local gold = abs(value / 10000)
114 local silver = abs(mod(value / 100, 100))
115 local copper = abs(mod(value, 100))
116  
117 local negl = ""
118 local color = COLOR_WHITE
119 if value > 0 then
120 if textColor then
121 color = COLOR_GREEN
122 end
123 elseif value < 0 then
124 negl = "-"
125 if textColor then
126 color = COLOR_RED
127 end
128 end
129 if colorize then
130 if value == inf or value == -inf then
131 return format("|cff%s%s|r", color, value)
132 elseif value ~= value then
133 return format("|cff%s0|r|cff%s%s|r", COLOR_WHITE, COLOR_COPPER, COPPER_ABBR)
134 elseif value >= 10000 or value <= -10000 then
135 return format("|cff%s%s%d|r|cff%s%s|r |cff%s%d|r|cff%s%s|r |cff%s%d|r|cff%s%s|r", color, negl, gold, COLOR_GOLD, GOLD_ABBR, color, silver, COLOR_SILVER, SILVER_ABBR, color, copper, COLOR_COPPER, COPPER_ABBR)
136 elseif value >= 100 or value <= -100 then
137 return format("|cff%s%s%d|r|cff%s%s|r |cff%s%d|r|cff%s%s|r", color, negl, silver, COLOR_SILVER, SILVER_ABBR, color, copper, COLOR_COPPER, COPPER_ABBR)
138 else
139 return format("|cff%s%s%d|r|cff%s%s|r", color, negl, copper, COLOR_COPPER, COPPER_ABBR)
140 end
141 else
142 if value == inf or value == -inf then
143 return format("%s", value)
144 elseif value ~= value then
145 return format("0%s", COPPER_ABBR)
146 elseif value >= 10000 or value <= -10000 then
147 return format("%s%d%s %d%s %d%s", negl, gold, GOLD_ABBR, silver, SILVER_ABBR, copper, COPPER_ABBR)
148 elseif value >= 100 or value <= -100 then
149 return format("%s%d%s %d%s", negl, silver, SILVER_ABBR, copper, COPPER_ABBR)
150 else
151 return format("%s%d%s", negl, copper, COPPER_ABBR)
152 end
153 end
154 end
155  
156 function Abacus:FormatMoneyShort(copper, colorize, textColor)
157 self:argCheck(copper, 2, "number")
158 local color = COLOR_WHITE
159 if textColor then
160 if copper > 0 then
161 color = COLOR_GREEN
162 elseif copper < 0 then
163 color = COLOR_RED
164 end
165 end
166 if colorize then
167 if copper == inf or copper == -inf then
168 return format("|cff%s%s|r", color, copper)
169 elseif copper ~= copper then
170 return format("|cff%s0|r|cff%s%s|r", COLOR_WHITE, COLOR_COPPER, COPPER_ABBR)
171 elseif copper >= 10000 or copper <= -10000 then
172 return format("|cff%s%.1f|r|cff%s%s|r", color, copper / 10000, COLOR_GOLD, GOLD_ABBR)
173 elseif copper >= 100 or copper <= -100 then
174 return format("|cff%s%.1f|r|cff%s%s|r", color, copper / 100, COLOR_SILVER, SILVER_ABBR)
175 else
176 return format("|cff%s%d|r|cff%s%s|r", color, copper, COLOR_COPPER, COPPER_ABBR)
177 end
178 else
179 if value == copper or value == -copper then
180 return format("%s", copper)
181 elseif copper ~= copper then
182 return format("0%s", COPPER_ABBR)
183 elseif copper >= 10000 or copper <= -10000 then
184 return format("%.1f%s", copper / 10000, GOLD_ABBR)
185 elseif copper >= 100 or copper <= -100 then
186 return format("%.1f%s", copper / 100, SILVER_ABBR)
187 else
188 return format("%.0f%s", copper, COPPER_ABBR)
189 end
190 end
191 end
192  
193 function Abacus:FormatMoneyCondensed(value, colorize, textColor)
194 self:argCheck(value, 2, "number")
195 local negl = ""
196 local negr = ""
197 if value < 0 then
198 if colorize and textColor then
199 negl = "|cffff0000-(|r"
200 negr = "|cffff0000)|r"
201 else
202 negl = "-("
203 negr = ")"
204 end
205 end
206 local gold = floor(math.abs(value) / 10000)
207 local silver = mod(floor(math.abs(value) / 100), 100)
208 local copper = mod(floor(math.abs(value)), 100)
209 if colorize then
210 if value == inf or value == -inf then
211 return format("%s|cff%s%s|r%s", negl, COLOR_COPPER, math.abs(value), negr)
212 elseif value ~= value then
213 return format("|cff%s0|r", COLOR_COPPER)
214 elseif gold ~= 0 then
215 return format("%s|cff%s%d|r.|cff%s%02d|r.|cff%s%02d|r%s", negl, COLOR_GOLD, gold, COLOR_SILVER, silver, COLOR_COPPER, copper, negr)
216 elseif silver ~= 0 then
217 return format("%s|cff%s%d|r.|cff%s%02d|r%s", negl, COLOR_SILVER, silver, COLOR_COPPER, copper, negr)
218 else
219 return format("%s|cff%s%d|r%s", negl, COLOR_COPPER, copper, negr)
220 end
221 else
222 if value == inf or value == -inf then
223 return tostring(value)
224 elseif value ~= value then
225 return "0"
226 elseif gold ~= 0 then
227 return format("%s%d.%02d.%02d%s", negl, gold, silver, copper, negr)
228 elseif silver ~= 0 then
229 return format("%s%d.%02d%s", negl, silver, copper, negr)
230 else
231 return format("%s%d%s", negl, copper, negr)
232 end
233 end
234 end
235  
236 local t
237 function Abacus:FormatDurationExtended(duration, colorize, hideSeconds)
238 self:argCheck(duration, 2, "number")
239 local negative = ""
240 if duration ~= duration then
241 duration = 0
242 end
243 if duration < 0 then
244 negative = "-"
245 duration = -duration
246 end
247 local days = floor(duration / 86400)
248 local hours = mod(floor(duration / 3600), 24)
249 local mins = mod(floor(duration / 60), 60)
250 local secs = mod(floor(duration), 60)
251 if not t then
252 t = {}
253 else
254 for k in pairs(t) do
255 t[k] = nil
256 end
257 table_setn(t, 0)
258 end
259 if not colorize then
260 if not duration or duration > 86400*365 then
261 return L_UNDETERMINED
262 end
263 if days > 1 then
264 table.insert(t, format("%d %s", days, DAYS_ABBR_P1))
265 elseif days == 1 then
266 table.insert(t, format("%d %s", days, DAYS_ABBR))
267 end
268 if hours > 1 then
269 table.insert(t, format("%d %s", hours, HOURS_ABBR_P1))
270 elseif hours == 1 then
271 table.insert(t, format("%d %s", hours, HOURS_ABBR))
272 end
273 if mins > 1 then
274 table.insert(t, format("%d %s", mins, MINUTES_ABBR_P1))
275 elseif mins == 1 then
276 table.insert(t, format("%d %s", mins, MINUTES_ABBR))
277 end
278 if not hideSeconds then
279 if secs > 1 then
280 table.insert(t, format("%d %s", secs, SECONDS_ABBR_P1))
281 elseif secs == 1 then
282 table.insert(t, format("%d %s", secs, SECONDS_ABBR))
283 end
284 end
285 if table.getn(t) == 0 then
286 if not hideSeconds then
287 return "0 " .. SECONDS_ABBR_P1
288 else
289 return "0 " .. MINUTES_ABBR_P1
290 end
291 else
292 return negative .. table.concat(t, " ")
293 end
294 else
295 if not duration or duration > 86400*365 then
296 return "|cffffffff"..L_UNDETERMINED.."|r"
297 end
298 if days > 1 then
299 table.insert(t, format("|cffffffff%d|r %s", days, DAYS_ABBR_P1))
300 elseif days == 1 then
301 table.insert(t, format("|cffffffff%d|r %s", days, DAYS_ABBR))
302 end
303 if hours > 1 then
304 table.insert(t, format("|cffffffff%d|r %s", hours, HOURS_ABBR_P1))
305 elseif hours == 1 then
306 table.insert(t, format("|cffffffff%d|r %s", hours, HOURS_ABBR))
307 end
308 if mins > 1 then
309 table.insert(t, format("|cffffffff%d|r %s", mins, MINUTES_ABBR_P1))
310 elseif mins == 1 then
311 table.insert(t, format("|cffffffff%d|r %s", mins, MINUTES_ABBR))
312 end
313 if not hideSeconds then
314 if secs > 1 then
315 table.insert(t, format("|cffffffff%d|r %s", secs, SECONDS_ABBR_P1))
316 elseif secs == 1 then
317 table.insert(t, format("|cffffffff%d|r %s", secs, SECONDS_ABBR))
318 end
319 end
320 if table.getn(t) == 0 then
321 if not hideSeconds then
322 return "|cffffffff0|r " .. SECONDS_ABBR_P1
323 else
324 return "|cffffffff0|r " .. MINUTES_ABBR_P1
325 end
326 elseif negative == "-" then
327 return "|cffffffff-|r" .. table.concat(t, " ")
328 else
329 return table.concat(t, " ")
330 end
331 end
332 end
333  
334 local DAY_ONELETTER_ABBR = string.gsub(DAY_ONELETTER_ABBR, "%s*%%d%s*", "")
335 local HOUR_ONELETTER_ABBR = string.gsub(HOUR_ONELETTER_ABBR, "%s*%%d%s*", "")
336 local MINUTE_ONELETTER_ABBR = string.gsub(MINUTE_ONELETTER_ABBR, "%s*%%d%s*", "")
337 local SECOND_ONELETTER_ABBR = string.gsub(SECOND_ONELETTER_ABBR, "%s*%%d%s*", "")
338  
339 function Abacus:FormatDurationFull(duration, colorize, hideSeconds)
340 self:argCheck(duration, 2, "number")
341 local negative = ""
342 if duration ~= duration then
343 duration = 0
344 end
345 if duration < 0 then
346 negative = "-"
347 duration = -duration
348 end
349 if not colorize then
350 if not hideSeconds then
351 if not duration or duration > 86400*365 then
352 return L_UNDETERMINED
353 elseif duration >= 86400 then
354 return format("%s%d%s %02d%s %02d%s %02d%s", negative, duration/86400, L_DAY_ONELETTER_ABBR, mod(duration/3600, 24), L_HOUR_ONELETTER_ABBR, mod(duration/60, 60), L_MINUTE_ONELETTER_ABBR, mod(duration, 60), L_SECOND_ONELETTER_ABBR)
355 elseif duration >= 3600 then
356 return format("%s%d%s %02d%s %02d%s", negative, duration/3600, L_HOUR_ONELETTER_ABBR, mod(duration/60, 60), L_MINUTE_ONELETTER_ABBR, mod(duration, 60), L_SECOND_ONELETTER_ABBR)
357 elseif duration >= 120 then
358 return format("%s%d%s %02d%s", negative, duration/60, L_MINUTE_ONELETTER_ABBR, mod(duration, 60), L_SECOND_ONELETTER_ABBR)
359 else
360 return format("%s%d%s", negative, duration, L_SECOND_ONELETTER_ABBR)
361 end
362 else
363 if not duration or duration > 86400*365 then
364 return L_UNDETERMINED
365 elseif duration >= 86400 then
366 return format("%s%d%s %02d%s %02d%s", negative, duration/86400, L_DAY_ONELETTER_ABBR, mod(duration/3600, 24), L_HOUR_ONELETTER_ABBR, mod(duration/60, 60), L_MINUTE_ONELETTER_ABBR)
367 elseif duration >= 3600 then
368 return format("%s%d%s %02d%s", negative, duration/3600, L_HOUR_ONELETTER_ABBR, mod(duration/60, 60), L_MINUTE_ONELETTER_ABBR)
369 else
370 return format("%s%d%s", negative, duration/60, L_MINUTE_ONELETTER_ABBR)
371 end
372 end
373 else
374 if not hideSeconds then
375 if not duration or duration > 86400*365 then
376 return "|cffffffff"..L_UNDETERMINED.."|r"
377 elseif duration >= 86400 then
378 return format("|cffffffff%s%d|r%s |cffffffff%02d|r%s |cffffffff%02d|r%s |cffffffff%02d|r%s", negative, duration/86400, L_DAY_ONELETTER_ABBR, mod(duration/3600, 24), L_HOUR_ONELETTER_ABBR, mod(duration/60, 60), L_MINUTE_ONELETTER_ABBR, mod(duration, 60), L_SECOND_ONELETTER_ABBR)
379 elseif duration >= 3600 then
380 return format("|cffffffff%s%d|r%s |cffffffff%02d|r%s |cffffffff%02d|r%s", negative, duration/3600, L_HOUR_ONELETTER_ABBR, mod(duration/60, 60), L_MINUTE_ONELETTER_ABBR, mod(duration, 60), L_SECOND_ONELETTER_ABBR)
381 elseif duration >= 120 then
382 return format("|cffffffff%s%d|r%s |cffffffff%02d|r%s", negative, duration/60, L_MINUTE_ONELETTER_ABBR, mod(duration, 60), L_SECOND_ONELETTER_ABBR)
383 else
384 return format("|cffffffff%s%d|r%s", negative, duration, L_SECOND_ONELETTER_ABBR)
385 end
386 else
387 if not duration or duration > 86400*365 then
388 return "|cffffffff"..L_UNDETERMINED.."|r"
389 elseif duration >= 86400 then
390 return format("|cffffffff%s%d|r%s |cffffffff%02d|r%s |cffffffff%02d|r%s", negative, duration/86400, L_DAY_ONELETTER_ABBR, mod(duration/3600, 24), L_HOUR_ONELETTER_ABBR, mod(duration/60, 60), L_MINUTE_ONELETTER_ABBR)
391 elseif duration >= 3600 then
392 return format("|cffffffff%s%d|r%s |cffffffff%02d|r%s", negative, duration/3600, L_HOUR_ONELETTER_ABBR, mod(duration/60, 60), L_MINUTE_ONELETTER_ABBR)
393 else
394 return format("|cffffffff%s%d|r%s", negative, duration/60, L_MINUTE_ONELETTER_ABBR)
395 end
396 end
397 end
398 end
399  
400 function Abacus:FormatDurationShort(duration, colorize, hideSeconds)
401 self:argCheck(duration, 2, "number")
402 local negative = ""
403 if duration ~= duration then
404 duration = 0
405 end
406 if duration < 0 then
407 negative = "-"
408 duration = -duration
409 end
410 if not colorize then
411 if not duration or duration >= 86400*365 then
412 return "***"
413 elseif duration >= 172800 then
414 return format("%s%.1f %s", negative, duration/86400, DAYS_ABBR_P1)
415 elseif duration >= 7200 then
416 return format("%s%.1f %s", negative, duration/3600, HOURS_ABBR_P1)
417 elseif duration >= 120 or not hideSeconds then
418 return format("%s%.1f %s", negative, duration/60, MINUTES_ABBR_P1)
419 else
420 return format("%s%.0f %s", negative, duration, SECONDS_ABBR_P1)
421 end
422 else
423 if not duration or duration >= 86400*365 then
424 return "|cffffffff***|r"
425 elseif duration >= 172800 then
426 return format("|cffffffff%s%.1f|r %s", negative, duration/86400, DAYS_ABBR_P1)
427 elseif duration >= 7200 then
428 return format("|cffffffff%s%.1f|r %s", negative, duration/3600, HOURS_ABBR_P1)
429 elseif duration >= 120 or not hideSeconds then
430 return format("|cffffffff%s%.1f|r %s", negative, duration/60, MINUTES_ABBR_P1)
431 else
432 return format("|cffffffff%s%.0f|r %s", negative, duration, SECONDS_ABBR_P1)
433 end
434 end
435 end
436  
437 function Abacus:FormatDurationCondensed(duration, colorize, hideSeconds)
438 self:argCheck(duration, 2, "number")
439 local negative = ""
440 if duration ~= duration then
441 duration = 0
442 end
443 if duration < 0 then
444 negative = "-"
445 duration = -duration
446 end
447 if not colorize then
448 if hideSeconds then
449 if not duration or duration >= 86400*365 then
450 return format("%s**%s **:**", negative, L_DAY_ONELETTER_ABBR)
451 elseif duration >= 86400 then
452 return format("%s%d%s %d:%02d", negative, duration/86400, L_DAY_ONELETTER_ABBR, mod(duration/3600, 24), mod(duration/60, 60))
453 else
454 return format("%s%d:%02d", negative, duration/3600, mod(duration/60, 60))
455 end
456 else
457 if not duration or duration >= 86400*365 then
458 return negative .. "**:**:**:**"
459 elseif duration >= 86400 then
460 return format("%s%d%s %d:%02d:%02d", negative, duration/86400, L_DAY_ONELETTER_ABBR, mod(duration/3600, 24), mod(duration/60, 60), mod(duration, 60))
461 elseif duration >= 3600 then
462 return format("%s%d:%02d:%02d", negative, duration/3600, mod(duration/60, 60), mod(duration, 60))
463 else
464 return format("%s%d:%02d", negative, duration/60, mod(duration, 60))
465 end
466 end
467 else
468 if hideSeconds then
469 if not duration or duration >= 86400*365 then
470 return format("|cffffffff%s**|r%s |cffffffff**|r:|cffffffff**|r", negative, L_DAY_ONELETTER_ABBR)
471 elseif duration >= 86400 then
472 return format("|cffffffff%s%d|r%s |cffffffff%d|r:|cffffffff%02d|r", negative, duration/86400, L_DAY_ONELETTER_ABBR, mod(duration/3600, 24), mod(duration/60, 60))
473 else
474 return format("|cffffffff%s%d|r:|cffffffff%02d|r", negative, duration/3600, mod(duration/60, 60))
475 end
476 else
477 if not duration or duration >= 86400*365 then
478 return format("|cffffffff%s**|r%s |cffffffff**|r:|cffffffff**|r:|cffffffff**|r", negative, L_DAY_ONELETTER_ABBR)
479 elseif duration >= 86400 then
480 return format("|cffffffff%s%d|r%s |cffffffff%d|r:|cffffffff%02d|r:|cffffffff%02d|r", negative, duration/86400, L_DAY_ONELETTER_ABBR, mod(duration/3600, 24), mod(duration/60, 60), mod(duration, 60))
481 elseif duration >= 3600 then
482 return format("|cffffffff%s%d|r:|cffffffff%02d|r:|cffffffff%02d|r", negative, duration/3600, mod(duration/60, 60), mod(duration, 60))
483 else
484 return format("|cffffffff%s%d|r:|cffffffff%02d|r", negative, duration/60, mod(duration, 60))
485 end
486 end
487 end
488 end
489  
490 AceLibrary:Register(Abacus, MAJOR_VERSION, MINOR_VERSION)
491 Abacus = nil