Formating and Alignment issues

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

Formating and Alignment issues

Post 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 ::??
Code: [show] | [select all] lua
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! :)
Last edited by icester on Thu Feb 13, 2014 2:23 am, edited 1 time in total.

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

Re: Formating and Alignment issues

Post by icester »

sorry for the bump, but can no one at all help me with this?? :/

Jor'Mox
Posts: 1148
Joined: Wed Apr 03, 2013 2:19 am

Re: Formating and Alignment issues

Post 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.

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

Re: Formating and Alignment issues

Post by Oneymus »

Try something like this...
Code: [show] | [select all] lua
-- 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.

User avatar
Belgarath
Posts: 232
Joined: Fri Jul 26, 2013 7:19 am
Discord: macjabeth#7149

Re: Formating and Alignment issues

Post by Belgarath »

Here's a lil snippet I tested in my linux terminal.
Code: [show] | [select all] lua
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.

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

Re: Formating and Alignment issues

Post by icesteruk »

Oneymus wrote:Try something like this...
Code: [show] | [select all] lua
-- 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

Jor'Mox
Posts: 1148
Joined: Wed Apr 03, 2013 2:19 am

Re: Formating and Alignment issues

Post by Jor'Mox »

It should be string.format("%-11s::", "Perception")

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

Re: Formating and Alignment issues

Post by Oneymus »

Oh, my bad! LoL Wrote this right before bed, in the browser.

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

Re: Formating and Alignment issues

Post by icesteruk »

Well, it all works besides that part :P I just removed the -11 number ..

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

Re: Formating and Alignment issues

Post 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":

Code: Select all

Perception::
Herb::
Elixir::
Instead, what you want is (based off your original output):

Code: Select all

Perception ::
Herb       ::
Elixir     ::
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", ...
Code: [show] | [select all] lua
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:
Code: [show] | [select all] lua
echoDefenses("Perception")
echoDefenses("Herb")
Even better, make a table with your defenseCategories (this information already exists in system.defenses) and loop through it.
Code: [show] | [select all] lua
local defenseCategories = {
  "Perception",
  "Herb",
  "Elixir",
  ...
}

for _, defenseCategory in ipairs(defenseCategories) do
  echoDefenses(defenseCategory)
end
Good luck!

Post Reply