diff options
| author | ruki <[email protected]> | 2020-05-31 21:43:47 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-05-31 21:43:47 +0800 |
| commit | 41bd7287e986fcf881c8d20494058c576bbcf252 (patch) | |
| tree | 49d6b3d7082d87d37ff656ace7a0cd710245fda6 | |
| parent | 3c1d11faa2ca0c6e6eb1664ee938d5c49867b02d (diff) | |
add tty and replace clearline
| -rw-r--r-- | xmake/actions/require/impl/action/download.lua | 9 | ||||
| -rw-r--r-- | xmake/actions/require/impl/action/install.lua | 5 | ||||
| -rw-r--r-- | xmake/actions/require/impl/package.lua | 5 | ||||
| -rw-r--r-- | xmake/actions/update/main.lua | 9 | ||||
| -rw-r--r-- | xmake/core/base/colors.lua | 4 | ||||
| -rw-r--r-- | xmake/core/base/tty.lua | 74 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/tty.lua (renamed from xmake/core/base/terminal.lua) | 9 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/utils.lua | 10 | ||||
| -rw-r--r-- | xmake/modules/private/utils/progress.lua | 3 |
9 files changed, 97 insertions, 31 deletions
diff --git a/xmake/actions/require/impl/action/download.lua b/xmake/actions/require/impl/action/download.lua index 16e68eb20..b09e77011 100644 --- a/xmake/actions/require/impl/action/download.lua +++ b/xmake/actions/require/impl/action/download.lua @@ -20,6 +20,7 @@ -- imports import("core.base.option") +import("core.base.tty") import(".utils.filter") import("net.http") import("devel.git") @@ -35,7 +36,7 @@ function _checkout(package, url, sourcedir, url_alias) -- clean the previous build files git.clean({repodir = packagedir, force = true}) - utils.clearline() + tty.erase_line_to_start().cr() return end @@ -64,7 +65,7 @@ function _checkout(package, url, sourcedir, url_alias) os.mv(sourcedir .. ".tmp", sourcedir) -- trace - utils.clearline() + tty.erase_line_to_start().cr() cprint("${yellow} => ${clear}clone %s %s .. ${color.success}${text.success}", url, package:version_str()) end @@ -117,7 +118,7 @@ function _download(package, url, sourcedir, url_alias, url_excludes) package:originfile_set(path.absolute(packagefile)) -- trace - utils.clearline() + tty.erase_line_to_start().cr() if not cached then cprint("${yellow} => ${clear}download %s .. ${color.success}${text.success}", url) end @@ -200,7 +201,7 @@ function main(package) end -- trace - utils.clearline() + tty.erase_line_to_start().cr() if git.checkurl(url) then cprint("${yellow} => ${clear}clone %s %s .. ${color.failure}${text.failure}", url, package:version_str()) else diff --git a/xmake/actions/require/impl/action/install.lua b/xmake/actions/require/impl/action/install.lua index ce927c096..03edb566f 100644 --- a/xmake/actions/require/impl/action/install.lua +++ b/xmake/actions/require/impl/action/install.lua @@ -20,6 +20,7 @@ -- imports import("core.base.option") +import("core.base.tty") import("core.project.target") import("lib.detect.find_file") import("test") @@ -203,7 +204,7 @@ function main(package) package:envs_leave() -- trace - utils.clearline() + tty.erase_line_to_start().cr() cprint("${yellow} => ${clear}install %s %s .. ${color.success}${text.success}", package:name(), package:version_str() or "") end, @@ -222,7 +223,7 @@ function main(package) end -- trace - utils.clearline() + tty.erase_line_to_start().cr() cprint("${yellow} => ${clear}install %s %s .. ${color.failure}${text.failure}", package:name(), package:version_str() or "") -- leave the package environments diff --git a/xmake/actions/require/impl/package.lua b/xmake/actions/require/impl/package.lua index 8f7671e60..9bc25e3a4 100644 --- a/xmake/actions/require/impl/package.lua +++ b/xmake/actions/require/impl/package.lua @@ -24,6 +24,7 @@ import("core.base.option") import("core.base.global") import("core.base.hashset") import("core.base.scheduler") +import("core.base.tty") import("private.async.runjobs") import("private.utils.progress") import("lib.detect.cache", {alias = "detectcache"}) @@ -678,7 +679,7 @@ function _install_packages(packages_install, packages_download) -- trace progress_helper:clear() - utils.clearline() + tty.erase_line_to_start().cr() cprintf("${yellow} => ") if #downloading > 0 then cprintf("downloading ${magenta}%s", table.concat(downloading, ", ")) @@ -690,7 +691,7 @@ function _install_packages(packages_install, packages_download) progress_helper:write() end, exit = function(errors) if errors then - utils.clearline() + tty.erase_line_to_start().cr() io.flush() end end}) diff --git a/xmake/actions/update/main.lua b/xmake/actions/update/main.lua index c947288fe..fde0cfa06 100644 --- a/xmake/actions/update/main.lua +++ b/xmake/actions/update/main.lua @@ -23,6 +23,7 @@ import("core.base.semver") import("core.base.option") import("core.base.task") import("core.base.process") +import("core.base.tty") import("net.http") import("devel.git") import("devel.git.submodule") @@ -146,7 +147,7 @@ function _install(sourcedir) local installdir = is_host("windows") and os.programdir() or "~/.local/bin" -- trace - utils.clearline() + tty.erase_line_to_start().cr() cprintf("${yellow} => ${clear}installing to %s .. ", installdir) local ok = try { @@ -187,7 +188,7 @@ function _install(sourcedir) -- trace if ok then - utils.clearline() + tty.erase_line_to_start().cr() cprint("${yellow} => ${clear}install to %s .. ${color.success}${text.success}", installdir) else raise("install failed!") @@ -342,7 +343,7 @@ function main() -- the download task local download_task = function () for idx, url in ipairs(mainurls) do - utils.clearline() + tty.erase_line_to_start().cr() cprintf("${yellow} => ${clear}downloading %s .. ", url) local ok = try { @@ -373,7 +374,7 @@ function main() end } } - utils.clearline() + tty.erase_line_to_start().cr() if ok then cprint("${yellow} => ${clear}download %s .. ${color.success}${text.success}", url) break diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua index 2781de044..4aa4dcc12 100644 --- a/xmake/core/base/colors.lua +++ b/xmake/core/base/colors.lua @@ -139,7 +139,7 @@ colors._keys24 = } -- the escape string -colors._escape = '\x1b[%sm' +colors._ESC = '\x1b[%sm' -- get colorterm setting -- @@ -418,7 +418,7 @@ function colors.translate(str, opt) -- make result local result = "" if #color_buffer > 0 and not nocolors then - result = result .. colors._escape:format(table.concat(color_buffer, ";")) + result = result .. colors._ESC:format(table.concat(color_buffer, ";")) end if #text_buffer > 0 then result = result .. table.concat(text_buffer, " ") diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua new file mode 100644 index 000000000..d1245fc1a --- /dev/null +++ b/xmake/core/base/tty.lua @@ -0,0 +1,74 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed 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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file tty.lua +-- + +-- define module +local tty = tty or {} + +-- load modules +local io = require("base/io") + +-- @see http://www.termsys.demon.co.uk/vtansi.htm + +-- write control characters +function tty._iowrite(...) + local isatty = tty._ISATTY + if isatty == nil then + isatty = io.isatty() + tty._ISATTY = isatty + end + if isatty then + io.write(...) + end +end + +-- erases from the current cursor position to the end of the current line. +function tty.erase_line_to_end() + tty._iowrite("\x1b[K") + return tty +end + +-- erases from the current cursor position to the start of the current line. +function tty.erase_line_to_start() + tty._iowrite("\x1b[1K") + return tty +end + +-- erases the entire current line +function tty.erase_line() + tty._iowrite("\x1b[2K") + return tty +end + +-- carriage return +function tty.cr() + tty._iowrite("\r") + return tty +end + +-- flush control +function tty.flush() + if io.isatty() then + io.flush() + end + return tty +end + +-- return module +return tty diff --git a/xmake/core/base/terminal.lua b/xmake/core/sandbox/modules/import/core/base/tty.lua index 15a71b08a..810bbeb7d 100644 --- a/xmake/core/base/terminal.lua +++ b/xmake/core/sandbox/modules/import/core/base/tty.lua @@ -15,13 +15,10 @@ -- Copyright (C) 2015-2020, TBOOX Open Source Group. -- -- @author ruki --- @file terminal.lua +-- @file tty.lua -- --- define module -local terminal = terminal or {} +-- load modules +return require("base/tty") --- TODO http://www.termsys.demon.co.uk/terminal.htm --- return module -return terminal diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index 8b7ca4c5f..8492b1302 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -172,16 +172,6 @@ function sandbox_utils.wprint(format, ...) utils.warning(vformat(format, ...)) end --- clear the current terminal line -function sandbox_utils.clearline() - - -- we need not clear line if is not a tty - if not io.isatty() then - return - end - sandbox_utils.printf("\x1b[2K\r") -end - -- assert function sandbox_utils.assert(value, format, ...) diff --git a/xmake/modules/private/utils/progress.lua b/xmake/modules/private/utils/progress.lua index 96678eee3..4ee0f40e8 100644 --- a/xmake/modules/private/utils/progress.lua +++ b/xmake/modules/private/utils/progress.lua @@ -22,6 +22,7 @@ import("core.base.option") import("core.base.object") import("core.base.colors") +import("core.base.tty") import("core.theme.theme") -- make back characters @@ -109,7 +110,7 @@ function show(progress, format, ...) if is_scroll then cprint(progress_prefix .. format, progress, ...) else - utils.clearline() + tty.erase_line_to_start().cr() local msg = vformat(progress_prefix .. format, progress, ...) local msg_plain = colors.translate(msg, {plain = true}) local maxwidth = os.getwinsize().width |
