]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Disable colors if not a tty
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 3 Dec 2017 16:49:07 +0000 (16:49 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 3 Dec 2017 16:55:30 +0000 (16:55 +0000)
lualib/rspamadm/ansicolors.lua
src/lua/lua_util.c

index 739cf427c637cc55f6a05732796a82faa7f4cea1..81783f618b553a09418b794e7d44e5792ae25e53 100644 (file)
@@ -1,6 +1,9 @@
 local colormt = {}
 local ansicolors = {}
 
+local rspamd_util = require "rspamd_util"
+local isatty = rspamd_util.isatty()
+
 function colormt:__tostring()
   return self.value
 end
@@ -14,9 +17,16 @@ function colormt:__call(s)
 end
 
 colormt.__metatable = {}
-
 local function makecolor(value)
-  return setmetatable({ value = string.char(27) .. '[' .. tostring(value) .. 'm' }, colormt)
+  if isatty then
+    return setmetatable({
+      value = string.char(27) .. '[' .. tostring(value) .. 'm'
+    }, colormt)
+  else
+    return setmetatable({
+      value = ''
+    }, colormt)
+  end
 end
 
 local colors = {
index 29c24f0a0596f7e1febc0b0c035abaf553ed72f4..480269b733293f8685fc86874d53d31a71518d3c 100644 (file)
@@ -411,6 +411,12 @@ LUA_FUNCTION_DEF (util, mkdir);
  */
 LUA_FUNCTION_DEF (util, umask);
 
+/***
+ * @function util.isatty()
+ * Returns if stdout is a tty
+ * @return {boolean} true in case of output being tty
+ */
+LUA_FUNCTION_DEF (util, isatty);
 
 /***
  * @function util.pack(fmt, ...)
@@ -559,6 +565,7 @@ static const struct luaL_reg utillib_f[] = {
        LUA_INTERFACE_DEF (util, file_exists),
        LUA_INTERFACE_DEF (util, mkdir),
        LUA_INTERFACE_DEF (util, umask),
+       LUA_INTERFACE_DEF (util, isatty),
        LUA_INTERFACE_DEF (util, get_hostname),
        LUA_INTERFACE_DEF (util, pack),
        LUA_INTERFACE_DEF (util, unpack),
@@ -2253,6 +2260,19 @@ lua_util_umask (lua_State *L)
        return 1;
 }
 
+static gint
+lua_util_isatty (lua_State *L)
+{
+       if (isatty (STDOUT_FILENO)) {
+               lua_pushboolean (L, true);
+       }
+       else {
+               lua_pushboolean (L, false);
+       }
+
+       return 1;
+}
+
 /* Backport from Lua 5.3 */
 
 /******************************************************************************