Thanks to hempa for the initial idea. I cleaned it up a bit and made it for more than just centering.
EDIT: this is turned into a full fledged project for me.
EDIT: Newest version. This has now spread into four functions, so that it will format specifically for echo, cecho, decho, and hecho (align, calign, dalign, and halign respectiveiy)
EDIT: Very quick and easy update to fix [c|h|d]align() functions which were erroneously counting the color codes in the string length.
EDIT: Code will be kept up to date at https://github.com/demonnic/mudletalign
EDIT: New code pushed, fixed issues with color_names rather than colorNames being used. Previously forgot about the _ option.
For you, a screenshot: For usage (refer to screenshot):
calign("some string", {table containing options})
your options are as follows (order doesn't matter):
width: How many chars across the total line should be. Defaults to 80. set as width = #
alignment: How you want it aligned. Defaults to "centered", right/light also options. set as alignment = "center"
cap: What you want to use to encapsule the message. Defaults to "", set as cap = "[Blah]"
mirror: Reverse the right cap from the left cap when center? IE turns <{[[ into ]]}> when using the center alignment. Defaults to true, and is ignored when aligning left or right. Set as mirror = false
spacer: The thing you want to fill the extra space for formatting. Defaults to " ", set as spacer = "="
inside: Whether you want the spacer inside the caps, or outside the caps. Defaults to true which produces things like "[Blah]======= some text ========[Blah]" , set as inside = false
*capColor: What color you want the caps to be. Defaults to white, set as capColor = "<green>"
*spacerColor: What color you want your spacer to be. Defaults to whatever white, set as spacerColor = "<purple>"
*textColor: What color you want the text you're formatting to be. Defaults to white, set as textColor = "<red>"
*Note:
I have now included seperate functions. align() now is completely unformatted for colors. calign() is color formatting for the cecho function, dalign() and halign() are similarly biased. The example usage above is entirely correct, just be sure to use the align() function which matches the echo() function you plan to pass it through
For you, an example using all of the options:
calign("some text", {width = "35", alignment = "center", cap = "[Bal]", mirror = false, spacer = "-", inside = false, capColor = "<yellow:red>", spacerColor = "<green:purple>", textColor = "<dark_green:blue>"})
Which would produce something similar (though more eye searing) in concept to the fourth color demo in the screenshot above.
For you, a minimal example:
align("some text")
which will center "some text" minus "" in the middle of an 80 length line using spaces.
To create the screenshot above, create an alias which contains this
cecho("Centered, mirror caps, spacer inside")
cecho(calign("some text", {alignment = "center", width = 30, cap = "{[[", spacer = "=", inside = true, mirror = true}))
cecho("\nCentered, mirror caps, spacer outside")
cecho(calign("some text", {alignment = "center", width = 30, cap ="{[[", spacer = "=", inside = false, mirror = true}))
cecho("\nCentered, no mirroring, spacer inside")
cecho(calign("some text", {alignment = "center", width = 30, cap = "[Bal]", spacer = "=", inside = true, mirror = false}))
cecho("\nCentered, no mirroring, spacer outside")
cecho(calign("some text", {alignment = "center", width = 30, cap = "[Bal]", spacer = "=", inside = false, mirror = false}))
cecho("\nLeft aligned, spacer inside")
cecho(calign("some text", {alignment = "left", width = 30, cap = "|", spacer = ".", inside = true}))
cecho("\nLeft aligned, spacer outside")
cecho(calign("some text", {alignment = "left", width = 30, cap = "|", spacer = "=", inside = false}))
cecho("\nRight aligned, spacer inside")
cecho(calign("some text", {alignment = "right", width = 30, cap = "|", spacer = ".", inside = true}))
cecho("\nRight aligned, spacer outside")
cecho(calign("some text", {alignment = "right", width = 30, cap = "|", spacer = "=", inside = false}))
cecho("\n\nNow with colors!!")
cecho("\nCentered, mirror caps, spacer inside, <green>capColor <white>, <purple>spacerColor <white>, <red>textColor<white>")
cecho(calign("some text", {alignment = "center", width = 30, cap = "{[[", spacer = "=", inside = true, mirror = true, capColor = "<green>", spacerColor = "<purple>", textColor = "<red>"}))
cecho("\nCentered, mirror caps, spacer outside, <green>capColor <white>, <purple>spacerColor <white>, <red>textColor<white>")
cecho(calign("some text", {alignment = "center", width = 30, cap = "{[[", spacer = "=", inside = false, mirror = true, capColor = "<green>", spacerColor = "<purple>", textColor = "<red>"}))
cecho("\nCentered, no mirroring, spacer inside, <green>capColor <white>, <purple>spacerColor <white>, <red>textColor<white>")
cecho(calign("some text", {alignment = "center", width = 30, cap = "[Bal]", spacer = "=", inside = true, mirror = false, capColor = "<green>", spacerColor = "<purple>", textColor = "<red>"}))
cecho("\nCentered, no mirroring, spacer outside, <green>capColor <white>, <purple>spacerColor <white>, <red>textColor<white>")
cecho(calign("some text", {alignment = "center", width = 30, cap = "[Bal]", spacer = "=", inside = false, mirror = false, capColor = "<green>", spacerColor = "<purple>", textColor = "<red>"}))
cecho("\nLeft Aligned, spacer inside, <green>capColor <white>, <purple>spacerColor <white>, <red>textColor<white>")
cecho(calign("some text", {alignment = "left", width = 30, cap = "|", spacer = ".", inside = true, capColor = "<green>", spacerColor = "<purple>", textColor = "<red>"}))
cecho("\nLeft Aligned, spacer outside, <green>capColor <white>, <purple>spacerColor <white>, <red>textColor<white>")
cecho(calign("some text", {alignment = "left", width = 30, cap = "|", spacer = "=", inside = false, capColor = "<green>", spacerColor = "<purple>", textColor = "<red>"}))
cecho("\nRight Aligned, spacer inside, <green>capColor <white>, <purple>spacerColor <white>, <red>textColor<white>")
cecho(calign("some text", {alignment = "right", width = 30, cap = "|", spacer = ".", inside = true, capColor = "<green>", spacerColor = "<purple>", textColor = "<red>"}))
cecho("\nRight Aligned, spacer outside, <green>capColor <white>, <purple>spacerColor <white>, <red>textColor<white>")
cecho(calign("some text", {alignment = "right", width = 30, cap = "|", spacer = "=", inside = false, capColor = "<green>", spacerColor = "<purple>", textColor = "<red>"}))
function align(str,options) --str is a string, options is a table
--[[ If they sent anything but a table as the second argument, return useful
info. But if they didn't send a second argument then that's ok, the defaults
will be enough to get by and just center the txt
]]--
if (type(options) ~= "table") and (options ~= nil) then return "You call this with align(\"some text to format\", <table of options>. Pls check comments for what options and usage information" end
options = options or {} --if they sent options, don't overwrite them
options.width = options.width or 80 --default line length of 80
options.alignment = options.alignment or "center" --if we don't specify, it's centered
options.cap = options.cap or "" --default endcap of nothing (an empty string, technically)
options.spacer = options.spacer or " " --default spacer is.. well.. space
options.inside = options.inside or false --by default, when centering, formation as spacers|cap|text|cap|spacers
if not options.mirror == false then options.mirror = options.mirror or true end--by default, we do want to use mirroring for the caps
local strLen = string.len(str)
local leftCap = options.cap
local rightCap = options.cap
local leftPadLen = math.floor((options.width - strLen)/2,1) - 1
local rightPadLen = leftPadLen + ((options.width - strLen)%2)
local maxPad = 0
local capLen = string.len(options.cap)
if capLen > leftPadLen then --if the cap is bigger than the left total padding
options.cap = options.cap:sub(1, leftPadLen) -- trim it up right!
capLen = string.len(options.cap)
end --otherwise, don't mess with it
if options.alignment == "center" then --we're going to center something
leftPadLen = math.floor((options.width - strLen)/2,1) - 1 --get the padding needed on the left
rightPadLen = leftPadLen + ((options.width - strLen)%2) --and on the right
if options.mirror then --if we're reversing the left cap and the right cap (IE {{[[ turns into ]]}} )
rightCap = string.gsub(rightCap, "<", ">")
rightCap = string.gsub(rightCap, "%[", "%]")
rightCap = string.gsub(rightCap, "{", "}")
rightCap = string.gsub(rightCap, "%(", "%)")
rightCap = string.reverse(rightCap)
end --otherwise, they'll be the same, so don't do anything
str = string.format(" %s ", str)
elseif options.alignment == "right" then --we'll right-align the text
leftPadLen = options.width - strLen - 1
rightPadLen = 0
rightCap = ""
str = string.format(" %s", str)
else --Ok, so if it's not center or right, we assume it's left. We don't do justified. Sorry.
leftPadLen = 0
rightPadLen = options.width - strLen -1
leftCap = ""
str = string.format("%s ", str)
end--that's it, took care of both left, right, and center formattings, now to output the durn thing.
if options.inside then
--if we're placing the repated spacer inside
--"=====endcap some text endcap====="
--"=====endcap some text pacdne====="
--"=================endcap some text"
--"some text endcap================="
return leftCap .. string.rep(options.spacer, (leftPadLen - capLen)) .. str ..string.rep(options.spacer, (rightPadLen - capLen)).. rightCap
else
--otherwise, it''s be the spaces on the 'inside'
-- "endcap===== some text =====endcap"
-- "endcap===== some text =====pacdne"
-- "endcap================= some text"
-- "some text =================endcap"
return string.rep(options.spacer, (leftPadLen - capLen)) .. leftCap .. str .. rightCap .. string.rep(options.spacer, (rightPadLen - capLen))
end
end
function calign(str,options) --str is a string, options is a table
--[[ If they sent anything but a table as the second argument, return useful
info. But if they didn't send a second argument then that's ok, the defaults
will be enough to get by and just center the txt
]]--
if (not type(options) == "table") and (not options == nil) then return "You call this with align(\"some text to format\", <table of options>. Pls check comments for what options and usage information" end
options = options or {} --if they sent options, don't overwrite them
options.width = options.width or 80 --default line length of 80
options.alignment = options.alignment or "center" --if we don't specify, it's centered
options.cap = options.cap or "" --default endcap of nothing (an empty string, technically)
options.spacer = options.spacer or " " --default spacer is.. well.. space
options.inside = options.inside or false --by default, when centering, formation as spacers|cap|text|cap|spacers
options.capColor = options.capColor or "<white>"--by default, don't change the color of the caps
options.spacerColor = options.spacerColor or "<white>"
options.textColor = options.textColor or "<white>"--or the text
if not options.mirror == false then options.mirror = options.mirror or true end--by default, we do want to use mirroring for the caps
local strippedString = str:gsub("<%w*_?%w*:?%w*_?%w*>","")
local strLen = string.len(strippedString)
local leftCap = options.cap
local rightCap = options.cap
local leftPadLen = math.floor((options.width - strLen)/2,1) - 1
local rightPadLen = leftPadLen + ((options.width - strLen)%2)
local maxPad = 0
local capLen = string.len(options.cap)
if capLen > leftPadLen then --if the cap is bigger than the left total padding
options.cap = options.cap:sub(1, leftPadLen) -- trim it up right!
capLen = string.len(options.cap)
end --otherwise, don't mess with it
if options.alignment == "center" then --we're going to center something
leftPadLen = math.floor((options.width - strLen)/2,1) - 1 --get the padding needed on the left
rightPadLen = leftPadLen + ((options.width - strLen)%2) --and on the right
if options.mirror then --if we're reversing the left cap and the right cap (IE {{[[ turns into ]]}} )
rightCap = string.gsub(rightCap, "<", ">")
rightCap = string.gsub(rightCap, "%[", "%]")
rightCap = string.gsub(rightCap, "{", "}")
rightCap = string.gsub(rightCap, "%(", "%)")
rightCap = string.reverse(rightCap)
end --otherwise, they'll be the same, so don't do anything
str = string.format(" %s ", str)
elseif options.alignment == "right" then --we'll right-align the text
leftPadLen = options.width - strLen - 1
rightPadLen = 0
rightCap = ""
str = string.format(" %s", str)
else --Ok, so if it's not center or right, we assume it's left. We don't do justified. Sorry.
leftPadLen = 0
rightPadLen = options.width - strLen -1
leftCap = ""
str = string.format("%s ", str)
end--that's it, took care of both left, right, and center formattings, now to output the durn thing.
if options.inside then
--if we're placing the repated spacer inside
--"=====endcap some text endcap====="
--"=====endcap some text pacdne====="
--"=================endcap some text"
--"some text endcap================="
return options.capColor .. leftCap .. options.spacerColor.. string.rep(options.spacer, (leftPadLen - capLen)) .. options.textColor .. str .. options.spacerColor ..string.rep(options.spacer, (rightPadLen - capLen)) .. options.capColor .. rightCap
else
--otherwise, it''s be the spaces on the 'inside'
-- "endcap===== some text =====endcap"
-- "endcap===== some text =====pacdne"
-- "endcap================= some text"
-- "some text =================endcap"
return options.spacerColor .. string.rep(options.spacer, (leftPadLen - capLen)) .. options.capColor .. leftCap .. options.textColor .. str .. options.capColor .. rightCap .. options.spacerColor .. string.rep(options.spacer, (rightPadLen - capLen))
end
end
function dalign(str,options) --str is a string, options is a table
--[[ If they sent anything but a table as the second argument, return useful
info. But if they didn't send a second argument then that's ok, the defaults
will be enough to get by and just center the txt
]]--
if (not type(options) == "table") and (not options == nil) then return "You call this with align(\"some text to format\", <table of options>. Pls check comments for what options and usage information" end
options = options or {} --if they sent options, don't overwrite them
options.width = options.width or 80 --default line length of 80
options.alignment = options.alignment or "center" --if we don't specify, it's centered
options.cap = options.cap or "" --default endcap of nothing (an empty string, technically)
options.spacer = options.spacer or " " --default spacer is.. well.. space
options.inside = options.inside or false --by default, when centering, formation as spacers|cap|text|cap|spacers
options.capColor = options.capColor or "<255,255,255>"--by default, don't change the color of the caps
options.spacerColor = options.spacerColor or "<255,255,255>"
options.textColor = options.textColor or "<255,255,255>"--or the text
if not options.mirror == false then options.mirror = options.mirror or true end--by default, we do want to use mirroring for the caps
local strippedString = str:gsub("<%w*_?%w*:?%w*_?%w*>","")
local strLen = string.len(strippedString)
local leftCap = options.cap
local rightCap = options.cap
local leftPadLen = math.floor((options.width - strLen)/2,1) - 1
local rightPadLen = leftPadLen + ((options.width - strLen)%2)
local maxPad = 0
local capLen = string.len(options.cap)
if capLen > leftPadLen then --if the cap is bigger than the left total padding
options.cap = options.cap:sub(1, leftPadLen) -- trim it up right!
capLen = string.len(options.cap)
end --otherwise, don't mess with it
if options.alignment == "center" then --we're going to center something
leftPadLen = math.floor((options.width - strLen)/2,1) - 1 --get the padding needed on the left
rightPadLen = leftPadLen + ((options.width - strLen)%2) --and on the right
if options.mirror then --if we're reversing the left cap and the right cap (IE {{[[ turns into ]]}} )
rightCap = string.gsub(rightCap, "<", ">")
rightCap = string.gsub(rightCap, "%[", "%]")
rightCap = string.gsub(rightCap, "{", "}")
rightCap = string.gsub(rightCap, "%(", "%)")
rightCap = string.reverse(rightCap)
end --otherwise, they'll be the same, so don't do anything
str = string.format(" %s ", str)
elseif options.alignment == "right" then --we'll right-align the text
leftPadLen = options.width - strLen - 1
rightPadLen = 0
rightCap = ""
str = string.format(" %s", str)
else --Ok, so if it's not center or right, we assume it's left. We don't do justified. Sorry.
leftPadLen = 0
rightPadLen = options.width - strLen -1
leftCap = ""
str = string.format("%s ", str)
end--that's it, took care of both left, right, and center formattings, now to output the durn thing.
if options.inside then
--if we're placing the repated spacer inside
--"=====endcap some text endcap====="
--"=====endcap some text pacdne====="
--"=================endcap some text"
--"some text endcap================="
return options.capColor .. leftCap .. options.spacerColor.. string.rep(options.spacer, (leftPadLen - capLen)) .. options.textColor .. str .. options.spacerColor ..string.rep(options.spacer, (rightPadLen - capLen)) .. options.capColor .. rightCap
else
--otherwise, it''s be the spaces on the 'inside'
-- "endcap===== some text =====endcap"
-- "endcap===== some text =====pacdne"
-- "endcap================= some text"
-- "some text =================endcap"
return options.spacerColor .. string.rep(options.spacer, (leftPadLen - capLen)) .. options.capColor .. leftCap .. options.textColor .. str .. options.capColor .. rightCap .. options.spacerColor .. string.rep(options.spacer, (rightPadLen - capLen))
end
end
function halign(str,options) --str is a string, options is a table
--[[ If they sent anything but a table as the second argument, return useful
info. But if they didn't send a second argument then that's ok, the defaults
will be enough to get by and just center the txt
]]--
if (not type(options) == "table") and (not options == nil) then return "You call this with align(\"some text to format\", <table of options>. Pls check comments for what options and usage information" end
options = options or {} --if they sent options, don't overwrite them
options.width = options.width or 80 --default line length of 80
options.alignment = options.alignment or "center" --if we don't specify, it's centered
options.cap = options.cap or "" --default endcap of nothing (an empty string, technically)
options.spacer = options.spacer or " " --default spacer is.. well.. space
options.inside = options.inside or false --by default, when centering, formation as spacers|cap|text|cap|spacers
options.capColor = options.capColor or "|cFFFFFF"--by default, don't change the color of the caps
options.spacerColor = options.spacerColor or "|cFFFFFF"
options.textColor = options.textColor or "|cFFFFFF"--or the text
if not options.mirror == false then options.mirror = options.mirror or true end--by default, we do want to use mirroring for the caps
local strippedString = str:gsub("<%w*_?%w*:?%w*_?%w*>","")
local strLen = string.len(strippedString)
local leftCap = options.cap
local rightCap = options.cap
local leftPadLen = math.floor((options.width - strLen)/2,1) - 1
local rightPadLen = leftPadLen + ((options.width - strLen)%2)
local maxPad = 0
local capLen = string.len(options.cap)
if capLen > leftPadLen then --if the cap is bigger than the left total padding
options.cap = options.cap:sub(1, leftPadLen) -- trim it up right!
capLen = string.len(options.cap)
end --otherwise, don't mess with it
if options.alignment == "center" then --we're going to center something
leftPadLen = math.floor((options.width - strLen)/2,1) - 1 --get the padding needed on the left
rightPadLen = leftPadLen + ((options.width - strLen)%2) --and on the right
if options.mirror then --if we're reversing the left cap and the right cap (IE {{[[ turns into ]]}} )
rightCap = string.gsub(rightCap, "<", ">")
rightCap = string.gsub(rightCap, "%[", "%]")
rightCap = string.gsub(rightCap, "{", "}")
rightCap = string.gsub(rightCap, "%(", "%)")
rightCap = string.reverse(rightCap)
end --otherwise, they'll be the same, so don't do anything
str = string.format(" %s ", str)
elseif options.alignment == "right" then --we'll right-align the text
leftPadLen = options.width - strLen - 1
rightPadLen = 0
rightCap = ""
str = string.format(" %s", str)
else --Ok, so if it's not center or right, we assume it's left. We don't do justified. Sorry.
leftPadLen = 0
rightPadLen = options.width - strLen -1
leftCap = ""
str = string.format("%s ", str)
end--that's it, took care of both left, right, and center formattings, now to output the durn thing.
if options.inside then
--if we're placing the repated spacer inside
--"=====endcap some text endcap====="
--"=====endcap some text pacdne====="
--"=================endcap some text"
--"some text endcap================="
return options.capColor .. leftCap .. options.spacerColor.. string.rep(options.spacer, (leftPadLen - capLen)) .. options.textColor .. str .. options.spacerColor ..string.rep(options.spacer, (rightPadLen - capLen)) .. options.capColor .. rightCap
else
--otherwise, it''s be the spaces on the 'inside'
-- "endcap===== some text =====endcap"
-- "endcap===== some text =====pacdne"
-- "endcap================= some text"
-- "some text =================endcap"
return options.spacerColor .. string.rep(options.spacer, (leftPadLen - capLen)) .. options.capColor .. leftCap .. options.textColor .. str .. options.capColor .. rightCap .. options.spacerColor .. string.rep(options.spacer, (rightPadLen - capLen))
end
end