This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mads's profile picture

Print coloured text easily

Started by Mads, 03 April 2013 - 01:38 AM
Mads #1
Posted 03 April 2013 - 03:38 AM
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?
spyman68 #2
Posted 03 April 2013 - 03:48 AM
How the heck did you make this?
LBPHacker #3
Posted 03 April 2013 - 03:53 AM
Finally. Now this was worth the reading. +1
nutcase84 #4
Posted 03 April 2013 - 06:41 AM
I don't see a very big use for this… you can just do

term.setTextColor(colors.COLOR)
print(TEXT)
LBPHacker #5
Posted 03 April 2013 - 06:46 AM
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 #6
Posted 03 April 2013 - 02:22 PM
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.
oeed #7
Posted 03 April 2013 - 03:23 PM
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.
ElvishJerricco #8
Posted 03 April 2013 - 05:16 PM
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"
theoriginalbit #9
Posted 03 April 2013 - 05:28 PM
Nice… :)/>

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



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

oeed #10
Posted 03 April 2013 - 07:22 PM
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'.
Mads #11
Posted 03 April 2013 - 09:11 PM
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.
Dlcruz129 #12
Posted 06 April 2013 - 04:21 PM
Nice! Much easier way to print colors!