summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-09-07 11:55:37 +0800
committerruki <[email protected]>2017-09-07 11:55:37 +0800
commitf7fe93d37dd1e863a09c0d41f7ec74200cc9242c (patch)
tree8f921a530ce419499c04712d780e452aaff4d414
parentb64cfa7b328df9cb389821c9348b4774024b251a (diff)
disable to output colors code to file
-rw-r--r--CHANGELOG.md2
-rw-r--r--core/src/xmake/io/isatty.c70
-rw-r--r--core/src/xmake/io/prefix.h36
-rw-r--r--core/src/xmake/machine.c12
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--xmake/core/base/colors.lua10
-rw-r--r--xmake/core/base/io.lua13
-rw-r--r--xmake/core/sandbox/modules/io.lua10
8 files changed, 148 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e46a18d8f..f90ecb2d6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@
* Optimize to detect flags
* Add `COLORTERM=nocolor` to disable color output
* Remove `and_bindings` and `add_rbindings` api
+* Disable to output colors code to file
### Bugs fixed
@@ -365,6 +366,7 @@
* 通过缓存优化flags探测,加速编译效率
* 添加`COLORTERM=nocolor`环境变量开关,禁用彩色输出
* 移除`add_rbindings`和`add_bindings`接口
+* 禁止在重定向的时候进行彩色输出,避免输出文件中带有色彩代码干扰
### Bugs修复
diff --git a/core/src/xmake/io/isatty.c b/core/src/xmake/io/isatty.c
new file mode 100644
index 000000000..a210771a4
--- /dev/null
+++ b/core/src/xmake/io/isatty.c
@@ -0,0 +1,70 @@
+/*!The Make-like Build Utility based on Lua
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015 - 2017, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file isatty.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "isatty"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+#ifndef TB_CONFIG_OS_WINDOWS
+# include <unistd.h>
+#endif
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* stdout: io.isatty()
+ * stderr: io.isatty(io.stderr)
+ * stdin: io.isatty(io.stdin)
+ */
+tb_int_t xm_io_isatty(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // no arguments? default: stdout
+ tb_int_t answer = 1;
+#ifndef TB_CONFIG_OS_WINDOWS
+ if (lua_gettop(lua) == 0)
+ answer = isatty(1);
+ else
+ {
+ FILE** fp = (FILE**)luaL_checkudata(lua, 1, LUA_FILEHANDLE);
+ if (fp) answer = isatty(fileno(*fp));
+ }
+#endif
+
+ // return answer
+ lua_pushboolean(lua, answer);
+
+ // ok
+ return 1;
+}
diff --git a/core/src/xmake/io/prefix.h b/core/src/xmake/io/prefix.h
new file mode 100644
index 000000000..a75aa03e4
--- /dev/null
+++ b/core/src/xmake/io/prefix.h
@@ -0,0 +1,36 @@
+/*!The Make-like Build Utility based on Lua
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015 - 2017, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file prefix.h
+ *
+ */
+#ifndef XM_IO_PREFIX_H
+#define XM_IO_PREFIX_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "../prefix.h"
+
+
+#endif
+
+
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index e1a080d3a..34d80d618 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -87,6 +87,9 @@ tb_int_t xm_os_gid(lua_State* lua);
tb_int_t xm_os_getown(lua_State* lua);
#endif
+// the io functions
+tb_int_t xm_io_isatty(lua_State* lua);
+
// the path functions
tb_int_t xm_path_relative(lua_State* lua);
tb_int_t xm_path_absolute(lua_State* lua);
@@ -167,6 +170,12 @@ static luaL_Reg const g_os_functions[] =
, { tb_null, tb_null }
};
+// the io functions
+static luaL_Reg const g_io_functions[] =
+{
+ { "isatty", xm_io_isatty }
+};
+
// the path functions
static luaL_Reg const g_path_functions[] =
{
@@ -445,6 +454,9 @@ xm_machine_ref_t xm_machine_init()
// bind os functions
luaL_register(impl->lua, "os", g_os_functions);
+ // bind io functions
+ luaL_register(impl->lua, "io", g_io_functions);
+
// bind path functions
luaL_register(impl->lua, "path", g_path_functions);
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 9992ab656..ee3062634 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -40,6 +40,7 @@ xmake_C_FILES += \
os/uid \
os/gid \
os/getown \
+ io/isatty \
path/relative \
path/absolute \
path/translate \
diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua
index d1d96210d..f545dc6ce 100644
--- a/xmake/core/base/colors.lua
+++ b/xmake/core/base/colors.lua
@@ -178,6 +178,9 @@ function colors.rainbow(index, seed, freq, spread)
end
-- translate colors from the string
+--
+-- @param str the string with colors
+-- @param force force to translate all colors?
--
-- colors:
--
@@ -199,7 +202,7 @@ end
--
-- "${beer}hello${beer}world"
--
-function colors.translate(str)
+function colors.translate(str, force)
-- check string
if not str then
@@ -212,6 +215,11 @@ function colors.translate(str)
-- translate it
str = string.gsub(str, "(%${(.-)})", function(_, word)
+ -- ignore all colors if no tty (redirect ..)
+ if not force and not io.isatty() then
+ return ""
+ end
+
-- not supported? ignore it
if not colors.has256() and not colors.truecolor() then
return ""
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index bf794da17..8da88b10e 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -31,8 +31,9 @@ local path = require("base/path")
local table = require("base/table")
local utils = require("base/utils")
--- save original open
-io._open = io._open or io.open
+-- save original apis
+io._open = io._open or io.open
+io._isatty = io._isatty or io.isatty
-- read file
function _file:read(...)
@@ -239,6 +240,14 @@ function io.writefile(filepath, data)
return true
end
+-- replace the original isatty interface
+function io.isatty(fd)
+ if io._ISATTY == nil then
+ io._ISATTY = io._isatty(fd or io.stdout)
+ end
+ return io._ISATTY
+end
+
-- replace the original open interface
function io.open(filepath, mode)
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
index 414423620..6b47582c5 100644
--- a/xmake/core/sandbox/modules/io.lua
+++ b/xmake/core/sandbox/modules/io.lua
@@ -33,9 +33,13 @@ local vformat = require("sandbox/modules/vformat")
local sandbox_io = sandbox_io or {}
-- inherit some builtin interfaces
-sandbox_io.flush = io.flush
-sandbox_io.read = io.read
-sandbox_io.write = io.write
+sandbox_io.flush = io.flush
+sandbox_io.read = io.read
+sandbox_io.write = io.write
+sandbox_io.stdin = io.stdin
+sandbox_io.stderr = io.stderr
+sandbox_io.stdout = io.stdout
+sandbox_io.isatty = io.isatty
-- print file
function sandbox_io._print(self, ...)