--[[nef_20221122 FEATURED_TEXT Assorted utilities to support featured text displayed in chat log and client Lua console. Basic functions provided by bbcode, color, link, and link_tile. bbcode: simple 'buis' codes. color: foreground and/or background colors. link: hot link to userdata objects Tile, Unit, or City. link_tile: provides a Tile link for Unit or City (use this if the object is being destroyed). link_quick: creates a short form link using a unique name for the default. link, link_tile, link_quick take an optional 2nd argument for user supplied text. default is city name or unit type (for link, link_tile). also included ftext: the internal table (called as a function) used to generate default text. ]] do ---- FEATURED_TEXT local _G = _G local _ENV = _ENV local MF = _G.math.floor local type = _G.tolua.type local MT_txt = { __index = function () return function () return '' end end; __call = function(t,v) local Type = type(v) return t[Type](v),Type end; } local ids = setmetatable( { Tile = function(object) return " x="..MF(object.x).." y="..MF(object.y) end; Unit = function(object) return " id=".. MF(object.id) end; City = function(object) return " id=".. MF(object.id) end; },MT_txt ); local prefix = function(id, Type) return ("[link tgt=%q"):format(Type:lower())..id end; local ft = function(object, text) local id, Type = ids (object) return id == '' and '' or prefix(id, Type) .. ("]%s[/l]"):format(text) end; local function DUP(t) return t,t end local _ENV = _ENV _ENV = _G.log --[[ disable this if you want featured to be global; or change it to any other global table.]] _ENV, featured = DUP {} ftext = _G.setmetatable( { Tile = function(object) return '('..MF(object.x)..','..MF(object.y)..')' end; Unit = function(object) return object.utype:name_translation() end; City = function(object) return object.name end; },MT_txt); bbcode = function(bb,text) return "["..bb.."]"..text.."[/"..bb.."]" end; color = function(text, fg, bg) return "[color" .. (fg and (" fg=%q"):format(fg) or "") .. (bg and (" bg=%q"):format(bg) or "") .. ']'..text.."[/c]" end; link_short = function(object,text) local id, Type = ids (object) return id == '' and '' or prefix(id, Type) .. id .. (text and ('name=%q '):format(text) or '')..'/]' end; link = function(object, text) return ft(object, text or ftext (object)) end; link_tile = function(object, text) return ft(object.tile or object, text or ftext (object)) end; end ----------- end FEATURED_TEXT