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

Printf - Print a colorful string without term.setTextColour!

Started by Creator, 27 October 2015 - 03:12 PM
Creator #1
Posted 27 October 2015 - 04:12 PM
–printf–



(Isn't it a sexy name?)



Printf is a function that allows you to print a formatted print. This formatting lets you change the color of the string in the middle of it. That's it.

Normally, it the function detects "\027" in the string, it analyzes the next two characters. If they are special, it changes the colors accordingly. Then it analyzes the 3rd and the 4th and changes the color accordingly. If you want to have the charcter "-" or "+" to be printed and it is 3 positions after "\027" then put "@" between the 2nd character after "\027" and the special character.

You can load it as an API, however, I do recommend you just copy the code into your own. Give credit! Printf does not go to the next line at the end of the string!

Usage:

Colors codes:

				["0"] = colors.white,
				["1"] = colors.orange,
				["2"] = colors.magenta,
				["3"] = colors.lightBlue,
				["4"] = colors.yellow,
				["5"] = colors.lime,
				["6"] = colors.pink,
				["7"] = colors.gray,
				["8"] = colors.lightGray,
				["9"] = colors.cyan,
				["a"] = colors.purple,
				["b"] = colors.blue,
				["c"] = colors.brown,
				["d"] = colors.green,
				["e"] = colors.red,
				["f"] = colors.black,

Text color and background color:

"+" = setTextColor
"-" = setBackgroundColor

Escape character:
"\027" = starts color magic
"@" = stops color magic before the fourth symbol

Wanna download?

pastebin get STwcktKR printf

Screenies:
Edited on 27 October 2015 - 03:14 PM
Creator #2
Posted 27 October 2015 - 06:43 PM
Do you have any suggestions?
SquidDev #3
Posted 27 October 2015 - 07:27 PM
Do you have any suggestions?

The \27 reminds me of ansi escape codes. Adding support for them would be pretty useful. Also, supporting 'actual printf' commands would be pretty nice, though just passing it through string.format would work too.
Creator #4
Posted 27 October 2015 - 07:34 PM
\027 actually is the escape char in the ascii table.
Bomb Bloke #5
Posted 27 October 2015 - 11:26 PM
In your screenshot, your use of "@" to "stop the colour magic" results in "@" actually appearing on the screen. Is that intended?

You could get better execution speeds by caching all the characters in between each colour change, then term.write()'ing them with a single function call.

Did you consider having this operate like term.blit()?
Creator #6
Posted 28 October 2015 - 05:14 AM
Thanks for the suggestions. Since they seen reasonable, I will implement them. And thanks for the heads up about @.
Creator #7
Posted 28 October 2015 - 04:30 PM
I fixed both of the complaints/suggestions by BombBloke. However, I did this directly in the pastebin, so I can't guarantee the code is not faulty. If you encounter any issues, post it here and I will fix it first thing I open the topic.
Creator #8
Posted 07 November 2015 - 08:14 PM
Hi, is nobody interested in this? If you are, just give me some feedback.
Konlab #9
Posted 07 November 2015 - 08:28 PM
If nobody gives feedback it means that what you made is perfect, it has no bugs (atleast nobody found one) and has no missing features.
I am now breaking this perfectness of this things because I have some suggestions:
Printf() should support different aligns
Readf() - universal reading function supporting aligning, default text(a text that appears right after calling) and coloring (with function for it passed as param or idk how)
Edited on 07 November 2015 - 07:29 PM
Creator #10
Posted 07 November 2015 - 08:53 PM
If nobody gives feedback it means that what you made is perfect, it has no bugs (atleast nobody found one) and has no missing features.
I am now breaking this perfectness of this things because I have some suggestions:
Printf() should support different aligns
Readf() - universal reading function supporting aligning, default text(a text that appears right after calling) and coloring (with function for it passed as param or idk how)

Readf exactly describes this.

And thanks for the compliment.