Beautify String Formatter

Share your scripts and packages with other Mudlet users.
Post Reply
User avatar
Belgarath
Posts: 232
Joined: Fri Jul 26, 2013 7:19 am
Discord: macjabeth#7149

Beautify String Formatter

Post by Belgarath »

I made this to help me with changing the format of multiple strings in a line. It's been most useful to me, and it's easy to use. Posted it a while ago deep inside another post but decided to put it here so it's easier to find if anyone wants to use it. :P
Code: [show] | [select all] lua
--[[
USAGE: used in the format of: string. formatted table

> beautify(line, {fg = {255,0,0}, bold = true}, matches[2], {fg = "yellow", italics = true})
> beautify("this is a really long string", {replace = "short string"})

^Your piercing gaze bores into (.+) and saps away \w+ resolve to contend with your might\.$

beautify(line, {
    fg = "medium_turquoise",
    bold = true,
    replace = "You have weakened " .. matches[2] .. ".",
  }, matches[2], {
    fg = "light_blue",
  })
]]

function beautify (...)
  local args = { ... }
  if not args or #args == 0 then
    return cecho "<red>I'm not a mind reader."
  end
  for i = 2, #args, 2 do
    local s = args[i - 1]
    local a = args[i]
    if type(s) == "table" then
      selectString(s[1], s[2])
    else
      selectString(s, 1)
    end
    if a.fg then
      if type(a.fg) == "table" then
        setFgColor(unpack(a.fg))
      else
        fg(a.fg)
      end
    end
    if a.bg then
      if type(a.bg) == "table" then
        setBgColor(unpack(a.bg))
      else
        bg(a.bg)
      end
    end
    if a.bold then setBold(a.bold) end
    if a.italics then setItalics(a.italics) end
    if a.underline then setUnderline(a.underline) end
    if a.replace then replace(a.replace) end
  end
  deselect()
  resetFormat()
end

Post Reply