Page 1 of 2
Formating and Alignment issues
Posted: Wed Feb 12, 2014 3:30 am
by icester
I have two issues so instead of two posts I'll try ask them both into this one..
at momnt I have
http://postimg.org/image/o5kud15fx/
Which you can see -
Perception :: [telesense] [nightsight] [hypersight] [alertness] [treewatch] [landsense]
[skywatch] [deathsight] [vigiliance] [warning]
the skywatch falls straight under perception, How would I make it be below the two ::??
cecho("<grey>Perception" .. string.rep(" ", 1 - string.len(tostring(nclass))).. " :: ")
for v, i in pairs(system.defenses.perception) do
local color = (dabomb.defenses[v] and "green" or "red")
-- cecho("<grey>[<" .. color .. ">" .. v .. "<grey>] ")
cechoLink("<grey>[<"..color..">"..v.."<grey>] " .. [[]], [[send("]] .. i.action .. [[")]], "Use Skill", true)
end
echo("\n")
Any help on either will be greatfully appreciated, the first one is the main one I need help for the 2nd one I just used to it by now!

Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 2:23 am
by icester
sorry for the bump, but can no one at all help me with this?? :/
Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 3:35 am
by Jor'Mox
Just from looking at your code, you are letting Mudlet word wrap it for you, which is the problem. If you wrap it yourself (put in new lines "\n" where needed), then adding in leading white space characters to achieve a polished look is easy. Trying to guess where it will be automatically wrapped, and add in spaces to account for that will generally not work, because most word wrap algorithms remove leading and trailing white space when the wrap is done, so you would need some visible character exactly at the break (the first character of the new line) before any extra spaces you put in.
Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 7:08 am
by Oneymus
Try something like this...
-- Scoped to simulate a function, where locals would be local.
do
-- 11 is based off a count given your image. This could be calculated.
local category = string.format("-11%s::", "Perception")
cecho(string.format("<grey>%s", category)
local firstLine = true
local count = 0
-- When iterating through a dictionary like this, the usual variables are
-- k(ey) and v(alue). I named them otherwise to make it clearer.
for defenseName, defense in pairs(system.defenses.perception) do
if not firstLine and count == 0 then
echo(string.rep(" ", string.len(category)))
end
local color = (dabomb.defenses[defenseName] and "green" or "red")
local output = string.format("<grey>%s%s<grey>", color, defenseName)
cechoLink(output, [[send("]] .. defense.action .. [[")]], "Use Skill", true)
count = count + 1
if count < 4 then
echo(" ")
else
echo("\n")
count = 0
if firstLine then
firstLine = false
end
end
end
-- This may result in double newlines. Can be handled with a little bit of line parsing.
echo("\n")
end
Disclaimer: I wrote this in the browser. Should be pretty straightforward. The gist of this snippet is to use the length of the category to indent the next line. I arbitrarily chose to show four defenses per line. You could fiddle with this, or you could turn them into real columns of defined length using string.format. This won't be perfect, but it should get you heading in the right direction.
Also, I envisioned that as a function where you would pass in "Perception" or whatever, so that should be parameterized.
Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 1:29 pm
by Belgarath
Here's a lil snippet I tested in my linux terminal.
local defences = {
"telesense", "nightsight", "hypersight", "alertness",
"treewatch", "landsense", "skywatch", "deathsight",
"vigilance", "warning"
}
io.write(string.format("%-11s:: ", "Perception"))
local i = 1
for _,def in ipairs(defences) do
if i % 5 ~= 0 then
io.write(string.format("%-15s", "[" .. def .. "]"))
else
io.write(string.format("%s\n", "[" .. def .. "]"))
io.write(string.rep(" ", 14))
end
i = i + 1
end
Colours and echoLinks could easily be added.
Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 4:53 pm
by icesteruk
Oneymus wrote:Try something like this...
-- Scoped to simulate a function, where locals would be local.
do
-- 11 is based off a count given your image. This could be calculated.
local category = string.format("-11%s::", "Perception")
cecho(string.format("<grey>%s", category)
local firstLine = true
local count = 0
-- When iterating through a dictionary like this, the usual variables are
-- k(ey) and v(alue). I named them otherwise to make it clearer.
for defenseName, defense in pairs(system.defenses.perception) do
if not firstLine and count == 0 then
echo(string.rep(" ", string.len(category)))
end
local color = (dabomb.defenses[defenseName] and "green" or "red")
local output = string.format("<grey>%s%s<grey>", color, defenseName)
cechoLink(output, [[send("]] .. defense.action .. [[")]], "Use Skill", true)
count = count + 1
if count < 4 then
echo(" ")
else
echo("\n")
count = 0
if firstLine then
firstLine = false
end
end
end
-- This may result in double newlines. Can be handled with a little bit of line parsing.
echo("\n")
end
Disclaimer: I wrote this in the browser. Should be pretty straightforward. The gist of this snippet is to use the length of the category to indent the next line. I arbitrarily chose to show four defenses per line. You could fiddle with this, or you could turn them into real columns of defined length using string.format. This won't be perfect, but it should get you heading in the right direction.
Also, I envisioned that as a function where you would pass in "Perception" or whatever, so that should be parameterized.
Thank you this works perfect... only thing that Im unsure on is where it says
local category = string.format("-11%s::", "Perception")
its showing -11Perception::
thank you for your help tho this has been driving me crazy as I've not messed around with the string stuff at all
Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 5:02 pm
by Jor'Mox
It should be string.format("%-11s::", "Perception")
Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 5:04 pm
by Oneymus
Oh, my bad! LoL Wrote this right before bed, in the browser.
Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 5:43 pm
by icesteruk
Well, it all works besides that part

I just removed the -11 number ..
Re: Formating and Alignment issues
Posted: Thu Feb 13, 2014 7:19 pm
by Oneymus
The -11 part keeps all of your ::'s aligned, when you expand this code to cover multiple categories. The "%-11s" tells Lua to create a string 11 characters wide, using whitespace as padding.
This is what it will look like without the "%-11s":
Instead, what you want is (based off your original output):
Because I have some to kill, I'm going to turn this into a reusable function. All you'll need to do is pass in "Perception", "Herb", "Elixir", ...
function echoDefenses (defenseCategory)
local categoryHeader = string.format("%-11s::", defenseCategory)
cecho(string.format("<grey>%s", category)
local firstLine = true
local count = 0
-- When iterating through a dictionary like this, the usual variables are
-- k(ey) and v(alue). I named them otherwise to make it clearer.
for defenseName, defense in pairs(system.defenses[string.lower(defenseCategory)]) do
if not firstLine and count == 0 then
echo(string.rep(" ", string.len(categoryHeader)))
end
local color = (dabomb.defenses[defenseName] and "green" or "red")
local output = string.format("<grey>%s%s<grey>", color, defenseName)
cechoLink(output, [[send("]] .. defense.action .. [[")]], "Use Skill", true)
count = count + 1
if count < 4 then
echo(" ")
else
echo("\n")
count = 0
if firstLine then
firstLine = false
end
end
end
-- This may result in double newlines. Can be handled with a little bit of line parsing.
echo("\n")
end
Note the change to system.defenses.perception; assuming that all follow the same naming scheme (Perception => perception), this is easily parameterized.
All you have to do to use this function is:
echoDefenses("Perception")
echoDefenses("Herb")
Even better, make a table with your defenseCategories (this information already exists in system.defenses) and loop through it.
local defenseCategories = {
"Perception",
"Herb",
"Elixir",
...
}
for _, defenseCategory in ipairs(defenseCategories) do
echoDefenses(defenseCategory)
end
Good luck!