Print coloured text easily
Mads 03 Apr 2013
I just created this function for fun, but it actually turned out to come in very handy. So, here it is:
You can do something like this:
and it would print this:
Hello, World!
Pretty awesome, eh?
local function printWithFormat(...) local s = "&1" for k, v in ipairs(arg) do s = s .. v end s = s .. "&0" local fields = {} local lastcolor, lastpos = "0", 0 for pos, clr in s:gmatch"()&(%x)" do table.insert(fields, {s:sub(lastpos + 2, pos - 1), lastcolor}) lastcolor, lastpos = clr , pos end for i = 2, #fields do term.setTextColor(2 ^ (tonumber(fields[i][2], 16))) io.write(fields[i][1]) end end
You can do something like this:
printWithFormat("&7Hello, &aWorld!")
and it would print this:
Hello, World!
Pretty awesome, eh?
nutcase84 03 Apr 2013
I don't see a very big use for this... you can just do
term.setTextColor(colors.COLOR) print(TEXT)
LBPHacker 03 Apr 2013
nutcase84, on 03 April 2013 - 05:41 AM, said:
I don't see a very big use for this... you can just do
term.setTextColor(colors.COLOR) print(TEXT)
It's not about usefulness (though it's pretty useful) - it's about awesomeness. The point of the whole color thingie with the "&" prefix is that eg. in a chat program in CC, you can use color coding just like in the real Minecraft chat with some plugins.
But anyways, I respect your opinion. You don't have to like it.
alakazard12 03 Apr 2013
First, this is an API, but you posted in programs.
Second, this actually could come in handy. I never thought about making something like this.
Second, this actually could come in handy. I never thought about making something like this.
oeed 03 Apr 2013
This is pretty neat.
One thing that would be cool, to extend the 'WithFormat' aspect you could include an Objective-C type stringWithFormat.
For example, if you have a string and you want to insert a string with in it you can do something like this:
The equivalent in Lua would look something like:
Just an idea, it would extend the function by quite a bit.
One thing that would be cool, to extend the 'WithFormat' aspect you could include an Objective-C type stringWithFormat.
For example, if you have a string and you want to insert a string with in it you can do something like this:
//Outputs 'The time is 8 o'clock' [NSString stringWithFormat: @"The time is %@", @"8 o'clock"];
The equivalent in Lua would look something like:
stringWithFormat("The time is %@", "8 o'clock")
Just an idea, it would extend the function by quite a bit.
ElvishJerricco 03 Apr 2013
Yea this could be a bit like printf in C. printWithFormat("Text &aColored Text; %d is a number and will cause an error if it's not, while \"%s\" is a string", 3, "test")
Would print
"Text Colored Text; 3 is a number and will cause an error if it's not, while "test" is a string"
Would print
"Text Colored Text; 3 is a number and will cause an error if it's not, while "test" is a string"
theoriginalbit 03 Apr 2013
Nice...
Did you get the idea from this reading what I was saying in that Tutorial??
So you mean string.format then?
Did you get the idea from this reading what I was saying in that Tutorial??
oeed, on 03 April 2013 - 02:23 PM, said:
One thing that would be cool, to extend the 'WithFormat' aspect you could include an Objective-C type stringWithFormat.
-snip-
The equivalent in Lua would look something like:
-snip-
The equivalent in Lua would look something like:
stringWithFormat("The time is %@", "8 o'clock")
> = string.format("%s %q", "Hello", "Lua user!") -- string and quoted string Hello "Lua user!" > = string.format("%c%c%c", 76,117,97) -- char Lua > = string.format("%e, %E", math.pi,math.pi) -- exponent 3.141593e+000, 3.141593E+000 > = string.format("%f, %g", math.pi,math.pi) -- float and compact float 3.141593, 3.14159 > = string.format("%d, %i, %u", -100,-100,-100) -- signed, signed, unsigned integer -100, -100, 4294967196 > = string.format("%o, %x, %X", -100,-100,-100) -- octal, hex, hex 37777777634, ffffff9c, FFFFFF9C
oeed 03 Apr 2013
Yea, but you don't really need to have all the different variable types. If I was going to do it (I'm thinking of making this, would be useful) I'd just use one symbol.
And yea, it would be string.format if you are doing it 'correctly'.
And yea, it would be string.format if you are doing it 'correctly'.