←  Programs

ComputerCraft | Programmable Computers for Minecraft

»

Print coloured text easily

Mads's Photo 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:
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?
Quote

spyman68's Photo spyman68 03 Apr 2013

How the heck did you make this?
Quote

LBPHacker's Photo LBPHacker 03 Apr 2013

Finally. Now this was worth the reading. +1
Quote

nutcase84's Photo nutcase84 03 Apr 2013

I don't see a very big use for this... you can just do
term.setTextColor(colors.COLOR)
print(TEXT)
Quote

LBPHacker's Photo LBPHacker 03 Apr 2013

View Postnutcase84, 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.
Quote

alakazard12's Photo 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.
Quote

oeed's Photo 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:
//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.
Quote

ElvishJerricco's Photo 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"
Quote

theoriginalbit's Photo theoriginalbit 03 Apr 2013

Nice... :)

Did you get the idea from this reading what I was saying in that Tutorial??



View Postoeed, 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:
stringWithFormat("The time is %@", "8 o'clock")
So you mean string.format then? :P

> = 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

Quote

oeed's Photo 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'.
Quote

Mads's Photo Mads 03 Apr 2013

View Postalakazard12, on 03 April 2013 - 01:22 PM, said:

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.

This is not an API at all. It might be more of a utility, but meh.
Quote

Dlcruz129's Photo Dlcruz129 06 Apr 2013

Nice! Much easier way to print colors!
Quote