Software /
code /
prosody
Diff
util/stanza.lua @ 262:8c73fb2ff4a2
A treat for Linux users ;)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 14 Nov 2008 16:03:33 +0000 |
parent | 251:5b6dec537914 |
child | 338:804f5e62a41f |
line wrap: on
line diff
--- a/util/stanza.lua Fri Nov 14 14:26:56 2008 +0000 +++ b/util/stanza.lua Fri Nov 14 16:03:33 2008 +0000 @@ -10,8 +10,11 @@ local print = print; local unpack = unpack; local s_gsub = string.gsub; +local os = os; -local debug = debug; +local do_pretty_printing = not os.getenv("WINDIR"); +local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; + local log = require "util.logger".init("stanza"); module "stanza" @@ -157,9 +160,6 @@ end stanza.tags = tags; end - if not stanza.last_add then - stanza.last_add = {}; - end end return stanza; @@ -195,4 +195,44 @@ return stanza("presence", attr); end +if do_pretty_printing then + local style_attrk = getstyle("yellow"); + local style_attrv = getstyle("red"); + local style_tagname = getstyle("red"); + local style_punc = getstyle("magenta"); + + local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); + local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); + --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, ">"); + local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); + function stanza_mt.pretty_print(t) + local children_text = ""; + for n, child in ipairs(t) do + if type(child) == "string" then + children_text = children_text .. xml_escape(child); + else + children_text = children_text .. child:pretty_print(); + end + end + + local attr_string = ""; + if t.attr then + 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 + end + return s_format(tag_format, t.name, attr_string, children_text, t.name); + end + + function stanza_mt.pretty_top_tag(t) + local attr_string = ""; + if t.attr then + 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 + end + return s_format(top_tag_format, t.name, attr_string); + end +else + -- Sorry, fresh out of colours for you guys ;) + stanza_mt.pretty_print = stanza_mt.__tostring; + stanza_mt.pretty_top_tag = stanza_mt.top_tag; +end + return _M;