Auto Wrapping Console Class

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

Auto Wrapping Console Class

Post by Jor'Mox » Fri Apr 07, 2017 3:58 pm

This script creates the autoWrap class that is a mini console that automatically rewraps text as it is resized or has the font size changed, and allows access to the most commonly used mini console functions. For functions that don't affect the text or the size of the text display area in the mini console, you would just use the regular functions with the same name you used when creating the autoWrap object, while for the relevant functions, you can either call them as methods on the object returned when the autoWrap object is created, or call them via autoWrap.<function>(name,...).

Supported Functions
resize, clear, append, setFontSize, echo, cecho, decho, hecho, deleteLine, moveCursor, moveCursorEnd, insertText, cinsertText

The resize function mimics the resizeWindow function, the clear function mimics the clearWindow function, the append function mimics the appendBuffer function, and the setFontSize function mimics the setMiniConsoleFontSize function. Additionally, there is a delete function which clears the window, hides it, and removes it from the list of autoWrap consoles.

To create a new autoWrap window, use the autoWrap.new function. Syntax: autoWrap.new(name, font_size, [optional] x, y, width, height). Ex: my_win = autoWrap.new("my console",12,0,0,300,400)
Code: [show] | [select all] lua
autoWrap = autoWrap or {}
autoWrap.list = autoWrap.list or {}

local function rewrap_window(name)
    local info = autoWrap.list[name]
    local buffer = name .. "_autoWrap_buffer"
    local wrap = math.floor(info.width / calcFontSize(info.font_size))
    local line, moved
    setWindowWrap(name,wrap)
    clearWindow(name)
    line = 0
    moved = moveCursor(buffer,1,line)
    while moved do
        selectCurrentLine(buffer)
        copy(buffer)
        line = line + 1
        moved = moveCursor(buffer,1,line)
        appendBuffer(name)
    end
end

autoWrap.new = function (name,f,x,y,w,h)
    assert(name, "autoWrap.new: window name required")
    assert(not autoWrap.list[name], "autoWrap.new: window already exists")
    assert(f, "autoWrap.new: font size required")
    x = x or 0
    y = y or 0
    w = w or 0
    h = h or 0
    createMiniConsole(name,x,y,w,h)
    setMiniConsoleFontSize(name,f)
    createBuffer(name.."_autoWrap_buffer")
    setWindowWrap(name.."_autoWrap_buffer",1000)
    local obj = {name = name}
    setmetatable(obj,{__index = autoWrap})
    autoWrap.list[name] = {width = w, height = h, font_size = f, object = obj}
    return obj
end

autoWrap.resize = function (name,w,h)
    name = name.name or name
    assert(autoWrap.list[name],"autoWrap.resize: no such window.")
    assert(w and h,"autoWrap.resize: width and height required")
    resizeWindow(name,w,h)
    autoWrap.list[name].width = w
    autoWrap.list[name].height = h
    rewrap_window(name)
end

autoWrap.setFontSize = function (name,f)
    name = name.name or name
    assert(autoWrap.list[name],"autoWrap.setFontSize: no such window.")
    assert(f,"autoWrap.setFontSize: font size required")
    setMiniConsoleFontSize(name,f)
    autoWrap.list[name].font_size = f
    rewrap_window(name)
end

autoWrap.delete = function (name)
    name = name.name or name
    clearWindow(name)
    hideWindow(name)
    clearWindow(name.."_autoWrap_buffer")
    list[name] = nil
end

local function run_console(func,f_name,name,...)
    name = name.name or name
    assert(autoWrap.list[name],"autoWrap."..f_name..": no such window.")
    func(name, ...)
    func(name .. "_autoWrap_buffer", ...)
end

function autoWrap.clear(name) run_console(clearWindow,"clear",name) end
function autoWrap.append(name) run_console(appendBuffer,"append",name) end
function autoWrap.echo(name, text) run_console(echo,"echo",name,text) end
function autoWrap.cecho(name, text) run_console(cecho,"cecho",name,text) end
function autoWrap.hecho(name, text) run_console(hecho,"hecho",name,text) end
function autoWrap.decho(name, text) run_console(decho,"decho",name,text) end
function autoWrap.deleteLine(name) run_console(deleteLine,"deleteLine",name) end
function autoWrap.moveCursor(name,x,y) run_console(moveCursor,"moveCursor",name,x,y) end
function autoWrap.moveCursorEnd(name) run_console(moveCursorEnd,"moveCursorEnd",name) end
function autoWrap.insertText(name,text) run_console(insertText,"insertText",name,text) end
function autoWrap.cinsertText(name,text) run_console(cinsertText,"cinsertText",name,text) end

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests