Is it possible to print to the terminal/console in custom colors?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Jasoco
Inner party member
Posts: 3620
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Is it possible to print to the terminal/console in custom colors?

Post by Jasoco » Fri May 04, 2018 8:55 pm

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.

User avatar
pgimeno
Party member
Posts: 1231
Joined: Sun Oct 18, 2015 2:58 pm

Re: Is it possible to print to the terminal/console in custom colors?

Post by pgimeno » Sat May 05, 2018 12:02 am

You can assume an ANSI type terminal and use these:

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")
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories
The Microsoft Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

User avatar
Jasoco
Inner party member
Posts: 3620
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?

Post by Jasoco » Sat May 05, 2018 4:58 am

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.

User avatar
Nixola
Inner party member
Posts: 1935
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Is it possible to print to the terminal/console in custom colors?

Post by Nixola » Sat May 05, 2018 8:55 am

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

grump
Party member
Posts: 378
Joined: Sat Jul 22, 2017 7:43 pm

Re: Is it possible to print to the terminal/console in custom colors?

Post by grump » Sat May 05, 2018 9:12 am

Nixola wrote:
Sat May 05, 2018 8:55 am
Will that work on windows?
Not with vanilla cmd or PowerShell. ANSICON makes it work.

User avatar
pgimeno
Party member
Posts: 1231
Joined: Sun Oct 18, 2015 2:58 pm

Re: Is it possible to print to the terminal/console in custom colors?

Post by pgimeno » Sat May 05, 2018 9:39 am

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:

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")
The other, simpler way is to clear to end of line after sending the reset code, to undo the colourization:

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")
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.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories
The Microsoft Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

User avatar
D0NM
Party member
Posts: 192
Joined: Mon Feb 08, 2016 10:35 am
Location: Zabuyaki
Contact:

Re: Is it possible to print to the terminal/console in custom colors?

Post by D0NM » Thu May 10, 2018 4:25 pm

grump wrote:
Sat May 05, 2018 9:12 am
Not with vanilla cmd or PowerShell. ANSICON makes it work.
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). Follow @Zabuyaki.
Twitter @FaMeSoft / GP32 Free Platformer: SUPER PLUSHA
:joker: LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua :joker:

grump
Party member
Posts: 378
Joined: Sat Jul 22, 2017 7:43 pm

Re: Is it possible to print to the terminal/console in custom colors?

Post by grump » Thu May 10, 2018 6:14 pm

D0NM wrote:
Thu May 10, 2018 4:25 pm
grump wrote:
Sat May 05, 2018 9:12 am
Not with vanilla cmd or PowerShell. ANSICON makes it work.
I'm sorry, but ANSICON v1.83 detects as Trojan:Win32/Tiggre!rfn
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 ;)

User avatar
D0NM
Party member
Posts: 192
Joined: Mon Feb 08, 2016 10:35 am
Location: Zabuyaki
Contact:

Re: Is it possible to print to the terminal/console in custom colors?

Post by D0NM » Fri May 11, 2018 11:26 am

Yup. Sure.

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). Follow @Zabuyaki.
Twitter @FaMeSoft / GP32 Free Platformer: SUPER PLUSHA
:joker: LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua :joker:

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests