Is it possible to print to the terminal/console in custom colors?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Jasoco
- Inner party member
- Posts: 3725
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Is it possible to print to the terminal/console in custom colors?
As said, when I run my project from the terminal it would be useful if I could print out my debug information in different colors. Is it possible for Löve to do this? Maybe using some codes in the string being printed? It would be so useful for debugging and separating relevant info when you're printing so much stuff at once during debugging.
Re: Is it possible to print to the terminal/console in custom colors?
You can assume an ANSI type terminal and use these:
https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
Code: Select all
local codes = {reset = "\x1B[m", red = "\x1B[31m", green = "\x1B[32m", --[[ rest left as exercise :P ]]}
function printC(colour, ...)
if not codes[colour] then error("Undefined colour: " .. colour) end
io.write(codes[colour])
print(...)
io.write(codes.reset)
end
printC("red", "This text is red", "This one too")
print("This is normal")
printC("reset", "This is normal too")
- Jasoco
- Inner party member
- Posts: 3725
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Is it possible to print to the terminal/console in custom colors?
Awesome. That works perfectly. Will be really useful. Thanks!
Is there a way to set the background color of the text too? The method on the Wiki doesn't seem to work. Well it works, but only for the black text on white background. Is there a limit because playing around doesn't seem to work.
Is there a way to set the background color of the text too? The method on the Wiki doesn't seem to work. Well it works, but only for the black text on white background. Is there a limit because playing around doesn't seem to work.
Re: Is it possible to print to the terminal/console in custom colors?
Will that work on windows?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: Is it possible to print to the terminal/console in custom colors?
It's working fine for me. The only problem I have is that when changing the background colour and scrolling due to printing a newline, the new line's background colour is the current one, so it kind of persists. There are two ways around that.
1) Don't use print() so that we have control over the end of line:
The other, simpler way is to clear to end of line after sending the reset code, to undo the colourization:
The second one is not so robust, in that it does not preserve any text that might be present there in advance, but that's unlikely to be a problem when using printC for logging the way you usually use print.
Finally, I'd recommend you do it for your internal use only, but to remove colours in the final version. First, it won't work in all Windows terminals, as noted above; second, everyone has different FG/BG settings, e.g. my text is wheat on black, and text colours like blue look fine on a white background but are next to unreadable on a black one. OTOH, yellow looks fine in mine but is next to unreadable on a white BG.
1) Don't use print() so that we have control over the end of line:
Code: Select all
local reset = "\x1B[m"
local fgs = {red = "\x1B[31m", green = "\x1B[32m", --[[ rest left as exercise :P ]]}
local bgs = {red = "\x1B[41m", green = "\x1B[42m", --[[ rest left as exercise :P ]]}
function printC(fg,bg, ...)
if fg then
if not fgs[fg] then error("Undefined colour: " .. fg) end
io.write(fgs[fg])
end
if bg then
if not bgs[bg] then error("Undefined colour: " .. bg) end
io.write(bgs[bg])
end
-- mimick print():
for i = 1, select('#', ...) do
if i ~= 1 then io.write('\t') end
io.write(tostring(select(i, ...)))
end
-- reset before sending the newline
if fg or bg then
io.write(reset)
end
io.write('\n')
end
printC("red", "green", "This text is red on green", "This one too")
print("This is normal")
printC(false, false, "This is normal too")
printC("red", false, "This is red text")
printC(false, "green", "This is normal text on green")
Code: Select all
local reset = "\x1B[m\x1B[K" -- adds clear-to-EOL code
local fgs = {red = "\x1B[31m", green = "\x1B[32m", --[[ rest left as exercise :P ]]}
local bgs = {red = "\x1B[41m", green = "\x1B[42m", --[[ rest left as exercise :P ]]}
function printC(fg,bg, ...)
if fg then
if not fgs[fg] then error("Undefined colour: " .. fg) end
io.write(fgs[fg])
end
if bg then
if not bgs[bg] then error("Undefined colour: " .. bg) end
io.write(bgs[bg])
end
print(...)
if fg or bg then
io.write(reset)
end
end
printC("red", "green", "This text is red on green", "This one too")
print("This is normal")
printC(false, false, "This is normal too")
printC("red", false, "This is red text")
printC(false, "green", "This is normal text on green")
Finally, I'd recommend you do it for your internal use only, but to remove colours in the final version. First, it won't work in all Windows terminals, as noted above; second, everyone has different FG/BG settings, e.g. my text is wheat on black, and text colours like blue look fine on a white background but are next to unreadable on a black one. OTOH, yellow looks fine in mine but is next to unreadable on a white BG.
Re: Is it possible to print to the terminal/console in custom colors?
I'm sorry, but ANSICON v1.83 detects as Trojan:Win32/Tiggre!rfn
Our LÖVE Gamedev blog Zabuyaki (an open source retro beat 'em up game). Twitter: @Zabuyaki.
LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua
LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua
Re: Is it possible to print to the terminal/console in custom colors?
I'm pretty sure that's a false positive caused by the code injection that is at play here. Anyway, source code is available to check and compile it yourself
Re: Is it possible to print to the terminal/console in custom colors?
Yup. Sure.
There is a gfx console emulator with UTF8 / colors support
https://github.com/rinqu-eu/love2d-console
There is a gfx console emulator with UTF8 / colors support
https://github.com/rinqu-eu/love2d-console
Our LÖVE Gamedev blog Zabuyaki (an open source retro beat 'em up game). Twitter: @Zabuyaki.
LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua
LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua
Who is online
Users browsing this forum: Google [Bot] and 8 guests