Comparison

util/stanza.lua @ 11640:51598e46e136

util.stanza: Simplify and make pretty-printing look nicer I've had this color theme in a local debug module for some time and I quite like it. The colors are from the XMPP logo. Removes extra XML serialization implementation in favor of the standard one. Also removes recursive str=str..more string building. The new two-level gsub has the accumulator in C space so shouldn't be too bad. The inner gsub calls use no callback, so should be fast and not create all that much garbage. No serious benchmarking has been done, but who cares if it looks nice?
author Kim Alvefur <zash@zash.se>
date Sat, 07 Nov 2020 22:09:46 +0100
parent 11639:ad39528e647d
child 11642:7f2dee4249aa
comparison
equal deleted inserted replaced
11639:ad39528e647d 11640:51598e46e136
9 9
10 local error = error; 10 local error = error;
11 local t_insert = table.insert; 11 local t_insert = table.insert;
12 local t_remove = table.remove; 12 local t_remove = table.remove;
13 local t_concat = table.concat; 13 local t_concat = table.concat;
14 local s_format = string.format;
15 local s_match = string.match; 14 local s_match = string.match;
16 local tostring = tostring; 15 local tostring = tostring;
17 local setmetatable = setmetatable; 16 local setmetatable = setmetatable;
18 local getmetatable = getmetatable; 17 local getmetatable = getmetatable;
19 local pairs = pairs; 18 local pairs = pairs;
489 return new_stanza("presence", attr); 488 return new_stanza("presence", attr);
490 end 489 end
491 490
492 if do_pretty_printing then 491 if do_pretty_printing then
493 local getstyle, getstring = termcolours.getstyle, termcolours.getstring; 492 local getstyle, getstring = termcolours.getstyle, termcolours.getstring;
494 local style_attrk = getstyle("yellow"); 493
495 local style_attrv = getstyle("red"); 494 local blue1 = getstyle("1b3967");
496 local style_tagname = getstyle("red"); 495 local blue2 = getstyle("13b5ea");
497 local style_punc = getstyle("magenta"); 496 local green1 = getstyle("439639");
498 497 local green2 = getstyle("a0ce67");
499 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); 498 local orange1 = getstyle("d9541e");
500 local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); 499 local orange2 = getstyle("e96d1f");
501 --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); 500
502 local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); 501 local attr_replace = (
502 getstring(green2, "%1") .. -- attr name
503 getstring(green1, "%2") .. -- equal
504 getstring(orange1, "%3") .. -- quote
505 getstring(orange2, "%4") .. -- attr value
506 getstring(orange1, "%5") -- quote
507 );
508
509 local text_replace = (
510 getstring(green1, "%1") .. -- &
511 getstring(green2, "%2") .. -- amp
512 getstring(green1, "%3") -- ;
513 );
514
515 local function pretty(s)
516 -- Tag soup color
517 -- Outer gsub call takes each <tag>, applies colour to the brackets, the
518 -- tag name, then applies one inner gsub call to colour the attributes and
519 -- another for any text content.
520 return (s:gsub("(</?)([^ >]*)(.-)([?/]?>)([^<]*)", function(opening_bracket, tag_name, attrs, closing_bracket, content)
521 return getstring(blue1, opening_bracket)..getstring(blue2, tag_name)..
522 attrs:gsub("([^=]+)(=)([\"'])(.-)([\"'])", attr_replace) ..
523 getstring(blue1, closing_bracket) ..
524 content:gsub("(&#?)(%w+)(;)", text_replace);
525 end, 100));
526 end
527
503 function stanza_mt.pretty_print(t) 528 function stanza_mt.pretty_print(t)
504 local children_text = ""; 529 return pretty(tostring(t));
505 for _, child in ipairs(t) do
506 if type(child) == "string" then
507 children_text = children_text .. xml_escape(child);
508 else
509 children_text = children_text .. child:pretty_print();
510 end
511 end
512
513 local attr_string = "";
514 if t.attr then
515 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
516 end
517 return s_format(tag_format, t.name, attr_string, children_text, t.name);
518 end 530 end
519 531
520 function stanza_mt.pretty_top_tag(t) 532 function stanza_mt.pretty_top_tag(t)
521 local attr_string = ""; 533 return pretty(t:top_tag());
522 if t.attr then
523 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
524 end
525 return s_format(top_tag_format, t.name, attr_string);
526 end 534 end
527 else 535 else
528 -- Sorry, fresh out of colours for you guys ;) 536 -- Sorry, fresh out of colours for you guys ;)
529 stanza_mt.pretty_print = stanza_mt.__tostring; 537 stanza_mt.pretty_print = stanza_mt.__tostring;
530 stanza_mt.pretty_top_tag = stanza_mt.top_tag; 538 stanza_mt.pretty_top_tag = stanza_mt.top_tag;