Copy in Color

Share your scripts and packages with other Mudlet users.
Post Reply
Posts: 1099
Joined: Wed Apr 03, 2013 2:19 am

Copy in Color

Post by Jor'Mox » Tue Sep 25, 2018 1:18 pm

I created two functions to capture text from a window in color, suited either for writing in labels or using with decho. They are copy2html and copy2decho. They both take a window name (defaults to "main"), the string you want to copy from that line (defaults to the entire line), and the instance of that string in the line that you want to copy (defaults to 1).
Code: [show] | [select all] lua
local function copy2color(name,win,str,inst)
    local line = getCurrentLine(win or "main")
    if (not str and line == "ERROR: mini console does not exist") or type(str) == "number" then
        win, str, inst = "main", win, str
        line = getCurrentLine(win)
    win = win or "main"
    str = str or line
    inst = inst or 1
    local start, len = selectString(win, str, inst), #str
    if not start then
        error(name..": string not found",3)
    local style, endspan, result, r, g, b, br, bg, bb, cr, cg, cb, crb, cgb, cbb
    if name == "copy2html" then
        style = "%s<span style=\'color: rgb(%d,%d,%d);background: rgb(%d,%d,%d);'>%s"
        endspan = "</span>"
    elseif name == "copy2decho" then
        style = "%s<%d,%d,%d:%d,%d,%d>%s"
        endspan = "<r>"
    for index = start + 1, start + len do
        if win ~= "main" then
            selectSection(win, index - 1, 1)
            selectSection(index - 1, 1)
        r,g,b = getFgColor(win)
        rb,gb,bb = getBgColor(win)
        if not table.is_empty(table.complement({r,g,b,rb,gb,bb},{cr,cg,cb,crb,cgb,cbb})) then
            cr,cg,cb,crb,cgb,cbb = r,g,b,rb,gb,bb
            result = string.format(style, result and (result..endspan) or "", r, g, b, rb, gb, bb, line:sub(index, index))
            result = result .. line:sub(index, index)
    result = result .. endspan
    if name == "copy2html" then
        local conversions = {["¦"] = "&brvbar;", ["×"] = "&times;", ["«"] = "&#171;", ["»"] = "&raquo;"}
        for from, to in pairs(conversions) do
            result = string.gsub(result, from, to)
    return result

function copy2html(...)
    return copy2color("copy2html",...)

function copy2decho(...)
    return copy2color("copy2decho",...)
As an example, if you had a trigger capturing something that you wanted to display on a label, you could use the following code to do so with these functions: echo("my_label", copy2html(matches[2]))

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests