Jump to content




Print coloured text easily


11 replies to this topic

#1 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 03 April 2013 - 02: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?

#2 spyman68

  • Members
  • 155 posts

Posted 03 April 2013 - 02:48 AM

How the heck did you make this?

#3 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 03 April 2013 - 02:53 AM

Finally. Now this was worth the reading. +1

#4 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 03 April 2013 - 05:41 AM

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


#5 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 03 April 2013 - 05:46 AM

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.

#6 alakazard12

  • Banned
  • 93 posts

Posted 03 April 2013 - 01: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.

#7 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 03 April 2013 - 02: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.

#8 ElvishJerricco

  • Members
  • 803 posts

Posted 03 April 2013 - 04: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"

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 April 2013 - 04:28 PM

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



#10 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 03 April 2013 - 06: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'.

#11 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 03 April 2013 - 08:11 PM

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.

#12 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 06 April 2013 - 03:21 PM

Nice! Much easier way to print colors!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users