function to easily align text for display (actually, 4 now)

User avatar
Omni
Posts: 131
Joined: Fri Feb 12, 2010 10:26 am

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

Post by Omni » Sat Feb 12, 2011 12:21 am

As I said in the feature request, would love if these became natural mudlet functions.

User avatar
Vadi
Posts: 4728
Joined: Sat Mar 14, 2009 3:13 pm

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

Post by Vadi » Tue Sep 20, 2011 11:06 pm

Hm does calign respect the tags? When I use cecho(calign(...)), it's off the center.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

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

Post by tsuujin » Tue Sep 20, 2011 11:27 pm

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

icester
Posts: 17
Joined: Tue Jun 08, 2010 10:43 am

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

Post by icester » Thu Feb 13, 2014 1:59 am

is this broke? Ive tested them all and I cant seem to get any working :/

User avatar
Oneymus
Posts: 321
Joined: Thu Sep 17, 2009 5:24 am

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

Post by Oneymus » Thu Feb 13, 2014 7:13 am

icester: errors. Are you getting any? What are they? Help us help you.

User avatar
Akaya
Posts: 412
Joined: Thu Apr 19, 2012 1:36 am

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

Post by Akaya » Tue Jun 03, 2014 7:50 pm

Great script, demonnic. Very helpful. Just what I was looking for!

User avatar
demonnic
Posts: 576
Joined: Sat Dec 05, 2009 3:19 pm

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

Post by demonnic » Wed Jun 04, 2014 3:55 pm

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.

User avatar
demonnic
Posts: 576
Joined: Sat Dec 05, 2009 3:19 pm

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

Post by demonnic » Thu Jun 05, 2014 6:40 am

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.

User avatar
demonnic
Posts: 576
Joined: Sat Dec 05, 2009 3:19 pm

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

Post by demonnic » Thu Jun 05, 2014 9:54 pm

New version pushed to github. Fixes issues with the color_names being used instead of colorNames.

reyl
Posts: 12
Joined: Wed Jul 10, 2013 5:33 pm

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

Post by reyl » Fri Aug 01, 2014 3:27 am

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)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests