peddroelm Advanced Cheater Reputation: 0
Joined: 03 Oct 2014 Posts: 84
|
Posted: Fri Apr 06, 2018 10:49 pm Post subject: Is it possible to change colors for Lua output window texts |
|
|
Working on some combat feedback table and I would like to have damage related feedback stuff, XP gain related feedback stuff, various state and status related stuff all with different text color.
Is there some simple set text color instruction my google fu has failed to find ?
(how) Can this be achieved ?
|
|
peddroelm Advanced Cheater Reputation: 0
Joined: 03 Oct 2014 Posts: 84
|
Posted: Sat Apr 07, 2018 1:25 am Post subject: |
|
|
Sorry , I can't make much sense of your previous advice .
I tried saving this as ansicolors.LUA on desktop (same location as my .CT file)
http://lua-users.org/wiki/AnsiTerminalColors
https://github.com/kikito/ansicolors.lua
Code: |
- support detection
local function isWindows()
return type(package) == 'table' and type(package.config) == 'string' and package.config:sub(1,1) == '\\'
end
local supported = not isWindows()
if isWindows() then supported = os.getenv("ANSICON") end
local keys = {
-- reset
reset = 0,
-- misc
bright = 1,
dim = 2,
underline = 4,
blink = 5,
reverse = 7,
hidden = 8,
-- foreground colors
black = 30,
red = 31,
green = 32,
yellow = 33,
blue = 34,
magenta = 35,
cyan = 36,
white = 37,
-- background colors
blackbg = 40,
redbg = 41,
greenbg = 42,
yellowbg = 43,
bluebg = 44,
magentabg = 45,
cyanbg = 46,
whitebg = 47
}
local escapeString = string.char(27) .. '[%dm'
local function escapeNumber(number)
return escapeString:format(number)
end
local function escapeKeys(str)
if not supported then return "" end
local buffer = {}
local number
for word in str:gmatch("%w+") do
number = keys[word]
assert(number, "Unknown key: " .. word)
table.insert(buffer, escapeNumber(number) )
end
return table.concat(buffer)
end
local function replaceCodes(str)
str = string.gsub(str,"(%%{(.-)})", function(_, str) return escapeKeys(str) end )
return str
end
-- public
local function ansicolors( str )
str = tostring(str or '')
return replaceCodes('%{reset}' .. str .. '%{reset}')
end
return setmetatable({noReset = replaceCodes}, {__call = function (_, str) return ansicolors (str) end})
|
and then executing
Code: |
local colors = require 'ansicolors'
print(colors('%{red}hello'))
print(colors('%{redbg}hello%{reset}'))
print(colors('%{bright red underline}hello'))
|
in the Lua engine window, without success :
Code: |
local colors = require 'ansicolors'
print(colors('%{red}hello'))
print(colors('%{redbg}hello%{reset}'))
print(colors('%{bright red underline}hello'))
Error:[string " local colors = require 'ansicolors'
..."]:2: attempt to call a table value (local 'colors')
Script Error
|
EDIT:
"sadly ....
Notice that the tests will only work on an ANSI-compatible machine (windows isn’t ANSI-compatible)
Windows
Windows console, by default, isn’t capable of handling ANSI color codes correctly. This library tries to detect whether it is on a windows machine (by looking at package.path) and in that case it suppresses all ANSI control characters (the text appears devoid of color, but legible)."
EDIT2 :
or maybe not?
"... One notable exception was the Win32 console of Microsoft Windows before Windows 10 update TH2."
|
|