From c98d28e118d4da9a4f99ca109106ca7721b58df4 Mon Sep 17 00:00:00 2001 From: Thijs Date: Sun, 26 May 2024 00:31:29 +0200 Subject: [PATCH] feat: 'enable' function and improved Windows detection Adds a method to manually enable/disable the functionality. And adds better Windows detection when setting the default value for `enabled`. --- README.textile | 13 ++++++++++++- ansicolors.lua | 27 +++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.textile b/README.textile index 4e37acf..dccda08 100644 --- a/README.textile +++ b/README.textile @@ -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')) 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. @@ -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: diff --git a/ansicolors.lua b/ansicolors.lua index f474952..5f7ae0e 100644 --- a/ansicolors.lua +++ b/ansicolors.lua @@ -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 @@ -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 @@ -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, +})