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: "&<color-code>"
To set background color: "$<color-code>"
To print "$" sign: Normal string: "\\$", Square bracket string: [[\$]]
To print "&" sign: Normal string: "\\&", Square bracket string: [[\&]]
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
- 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) == "&" then
write("&")
i = i + 1
elseif text:sub(i+1, i+1) == "$" then
write("$")
i = i + 1
else
write(c)
end
elseif c == "&" 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&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff$f&f $00$11$22$33
$1&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff$f&f $44$55$66$77
$2&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff$f&f $88$99$aa$bb$f &0 <-- All codes (&716&0)
$3&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff$f&f $cc$dd$ee$f&7f
$4&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$5&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff$f&0 Text color: \&<code>
$6&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff$f&0 Background color: \$<code>
$7&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$8&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$9&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$a&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$b&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff$f&0 <-- All combinations (&7256&0)
$c&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$d&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$e&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$f&00&11&22&33&44&55&66&77&88&99&aa&bb&cc&dd&ee&ff
$f&0\\& = \&, \\$ = \$ $f&0+-- Animation with cPrint (&710 FPS&0)
&7Press &eQ &7to exit. $f&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).."&f cPrint by Jesusthekiller ")
sleep(0.1)
end
end
end
parallel.waitForAny(wait, cols)
License:
Spoiler
THIS 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.