-- Compiled with roblox-ts v1.2.7 local TS = _G[script] local _message_templates = TS.import(script, TS.getModule(script, "@rbxts", "message-templates").out) local MessageTemplateParser = _message_templates.MessageTemplateParser local TemplateTokenKind = _message_templates.TemplateTokenKind local t = TS.import(script, TS.getModule(script, "@rbxts", "t").lib.ts) local ZrTextStream = TS.import(script, TS.getModule(script, "@rbxts", "zirconium").out.Ast.TextStream).default local _ThemeContext = TS.import(script, script.Parent, "UIKit", "ThemeContext") local getRichTextColor3 = _ThemeContext.getRichTextColor3 local ZirconTheme = _ThemeContext.ZirconTheme local function formatParse(formatString) local tokens = {} local stream = ZrTextStream.new(formatString) local isNotEndVarBracket = function(c) return c ~= "}" end --[[ * * Reads while the specified condition is met, or the end of stream ]] local function readWhile(condition) local src = "" while stream:hasNext() == true and condition(stream:peek()) == true do src ..= stream:next() end return src end local str = "" while stream:hasNext() do local char = stream:next() if char == "{" then local _arg0 = { Type = "Text", Value = str, } -- ▼ Array.push ▼ tokens[#tokens + 1] = _arg0 -- ▲ Array.push ▲ str = "" local variable = readWhile(isNotEndVarBracket) local _arg0_1 = { Type = "Variable", Value = variable, } -- ▼ Array.push ▼ tokens[#tokens + 1] = _arg0_1 -- ▲ Array.push ▲ stream:next() else str ..= char end end if str ~= "" then local _arg0 = { Type = "Text", Value = str, } -- ▼ Array.push ▼ tokens[#tokens + 1] = _arg0 -- ▲ Array.push ▲ end return tokens end local isArray = t.array(t.any) local isMap = t.map(t.string, t.any) local function formatRichText(value, level, theme) if level == nil then level = 1 end if type(value) == "string" then return getRichTextColor3(theme, "Green", value) elseif type(value) == "number" or type(value) == "boolean" then return getRichTextColor3(theme, "Cyan", tostring(value)) elseif isArray(value) then if level > 1 then return getRichTextColor3(theme, "Grey", "[...]") else local _arg0 = function(v) return formatRichText(v, level + 1, theme) end -- ▼ ReadonlyArray.map ▼ local _newValue = table.create(#value) for _k, _v in ipairs(value) do _newValue[_k] = _arg0(_v, _k - 1, value) end -- ▲ ReadonlyArray.map ▲ return getRichTextColor3(ZirconTheme, "Grey", "[" .. (table.concat(_newValue, ", ") .. "]")) end elseif isMap(value) then if level > 1 then return getRichTextColor3(theme, "Grey", "{...}") else local arr = {} for k, v in pairs(value) do local _arg0 = getRichTextColor3(theme, "White", k) .. (": " .. formatRichText(v, level + 1, theme)) -- ▼ Array.push ▼ arr[#arr + 1] = _arg0 -- ▲ Array.push ▲ end return getRichTextColor3(theme, "Grey", "{" .. (table.concat(arr, ", ") .. "}")) end elseif typeof(value) == "Instance" then return getRichTextColor3(theme, "Orange", value:GetFullName()) elseif value == nil then return getRichTextColor3(theme, "Cyan", "undefined") else return getRichTextColor3(theme, "Yellow", "<" .. (tostring(value) .. ">")) end end local function formatPlainText(value, level) if level == nil then level = 1 end if type(value) == "string" or (type(value) == "number" or type(value) == "boolean") then return tostring(value) elseif isArray(value) then if level > 1 then return "[...]" else local _arg0 = function(v) return formatPlainText(v, level + 1) end -- ▼ ReadonlyArray.map ▼ local _newValue = table.create(#value) for _k, _v in ipairs(value) do _newValue[_k] = _arg0(_v, _k - 1, value) end -- ▲ ReadonlyArray.map ▲ return "[" .. (table.concat(_newValue, ", ") .. "]") end elseif isMap(value) then if level > 1 then return "{...}" else local arr = {} for k, v in pairs(value) do local _arg0 = k .. (": " .. formatPlainText(v, level + 1)) -- ▼ Array.push ▼ arr[#arr + 1] = _arg0 -- ▲ Array.push ▲ end return "{" .. (table.concat(arr, ", ") .. "}") end elseif typeof(value) == "Instance" then return value:GetFullName() elseif value == nil then return "undefined" else return tostring(value) end end local function formatTokensPlain(tokens, vars) local resultingStr = "" local idxOffset = 0 for _, token in ipairs(tokens) do if token.Type == "Text" then resultingStr ..= token.Value elseif token.Type == "Variable" then if token.Value == "" then if idxOffset > #vars then resultingStr ..= "{" .. (token.Value .. "}") else resultingStr ..= formatPlainText(vars[idxOffset + 1]) idxOffset += 1 end end end end return resultingStr end local function formatMessageTemplate(template, values) local tokens = MessageTemplateParser.GetTokens(template) for _, token in ipairs(tokens) do if token.kind == TemplateTokenKind.Property then local value = values[token.propertyName] return formatRichText(value, nil, ZirconTheme) end end end local function formatTokens(tokens, vars) local resultingStr = "" local idxOffset = 0 for _, token in ipairs(tokens) do if token.Type == "Text" then resultingStr ..= token.Value elseif token.Type == "Variable" then if token.Value == "" then if idxOffset > #vars then resultingStr ..= getRichTextColor3(ZirconTheme, "Red", "{" .. (token.Value .. "}")) else resultingStr ..= formatRichText(vars[idxOffset + 1], nil, ZirconTheme) idxOffset += 1 end end end end return resultingStr end return { formatParse = formatParse, formatRichText = formatRichText, formatTokensPlain = formatTokensPlain, formatMessageTemplate = formatMessageTemplate, formatTokens = formatTokens, }