Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ h2. Usage
print(colors('%{red}hello'))
print(colors('%{redbg}hello%{reset}'))
print(colors('%{bright red underline}hello'))

colors.enable(false)
print(colors('%{red}this is printed plain since coloring is disabled'))
colors.enable(true)
print(colors('%{red}this is printed in red again'))
</pre>

The @colors@ function makes sure that color attributes are reset at each end of the generated string. If you want to generate complex strings piece-by-piece, use @colors.noReset@, which works exactly the same, but without adding the reset codes at each end of the string.
Expand All @@ -35,7 +40,13 @@ Notice that the tests will only work on an ANSI-compatible machine (windows isn'

h2. 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).
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).

By default coloring is disabled on Windows, except if:

* Environment variable @ANSICON@ has been set
* Module @luasystem@ is available and terminal processing has been enabled on @io.stdout@
* It is manually enabled by calling @enable(true)@

h2. Valid attribute list:

Expand Down
27 changes: 23 additions & 4 deletions ansicolors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ 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 enabled = not isWindows()
if isWindows() then
enabled = os.getenv("ANSICON")
if not enabled then
local ok, sys = pcall(require, 'system')
if ok and sys.getconsoleflags then
local flags = sys.getconsoleflags(io.stdout)
if flags then
enabled = flags:has_all_of(sys.COF_VIRTUAL_TERMINAL_PROCESSING)
end
end
end
end


local keys = {
-- reset
Expand Down Expand Up @@ -70,7 +82,7 @@ end

local function escapeKeys(str)

if not supported then return "" end
if not enabled then return "" end

local buffer = {}
local number
Expand All @@ -97,4 +109,11 @@ local function ansicolors( str )
end


return setmetatable({noReset = replaceCodes}, {__call = function (_, str) return ansicolors (str) end})
return setmetatable({
noReset = replaceCodes,
enable = function(val) enabled = not not val end,
}, {
__call = function(_, str)
return ansicolors(str)
end,
})