[help] console table

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

[help] console table

Post by icesteruk »

Code: [show] | [select all] lua
GUI.misc.Cachewindow:echo("\n CACHE\n\n")

name_align = 14
num_align = 10

local mycache = {}
	
	for k,v in pairs(precache) do
name_len = (name_align - string.len(k))
if table.isMember(inkcache, k) then
			table.insert(mycache, k .. " "..string.rep(" ", name_len) .."[<pink>" .. v .. "<white>]")
elseif table.isMember(incache, k) then
if v < 500 then
			table.insert(mycache, k .. " "..string.rep(" ", name_len) .."[<red>" .. v.. "<white>]")
else
			table.insert(mycache, k .. " "..string.rep(" ", name_len) .."[<green>" .. v .. "<white>]")
	end
end
end
	for i=1,#mycache,1 do
GUI.misc.Cachewindow:cecho("<white> " .. mycache[i])
GUI.misc.Cachewindow:echo("\n ")
GUI.misc.Cachewindow:echo(string.rep(" ", (13-#mycache[i])))


end
end
is the code I'm using for the picture... How would I change it soo, instead of straight down, it sides neatly side by side? maybe 2/3 width so it like

moss [100] bloodroot [100] goldenseal [100]
kelp [10]

etc

I been trying to change this for months but had no luck and unsure where to find the correct information
Attachments
mudletview.png
mudletview.png (8.28 KiB) Viewed 3853 times

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

Re: [help] console table

Post by Jor'Mox »

Well, I'm not 100% sure what you are trying to achieve here, but string.format should dramatically simplify all of your formatting code for this, and likely make whatever you are trying to do much easier.

string.format("%s %s", str1, str2) -- this would. Just put the two strings side by side.
string.format("%-15s %s", str1, str2) -- this would put the first string in a 15 character wide space, followed by the second one.

Look up examples of how to use printf (in C) to see how to adjust the formatting string.

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

Re: [help] console table

Post by icesteruk »

Im just trying to achieve something so its not all one sided and is better formatted, I'll look into string.format :) thanks :)

edit: Happen to know a site where I can read on string.format as seems wiki doesnt have nothing :/

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

Re: [help] console table

Post by Jor'Mox »

Like I said, look for something on printf, not string.format. All the lua stuff about it just says it is like printf. Though, I seem to recall there is some major difference in that printf lets you pass number arguments to set width, where we have to have the numbers in the formatting string.

So long as you are only working with strings, you really only need to know a few things.
%s represents a string that you have to pass as an argument.
A number between the % and the s sets the width of the space that string will be put in (it is a minimum width, so %5s that gets "Lots of stuff" is going to show the whole string, not just 5 characters of it. Basically it does space padding for you.
A - before a number means that the text will be left aligned in that space instead of right aligned.

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

Re: [help] console table

Post by icesteruk »

Ok I get that is better but my main issue really is HOW would I add it to my code lol... :/

I'll look for other peoples free examples i guess

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

Re: [help] console table

Post by Jor'Mox »

You have things like this:

table.insert(mycache, k .. " "..string.rep(" ", name_len) .."[<red>" .. v.. "<white>]")

Which you can replace with:

table.insert(mycache, string.format("%-"..name_len.."s [<red>%s<white>]",k,v))

Those two lines do exactly the same thing (I think), but the format version lets you more easily lay things out.

Drevarr
Posts: 43
Joined: Tue Aug 06, 2013 12:07 am
Location: GA, USA

Re: [help] console table

Post by Drevarr »

I found the code for the showColors() function useful for grasping dynamic column output.

Post Reply