color
color copied to clipboard
Idea for supporting Windows
Have every element of colors.bg and colors.fg have a metatable which implements __concat, that will create a table storing some value representing the color as well as the string. Said table's metatable will also implement __concat, as well as __tostring. When print() is called on said metatable, __tostring is called. This will, on non-win32 systems, this just makes a string with the ANSI escapes. On win32 systems, this will call into a C function that calls SetConsoleTextAttribute, and prints the associated string, and recursively also prints any of the other such tables it was concatenated with.
A bit complex, but I think it's an elegant way to have cross-platform compatibility.
What do you think? Would you accept a pull request implementing that were I to implement it?
The problem I see is that you may be calling tostring() for reasons other than printing it. Also, does print offer any guarantees about which order it tostrings its arguments on? And this wouldn't work for someone using string.format().
What you'd need to do is make a replacement print(), I think, that would handle turning userdata into C calls. And that still wouldn't let you use string.format... :(
Hm. Maybe, rather than hijacking __tostring, could replace _G.print on windows, with an implementation that interprets ANSI escapes.
In fact, this sounds useful by itself, I think I'll implement it in its own library.