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

cPrint - Color Codes!

Started by jesusthekiller, 19 April 2013 - 06:28 AM
jesusthekiller #1
Posted 19 April 2013 - 08:28 AM


Spoiler

cPrint

No more term.setTextColor()!


What is it?
cPrint is an API which was originally created as past of uGUI for my OS, but it is powerful enough to be an standalone tool.

It allows to change text and background colors inside of a string - like Minecraft's color codes!

How to use it?

Color coding:
To set text color: "&amp;<color-code>"
To set background color: "$<color-code>"
To print "$" sign: Normal string: "\\$", Square bracket string: [[\$]]
To print "&amp;" sign: Normal string: "\\&amp;", Square bracket string: [[\&amp;]]

Functions:
cprint.cprint(string)
Prints color parsed string.
Returns: nothing

cprint.toCode(int)
Changes int to it's string-hexadecimal form.
Returns: string

cprint.cCode(string)
Turns color code to color
Returns: int

cprint.clear([bool])
Clears terminal and if bool is not equal to false (or unset) sets cursor pos to 1, 1

cprint.sb(int)
Wrapper for term.setBackgroundColor()

cprint.st(int)
Wrapper for term.setTextColor()

cprint.sc(int, int)
Wrapper for term.setCursorPos()

Colors:


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


@up If you can't read it, it means you probably should look at the screenshot below :P/>

Screenshots:

From demo app (palette):


Download:
Pastebin:
  • cPrint: pastebin get 2sxYu2Mq cprint
  • Palette (demo app): pastebin get Q6vtbpf6 palette
Code:
  • cPrint:
Spoiler
function sc(x, y)
	term.setCursorPos(x, y)
end
function clear(move)
	term.clear()
	if move ~= false then sc(1,1) end
end
function sb(color)
	term.setBackgroundColor(color)
end
function st(color)
	term.setTextColor(color)
end
function cCode(code)
	if code == "0" then return colors.white end
	if code == "1" then return colors.orange end
	if code == "2" then return colors.magenta end
	if code == "3" then return colors.lightBlue end
	if code == "4" then return colors.yellow end
	if code == "5" then return colors.lime end
	if code == "6" then return colors.pink end
	if code == "7" then return colors.gray  end
	if code == "8" then return colors.lightGray end
	if code == "9" then return colors.cyan end
	if code == "a" then return colors.purple end
	if code == "b" then return colors.blue end
	if code == "c" then return colors.brown end
	if code == "d" then return colors.green end
	if code == "e" then return colors.red end
	if code == "f" then return colors.black end
return colors.white;  -- Safe fuse
end
function toCode(n)
n = n % 16
if n < 10  then return n
elseif n == 10 then return "a"
elseif n == 11 then return "b"
elseif n == 12 then return "c"
elseif n == 13 then return "d"
elseif n == 14 then return "e"
elseif n == 15 then return "f"
end
end
function cprint(text)
local i = 0
	while true  do
  i = i + 1
  if i > #text then break end

		local c = text:sub(i, i)
  if c == "\\" then
			if text:sub(i+1, i+1) == "&amp;" then
				write("&amp;")
				i = i + 1
			elseif text:sub(i+1, i+1) == "$" then
				write("$")
				i = i + 1
   else
	write(c)
			end
		elseif c == "&amp;" then
			st(cCode(text:sub(i+1, i+1)))
			i = i + 1
		elseif c == "$" then
			sb(cCode(text:sub(i+1, i+1)))
			i = i + 1
		else
			write(c)
		end
	end
end
  • Palette (demo app)
Spoiler
os.unloadAPI('cprint')
os.loadAPI('cprint')
cprint.clear()
cprint.cprint([[
$0&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff$f&amp;f	 $00$11$22$33
$1&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff$f&amp;f	 $44$55$66$77
$2&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff$f&amp;f	 $88$99$aa$bb$f   &amp;0 <-- All codes (&amp;716&amp;0)
$3&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff$f&amp;f	 $cc$dd$ee$f&amp;7f
$4&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$5&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff$f&amp;0	 Text color:	   \&amp;<code>
$6&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff$f&amp;0	 Background color: \$<code>
$7&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$8&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$9&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$a&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$b&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff$f&amp;0	 <-- All combinations (&amp;7256&amp;0)
$c&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$d&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$e&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$f&amp;00&amp;11&amp;22&amp;33&amp;44&amp;55&amp;66&amp;77&amp;88&amp;99&amp;aa&amp;bb&amp;cc&amp;dd&amp;ee&amp;ff
$f&amp;0\\&amp; = \&amp;, \\$ = \$   $f&amp;0+-- Animation with cPrint (&amp;710 FPS&amp;0)
&amp;7Press &amp;eQ &amp;7to exit. $f&amp;0V
]])
local function wait()
local e, k = nil
while k ~= "q" do
  e, k = os.pullEvent("char")
end
end
local function cols()
local x, y = term.getSize()
while true do
  for i = 0, 14 do
   cprint.sc(1, y)
   cprint.cprint("$"..cprint.toCode(i).."&amp;f			  cPrint by Jesusthekiller			 ")
   sleep(0.1)
  end
end
end
parallel.waitForAny(wait, cols)

License:
SpoilerTHIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. Copy of this license.
theoriginalbit #2
Posted 19 April 2013 - 05:17 PM
Looks very nice! I have just 2 improvements…

here is an improvement to your cCode function

function cCode(h)
  return 2 ^ tonumber(h, 16)
end

and your toCode, could become

function toCode(n)
  return string.format('%x', n)
end
jesusthekiller #3
Posted 19 April 2013 - 10:31 PM
I'll improve it when I'll get home :)/>
jesusthekiller #4
Posted 21 April 2013 - 04:56 AM
Version 1.2 is out!