cecho/echo Alignment

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: cecho/echo Alignment

Post by icesteruk »

Ok Ive taken a screen pic as 'center' points to the left and 'right' does center.. Am I really missing someting..
Code: [show] | [select all] lua
cecho(calign("[ OUTC: plantname ("..total_left..") ]", {alignment="right"}))


cecho(calign("[ OUTC: plantname ("..total_left..") ]", {alignment="center"}))

are the codes Im using I feel so dumb :/
Attachments
outcing.png
outcing.png (17.01 KiB) Viewed 5108 times

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

Re: cecho/echo Alignment

Post by demonnic »

I used this:
Code: [show] | [select all] lua
/cecho("\n"..calign("[ OUTC: plantname ("..total_left..") ]", {alignment="left", spacer = "="}).."\n")
/cecho("\n"..calign("[ OUTC: plantname ("..total_left..") ]", {alignment="center", spacer = "="}).."\n")
/cecho("\n"..calign("[ OUTC: plantname ("..total_left..") ]", {alignment="right", spacer = "="}).."\n")
To produce the attached screenshot. I added the spacer = "=" part in order to better show what the formatter is doing. Hard to count empty spaces =)

I added the \n at the beginning, because it's entirely possible it's formatting it properly, but mudlet is wrapping it from the end of your prompt and not from the left side of the screen. The \n makes sure the echo occurs at the start of its own line. I added the one at the end so the commands I sent could be seen more easily above the output.

If that code does not produce those results, then my suggestion would be to go back to the first post for the align() scripts and recopy the code. I just made a very minor change but it wouldn't effect this situation (it only occurs if you have color codes inside the "[ OUTC:..." stuff) but it's possible something happened to the copy you have in your scripts, and if you replace it with a fresh copy it will fix you up.

In fact, if you want to use color codes inside the text itself, you may want to consider just grabbing the code fresh anyway =)
Attachments
alignments.PNG

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: cecho/echo Alignment

Post by icesteruk »

Thanks so much dude, Your still the best :D Working now. But I have noticed.

IF I put <blue> or any color within the "<blue>[ .. it counts them 6 things as letters on the count space thingy ... is that meant to happen?

I did a fresh copy of Align() also just a moment ago

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

Re: cecho/echo Alignment

Post by demonnic »

The new copy should account for that, I tested last night. If not post up the code you have and I'll see what's different :)

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: cecho/echo Alignment

Post by icesteruk »

I grabbed the new code as I deleted the old stuff..
Code: [show] | [select all] lua
cecho("\n"..calign("<sandy_brown>[ OUTC: "..plant.." ("..total_left..") ]", {alignment="right", spacer = " "}).."\n")
the Align()
Code: [show] | [select all] lua


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+\>","")
  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+\>","")
  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+\>","")
  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
 


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

Re: cecho/echo Alignment

Post by demonnic »

ok, I see the issue. I'd forgotten we had color codes with _ in the name. I'll need to post a fix to that in a bit, but in the meantime if you use one of the camelCase colors it should work fine.

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: cecho/echo Alignment

Post by icesteruk »

Thanks dude, Your awesome per :P

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

Re: cecho/echo Alignment

Post by demonnic »

ok icesteruk, repull the code and you should be able to use your sandy_brown again =) I updated it in the post, and on github... but I don't think I'll update the post from here out. it's a pain.

icesteruk
Posts: 287
Joined: Sun Jan 20, 2013 9:16 pm

Re: cecho/echo Alignment

Post by icesteruk »

thanks :)

Post Reply