Page 2 of 4

Re: function to easily align text for display (actually, 4 n

Posted: Sat Feb 12, 2011 12:21 am
by Omni
As I said in the feature request, would love if these became natural mudlet functions.

Re: function to easily align text for display (actually, 4 n

Posted: Tue Sep 20, 2011 11:06 pm
by Vadi
Hm does calign respect the tags? When I use cecho(calign(...)), it's off the center.

Re: function to easily align text for display (actually, 4 n

Posted: Tue Sep 20, 2011 11:27 pm
by tsuujin
Here's my alignment function (called "bar" because I primarily use it to display a line of text pre- and post-fixed with centering characters), which strips tags for the alignment and then reinserts them afterwards. You can probably use this to modify demonnic's code.
Code: [show] | [select all] lua
function bar(s,c)
    local txtOnly = s:gsub("\<.+\>","")
    local sLen = txtOnly:len()
    local bLen = 100
    local bThreshhold = math.floor(bLen / 2)
    c = c or "-"
    local maxWidth = 0
    
    local function printBar(s,c)
        local sLen = string.gsub(s,"\<%w+\>",""):len()
        local minMaxDelta = maxWidth
        local sideLen = (bLen - (sLen+4)) / 2
        local side = string.rep(c,sideLen)
        cecho(string.format("\n<red>%s[ <white>%s <red>]%s",side,s,side))
    end


    if sLen > bThreshhold then
        -- Split the message, and recursively call the function again multiple times
        local j = 1
        while j < sLen do
            local i = j
            j = j + bThreshhold
            j = s:find(" ",j) or j
            local tmpString = s:sub(i,j)
            tmpString = tmpString:gsub("^%s*(.-)%s*$", "%1") -- trim the spaces off the start and end
            printBar(tmpString,c)
        end
    else
        printBar(s,c)
    end
end

Re: function to easily align text for display (actually, 4 n

Posted: Thu Feb 13, 2014 1:59 am
by icester
is this broke? Ive tested them all and I cant seem to get any working :/

Re: function to easily align text for display (actually, 4 n

Posted: Thu Feb 13, 2014 7:13 am
by Oneymus
icester: errors. Are you getting any? What are they? Help us help you.

Re: function to easily align text for display (actually, 4 n

Posted: Tue Jun 03, 2014 7:50 pm
by Akaya
Great script, demonnic. Very helpful. Just what I was looking for!

Re: function to easily align text for display (actually, 4 n

Posted: Wed Jun 04, 2014 3:55 pm
by demonnic
Happy to help =)

I remember the night I wrote this... and I need to go back and make sure I fixed it up to respect the x/d/cecho tags.

Re: function to easily align text for display (actually, 4 n

Posted: Thu Jun 05, 2014 6:40 am
by demonnic
Newly fixed up to properly calculate the string length when using color tags in the text for a [c|d|h]align() function. Slightly embarrassed I didn't catch that much much sooner, and more so it took me so long to post the fix.

Re: function to easily align text for display (actually, 4 n

Posted: Thu Jun 05, 2014 9:54 pm
by demonnic
New version pushed to github. Fixes issues with the color_names being used instead of colorNames.

Re: function to easily align text for display (actually, 4 n

Posted: Fri Aug 01, 2014 3:27 am
by reyl
sorry to revive such an ancient thread, but I have a question...

If I wanted to use this as part of a script, one kinda aimed towards novices to show them mudlet can be helpful and pretty, how could I go about it? Both from the perspective of easy 'installation' and also giving credit where credit is due. I don't really want to have to direct people to go to github, find the latest code, copy it into a new script and name that script "align" for safe measure. Is there a better way others would do it? Could I add it as a file within my package, with information/links to the github, BUT somehow have that not cause conflict if they already have a version of it (such as in the combined 'demonnic' pack for lusternia)