vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2  
3 Sea Print (Sea.io)
4  
5 A Mini-Library for standardized print functions.
6  
7 Compiler:
8 AnduinLothar (karlkfi@cosmosui.org)
9  
10 ==Installation/Utilization==
11  
12 Embedding:
13 - Drop the SeaPrint folder into your Interface\AddOns\YourAddon\ folder
14 - Add Sea and SeaPrint as optional dependancies
15 - Add the following line to the end of your TOC file, before your addon files:
16 SeaPrint\ SeaPrint.lua
17  
18 Standard:
19 - Drop the SeaPrint folder into your Interface\AddOns\ directory
20 - Add SeaPrint a required dependancy
21  
22  
23 Change Log:
24 v0.2
25 - Added makeHyperlink
26 - Changed lua file name
27 v0.1 (Alpha)
28 - SeaPrint Forked into Mini-Library from the main Sea. Still backwards compatible.
29  
30  
31 $LastChangedBy: karlkfi $
32 $Rev: 2577 $
33 $Date: 2005-10-10 14:44:01 -0700 (Mon, 10 Oct 2005) $
34 ]]--
35  
36 local SEA_PRINT_VERSION = 0.20;
37 local loadThisEmbeddedInstance;
38  
39 ------------------------------------------------------------------------------
40 --[[ Embedded Sub-Library Load Algorithm ]]--
41 ------------------------------------------------------------------------------
42  
43 if(not Sea) then
44 Sea = {};
45 Sea.versions = {};
46 Sea.versions.SeaPrint = SEA_PRINT_VERSION;
47 loadThisEmbeddedInstance = true;
48 Sea.io = {};
49 else
50 if(not Sea.versions) then
51 Sea.versions = {};
52 end
53 if (not Sea.versions.SeaPrint) or (Sea.versions.SeaPrint < SEA_PRINT_VERSION) then
54 Sea.versions.SeaPrint = SEA_PRINT_VERSION;
55 loadThisEmbeddedInstance = true;
56 end
57 if(not Sea.io) then
58 Sea.io = {};
59 end
60 end
61  
62  
63 if (loadThisEmbeddedInstance) then
64 loadThisEmbeddedInstance = nil;
65  
66 ------------------------------------------------------------------------------
67 --[[ Variables ]]--
68 ------------------------------------------------------------------------------
69  
70 SEA_DEBUG = false;
71 SEA_ERROR = false;
72  
73 -- Default chat frame set to ChatFrame1
74 Sea.io.DEFAULT_PRINT_FRAME = ChatFrame1;
75  
76 -- Default error frame set to ChatFrame1
77 Sea.io.DEFAULT_ERROR_FRAME = ChatFrame1;
78  
79 -- Default banner frame
80 Sea.io.DEFAULT_BANNER_FRAME = UIErrorsFrame;
81  
82 -- Default color scheme
83 Sea.io.DEFAULT_ERROR_COLOR = RED_FONT_COLOR;
84 Sea.io.DEFAULT_PRINT_COLOR = NORMAL_FONT_COLOR;
85  
86 -- Default Debug Tag
87 Sea.io.debugKey = "SEA_DEBUG";
88  
89 -- Default Error Tag
90 Sea.io.errorKey = "SEA_ERROR";
91  
92 -- Recursive check
93 Sea.io.recursed = false;
94  
95 ------------------------------------------------------------------------------
96 --[[ Helper Functions ]]--
97 ------------------------------------------------------------------------------
98  
99 --local func from SeaString
100 local byteString;
101 if (Sea.string and Sea.string.byte) then
102 byteString = Sea.string.byte;
103 else
104 byteString = function(c) return format("<%02X>",string.byte(c)) end;
105 end
106  
107 --
108 -- join(list, separator)
109 --
110 -- Arguments:
111 -- (table list, String separator)
112 -- list - table of things to join
113 -- separator - the separator to place between objects
114 --
115 -- Returns:
116 -- (string joinedstring)
117 -- joinedstring - the list.toString() joined by separator(s)
118 --
119 -- Written by Thott (thott@thottbot.com)
120 Sea.io.join = function (list, separator)
121 -- Type check
122 if ( not list or type(list) ~= "table" ) then
123 ChatFrame1:AddMessage("Non-table passed to Sea.io.join");
124 return;
125 end
126 if ( separator == nil ) then separator = ""; end
127  
128 local i;
129 local c = "";
130 local msg = "";
131 local currType;
132 for i,v in list do
133 if ( tonumber(i) ) then
134 currType = type(v);
135 if( currType == "string" and currType == "number") then
136 msg = msg .. c .. v;
137 else
138 msg = msg .. c .. "(" .. tostring(v) .. ")";
139 end
140 c = separator;
141 end
142 end
143 return msg;
144 end
145  
146 --Alias
147 if (not Sea.util) then
148 Sea.util = {};
149 end
150 Sea.util.join = Sea.io.join;
151 if (not Sea.string) then
152 Sea.string = {};
153 end
154 Sea.string.join = Sea.io.join;
155  
156 ------------------------------------------------------------------------------
157 --[[ Standard Prints ]]--
158 ------------------------------------------------------------------------------
159  
160 --
161 -- print ( ... )
162 --
163 -- Arguments
164 -- () arg
165 -- arg - the values to be printed
166 --
167 -- Returns
168 -- (nil)
169 --
170 Sea.io.print = function(...)
171 Sea.io.printf(nil, unpack(arg));
172 end
173  
174 --
175 -- banner ( ... )
176 --
177 -- Arguments
178 -- () arg
179 -- arg - the values to be printed
180 --
181 -- Returns
182 -- (nil)
183 --
184 Sea.io.banner = function(...)
185 Sea.io.printf(Sea.io.DEFAULT_BANNER_FRAME, unpack(arg));
186 end
187  
188 --
189 -- error (...)
190 --
191 -- prints just like Sea.io.print, except as an error
192 --
193 -- Arguments:
194 -- () arg
195 -- arg - contains all error output
196 --
197 Sea.io.error = function(...)
198 Sea.io.errorfc(nil, nil, unpack(arg) );
199 end
200  
201 --
202 -- dprint (string debugkey, ...)
203 --
204 -- prints a message when getglobal(debugkey) is true
205 --
206 -- Arguments:
207 -- (string debugkey) arg
208 --
209 Sea.io.dprint = function ( debugKey, ... )
210 Sea.io.dprintf(debugKey, Sea.io.DEFAULT_PRINT_FRAME, unpack(arg));
211 end
212  
213  
214 --
215 -- dprintc (string debugkey, Table[r,g,b] color, ...)
216 --
217 -- prints a message when getglobal(debugkey) is true
218 -- in the color specified by color
219 --
220 -- Arguments:
221 -- (string debugkey, Table[r,g,b] color) arg
222 --
223 Sea.io.dprintc = function ( debugKey, color, ... )
224 Sea.io.dprintfc(debugKey, Sea.io.DEFAULT_PRINT_FRAME, color, unpack(arg));
225 end
226  
227 --
228 -- derror (string errorKey, ...)
229 --
230 -- prints an error when getglobal(errorKey) is true
231 --
232 -- Arguments:
233 -- (string errorKey) arg
234 --
235 Sea.io.derror = function ( errorKey, ... )
236 Sea.io.derrorf(errorKey, Sea.io.DEFAULT_ERROR_FRAME, unpack(arg));
237 end
238  
239 --
240 -- derrorf (string errorKey, MessageFrame frame, ...)
241 --
242 -- prints an error when getglobal(errorKey) is true
243 --
244 -- Arguments:
245 -- (string errorKey, MessageFrame frame) arg
246 --
247 Sea.io.derrorf = function ( errorKey, frame, ... )
248 Sea.io.derrorfc(errorKey, frame, Sea.io.DEFAULT_ERROR_COLOR, unpack(arg));
249 end
250  
251 --
252 -- derrorc (string errorKey, Table[r,g,b] color, ...)
253 --
254 -- prints an error when getglobal(errorKey) is true
255 -- in the color specified by color
256 --
257 -- Arguments:
258 -- (string errorKey, Table[r,g,b] color) arg
259 --
260 Sea.io.derrorc = function ( errorKey, color, ... )
261 Sea.io.derrorfc(errorKey, Sea.io.DEFAULT_ERROR_FRAME, color, unpack(arg));
262 end
263  
264 --
265 -- derrorfc (string errorKey, MessageFrame frame, Table[r,g,b] color, ...)
266 --
267 -- prints an error when getglobal(errorKey) is true
268 -- in the frame specified, in the color specified
269 --
270 -- Arguments:
271 -- (string errorKey, MessageFrame frame, Table[r,g,b] color) arg
272 --
273 --
274 Sea.io.derrorfc = function ( errorKey, frame, color, ... )
275 if ( type(errorKey) ~= "string" ) then
276 if ( type(errorKey) == "nil" ) then
277 errorKey = Sea.io.errorKey;
278 else
279 --Sea.io.error("Invalid error key. Type: ", type(errorKey));
280 end
281 end
282 if ( getglobal(errorKey) == true ) then
283 Sea.io.errorfc(frame, color, unpack(arg));
284 end
285 end
286  
287  
288 --
289 -- dbanner (string debugkey, ...)
290 --
291 -- prints a banner when getglobal(debugkey) is true
292 --
293 -- Arguments:
294 -- (string debugkey) arg
295 --
296 Sea.io.dbanner = function ( debugKey, ... )
297 Sea.io.dprintf(debugKey, Sea.io.DEFAULT_BANNER_FRAME, unpack(arg));
298 end
299  
300 --
301 -- dbannerc (string debugkey, Table[r,g,b] ...)
302 --
303 -- prints a banner when getglobal(debugkey) is true
304 -- in the color specified by color
305 --
306 -- Arguments:
307 -- (string debugkey, Table[r,g,b] color) arg
308 --
309 Sea.io.dbannerc = function ( debugKey, color, ... )
310 Sea.io.dprintfc(debugKey, Sea.io.DEFAULT_BANNER_FRAME, color, unpack(arg));
311 end
312  
313 --
314 -- printf (MessageFrame frame, ...)
315 --
316 -- prints a message in a message frame
317 --
318 -- Arguments:
319 -- (MessageFrame frame) arg
320 --
321 -- frame - the object with AddMessage(self, string)
322 -- arg - the string to be composed
323 --
324 -- Returns
325 -- (nil)
326 --
327 Sea.io.printf = function (frame, ... )
328 Sea.io.printfc(frame, nil, unpack(arg));
329 end
330  
331 --
332 -- dprintf (string debugkey, MessageFrame frame, ...)
333 --
334 -- prints a message when getglobal(debugkey) is true
335 -- also decodes | and characters
336 --
337 -- Arguments:
338 -- (string debugkey, MessageFrame frame) arg
339 -- debugkey - string debug key
340 -- frame - debug target frame
341 --
342 Sea.io.dprintf = function ( debugKey, frame, ... )
343 Sea.io.dprintfc(debugKey, frame, nil, unpack(arg));
344 end
345  
346 --
347 -- dprintfc (string debugkey, MessageFrame frame, Table[r,g,b] color, ...)
348 --
349 -- prints a message when getglobal(debugkey) is true
350 -- also decodes | and characters, using the specified color
351 --
352 -- Arguments:
353 -- (string debugkey, MessageFrame frame) arg
354 -- debugkey - string/boolean debug key
355 -- if nil, then "getglobal(Sea.io.debugKey)" is used;
356 -- if string, then "getglobal(debugKey)" is used;
357 -- if boolean or non-string it is evaluated to print using "if (debugKey) then";
358 -- frame - debug target frame
359 -- color - table of colors
360 --
361 Sea.io.dprintfc = function ( debugKey, frame, color, ... )
362 if ( type(debugKey) == "string" ) then
363 debugKey = getglobal(debugKey);
364 elseif ( debugKey == nil ) then
365 debugKey = getglobal(Sea.io.debugKey);
366 end
367  
368 local msg = Sea.io.join(arg,"");
369 msg = gsub(msg,"|","<pipe>");
370 msg = gsub(msg,"([^%w%s%a%p])", byteString);
371  
372 if ( debugKey ) then
373 Sea.io.printfc(frame, color, unpack(arg));
374 end
375 end
376  
377  
378 --
379 -- errorc (Table[r,g,b] color, ...)
380 --
381 -- prints just like Sea.io.print, except as an error with the color
382 --
383 -- Arguments:
384 -- (Table[r,g,b] color) arg
385 -- color - the specified color
386 -- arg - contains all error output
387 --
388 Sea.io.errorc = function(color, ...)
389 Sea.io.errorfc(Sea.io.DEFAULT_ERROR_FRAME, color, unpack(arg) );
390 end
391  
392 --
393 -- errorf (MessageFrame frame, ...)
394 --
395 -- prints a message in an error message frame
396 --
397 -- Arguments:
398 -- (MessageFrame frame) arg
399 --
400 -- frame - the object with AddMessage(self, string)
401 -- arg - the string to be composed
402 --
403 -- Returns
404 -- (nil)
405 --
406  
407 Sea.io.errorf = function (frame, ... )
408 Sea.io.errorfc(frame, nil, unpack(arg));
409 end
410  
411 --
412 -- errorfc (MessageFrame frame, Table[r,g,b] color, ...)
413 --
414 -- prints a message in an error message frame with the color
415 --
416 -- Arguments:
417 -- (MessageFrame frame, Table[r,g,b] color) arg
418 --
419 -- frame - the object with AddMessage(self, string)
420 -- color - table containing the colors
421 -- arg - the string to be composed
422 --
423 -- Returns
424 -- (nil)
425 --
426 Sea.io.errorfc = function (frame, color, ... )
427 if ( frame == nil ) then
428 frame = Sea.io.DEFAULT_ERROR_FRAME;
429 end
430 if ( color == nil ) then
431 color = Sea.io.DEFAULT_ERROR_COLOR;
432 end
433  
434 Sea.io.printfc(frame, color, unpack(arg));
435 end
436 --
437 -- printc ( ColorTable[r,g,b] color, ... )
438 --
439 -- prints a message in the default frame with a
440 -- specified color
441 --
442 -- Arguments:
443 -- color - the color
444 -- arg - the message
445 --
446 Sea.io.printc = function ( color, ... )
447 Sea.io.printfc(nil, color, unpack(arg));
448 end
449  
450 --
451 -- bannerc ( ColorTable[r,g,b] color, ... )
452 --
453 -- prints a banner message with a
454 -- specified color
455 --
456 -- Arguments:
457 -- color - the color
458 -- arg - the message
459 --
460 Sea.io.bannerc = function ( color, ... )
461 if ( color == nil ) then
462 color = Sea.io.DEFAULT_PRINT_COLOR;
463 end
464  
465 Sea.io.printfc(Sea.io.DEFAULT_BANNER_FRAME, color, unpack(arg));
466 end
467  
468 --
469 -- printfc (MessageFrame frame, ColorTable[r,g,b] color, ... )
470 --
471 -- prints a message in a frame with a specified color
472 --
473 -- Arguments
474 -- frame - the frame
475 -- color - a table with .r .g and .b values
476 -- arg - the message objects
477 --
478 Sea.io.printfc = function (frame, color, ... )
479 if ( frame == nil ) then
480 frame = Sea.io.DEFAULT_PRINT_FRAME;
481 end
482 if ( color == nil ) then
483 color = Sea.io.DEFAULT_PRINT_COLOR;
484 end
485  
486 if ( Sea.io.recursed == false ) then
487 Sea.io.recursed = true;
488 if ( frame == Sea.io.DEFAULT_BANNER_FRAME ) then
489 frame:AddMessage(Sea.io.join(arg,""), color.r, color.g, color.b, 1.0, UIERRORS_HOLD_TIME);
490 else
491 frame:AddMessage(Sea.io.join(arg,""), color.r, color.g, color.b);
492 end
493 Sea.io.recursed = false;
494 else
495 if ( frame == Sea.io.DEFAULT_BANNER_FRAME ) then
496 frame:AddMessage(arg[1], color.r, color.g, color.b, 1.0, UIERRORS_HOLD_TIME);
497 else
498 frame:AddMessage(arg[1], color.r, color.g, color.b);
499 end
500 end
501 end
502  
503 --[[ End of Standard Prints ]]--
504  
505 --[[ Beginning of Special Prints ]]--
506  
507 --
508 -- printComma (...)
509 --
510 -- Prints the arguments separated by commas
511 --
512 Sea.io.printComma = function(...)
513 Sea.io.print(Sea.io.join(arg,","));
514 end;
515  
516 --
517 -- printTable (table, [rowname, level])
518 --
519 -- Recursively prints a table
520 --
521 -- Args:
522 -- table - table to be printed
523 -- rowname - row's name
524 -- level - level of depth
525 --
526 Sea.io.printTable = function (table, rowname, level)
527 if ( level == nil ) then level = 1; end
528  
529 if ( type(rowname) == "nil" ) then rowname = "ROOT";
530 elseif ( type(rowname) == "string" ) then
531 rowname = "\""..rowname.."\"";
532 elseif ( type(rowname) ~= "number" ) then
533 rowname = "*"..type(rowname).."*";
534 end
535  
536 local msg = "";
537 for i=1,level, 1 do
538 msg = msg .. " ";
539 end
540  
541 if ( table == nil ) then
542 Sea.io.print(msg,"[",rowname,"] := nil "); return
543 end
544 if ( type(table) == "table" ) then
545 Sea.io.print (msg,rowname," { ");
546 for k,v in table do
547 Sea.io.printTable(v,k,level+1);
548 end
549 Sea.io.print(msg,"}");
550 elseif (type(table) == "function" ) then
551 Sea.io.print(msg,"[",rowname,"] => {{FunctionPtr*}}");
552 elseif (type(table) == "userdata" ) then
553 Sea.io.print(msg,"[",rowname,"] => {{UserData}}");
554 elseif (type(table) == "boolean" ) then
555 local value = "true";
556 if ( not table ) then
557 value = "false";
558 end
559 Sea.io.print(msg,"[",rowname,"] => ",value);
560 else
561 Sea.io.print(msg,"[",rowname,"] => ",table);
562 end
563 end
564  
565 --[[ Hyperlinks ]] --
566 --
567 -- makeHyperlink(string type, string linkText, Table[r,g,b] color)
568 --
569 -- Creates a hyperlink string which is returned to you.
570 --
571 -- Args:
572 -- (string type, string linkText, Table[r,g,b] color, boolean braces, table[left,right] braceString)
573 -- linkType - the Hyperlink type.
574 -- linkText - the text shown in the link
575 -- color - color of the link
576 -- braces - if true, add braces
577 -- braceString - table with .left for left brace and .right for right brace
578 --
579 Sea.io.makeHyperlink = function (linkType, linkText, color, braces, braceString)
580 local link = linkText;
581 if ( braces ) then
582 if ( braceString == nil ) then braceString = {}; end
583 if ( braceString.left == nil ) then braceString.left="["; end
584 if ( braceString.right == nil ) then braceString.right="]"; end
585  
586 link = braceString.left..link..braceString.right;
587 end
588 if (color) then
589 link = "|cFF"..color..link.."|r";
590 end
591 return "|H"..linkType.."|h"..link.."|h";
592 end
593  
594 -- Aliasing
595 if (not Sea.util) then
596 Sea.util = {};
597 end
598 Sea.util.makeHyperlink = Sea.io.makeHyperlink;
599  
600 -- Aliases:
601 Sea.IO = Sea.io;
602  
603 end
604