diff options
| author | ruki <[email protected]> | 2017-12-07 22:46:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-12-08 14:10:23 +0800 |
| commit | cb44535305f596bd757ffb58e5ae3cde6d3589fa (patch) | |
| tree | 343f0ff9287b90204a6a000e0af1a7a1ed6c640b /xmake | |
| parent | abe87994168053e4f420a8b7ab286c65b546eaae (diff) | |
improve log and add color attrs for curses
Diffstat (limited to 'xmake')
| -rw-r--r-- | xmake/core/base/log.lua | 42 | ||||
| -rw-r--r-- | xmake/core/base/object.lua | 18 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/utils.lua | 18 | ||||
| -rw-r--r-- | xmake/core/main.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/object.lua | 26 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/ui/test.lua | 9 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/utils.lua | 14 | ||||
| -rw-r--r-- | xmake/core/ui/canvas.lua | 143 | ||||
| -rw-r--r-- | xmake/core/ui/curses.lua | 181 | ||||
| -rw-r--r-- | xmake/core/ui/log.lua | 48 | ||||
| -rw-r--r-- | xmake/core/ui/object.lua | 26 |
12 files changed, 475 insertions, 54 deletions
diff --git a/xmake/core/base/log.lua b/xmake/core/base/log.lua index e9b05f8c1..3ba6589a0 100644 --- a/xmake/core/base/log.lua +++ b/xmake/core/base/log.lua @@ -26,11 +26,11 @@ local log = log or {} -- get the log file -function log.file() +function log:file() -- get the output file - if log._FILE == nil then - local outputfile = log.outputfile() + if self._FILE == nil then + local outputfile = self:outputfile() if outputfile then -- get directory @@ -48,48 +48,48 @@ function log.file() end -- open the log file - log._FILE = io.open(outputfile, 'w+') + self._FILE = io.open(outputfile, 'w+') end - log._FILE = log._FILE or false + self._FILE = self._FILE or false end - return log._FILE + return self._FILE end -- get the output file -function log.outputfile() - if log._LOGFILE == nil then - log._LOGFILE = os.getenv("XMAKE_LOGFILE") or false +function log:outputfile() + if self._LOGFILE == nil then + self._LOGFILE = os.getenv("XMAKE_LOGFILE") or false end - return log._LOGFILE + return self._LOGFILE end -- flush log to file -function log.flush() - local file = log.file() +function log:flush() + local file = log:file() if file then io.flush(file) end end -- close the log file -function log.close() - local file = log.file() +function log:close() + local file = log:file() if file then file:close() end end -- print log to the log file -function log.print(...) - local file = log.file() +function log:print(...) + local file = log:file() if file then file:write(string.format(...) .. "\n") end end -- print variables to the log file -function log.printv(...) - local file = log.file() +function log:printv(...) + local file = log:file() if file then local values = {...} for i, v in ipairs(values) do @@ -108,8 +108,8 @@ function log.printv(...) end -- printf log to the log file -function log.printf(...) - local file = log.file() +function log:printf(...) + local file = self:file() if file then file:write(string.format(...)) end @@ -117,7 +117,7 @@ end -- write log the log file function log:write(...) - local file = log.file() + local file = self:file() if file then file:write(...) end diff --git a/xmake/core/base/object.lua b/xmake/core/base/object.lua index 1922b0b4d..ab4bf468f 100644 --- a/xmake/core/base/object.lua +++ b/xmake/core/base/object.lua @@ -28,20 +28,10 @@ local object = object or {} -- taken from 'std' library: http://luaforge.net/projects/stdlib/ -- and http://lua-cui.sourceforge.net/ -- --- create an object/class: --- > object/class = parent {value, ...; field = value ...} --- > An object's metatable is itself. --- > In the initialiser, unnamed values are assigned to the fields --- > given by _init (assuming the default _clone). --- > Private fields and methods start with "_" --- > --- --- Access an object field: object.field --- > Call an object method: object:method (...) --- > Call a class method: class.method (self, ...) --- > --- > Add a field: object.field = x --- > Add a method: function object:method (...) ... end +-- local point = object { _init = {"x", "y"} } +-- +-- local p1 = point {1, 2} +-- > p1 {x = 1, y = 2} -- -- permute some indices of a table diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index e311af3a3..00b38f60a 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -653,7 +653,7 @@ end function os.raise(msg, ...) -- flush log - log.flush() + log:flush() -- flush io buffer io.flush() diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index e28938648..8a31330e7 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -78,7 +78,7 @@ function utils.print(format, ...) utils._print(message) -- write to the log file - log.printv(message) + log:printv(message) end -- print format string without newline @@ -94,7 +94,7 @@ function utils.printf(format, ...) utils._iowrite(message) -- write to the log file - log.write(message) + log:write(message) end -- print format string and colors with newline @@ -110,8 +110,8 @@ function utils.cprint(format, ...) utils._print(colors.translate(message)) -- write to the log file - if log.file() then - log.printv(colors.ignore(message)) + if log:file() then + log:printv(colors.ignore(message)) end end @@ -128,8 +128,8 @@ function utils.cprintf(format, ...) utils._iowrite(colors.translate(message)) -- write to the log file - if log.file() then - log.write(colors.ignore(message)) + if log:file() then + log:write(colors.ignore(message)) end end @@ -148,7 +148,7 @@ function utils.verror(format, ...) -- enable verbose? if option.get("verbose") and format ~= nil then utils.cprint("${bright red}error: ${clear}" .. string.tryformat(format, ...)) - log.flush() + log:flush() end end @@ -158,7 +158,7 @@ function utils.error(format, ...) -- trace if format ~= nil then utils.cprint("${bright red}error: ${clear}" .. string.tryformat(format, ...)) - log.flush() + log:flush() end end @@ -182,7 +182,7 @@ function utils.warning(format, ...) end -- flush - log.flush() + log:flush() end -- ifelse, a? b : c diff --git a/xmake/core/main.lua b/xmake/core/main.lua index 6706b448d..6e76342ab 100644 --- a/xmake/core/main.lua +++ b/xmake/core/main.lua @@ -271,7 +271,7 @@ Or you can add `--root` option to allow run as root temporarily. end -- close log - log.close() + log:close() -- ok return 0 diff --git a/xmake/core/sandbox/modules/import/core/base/object.lua b/xmake/core/sandbox/modules/import/core/base/object.lua new file mode 100644 index 000000000..77e724611 --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/base/object.lua @@ -0,0 +1,26 @@ +--!A cross-platform 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 object.lua +-- + +-- return module +return require("base/object") diff --git a/xmake/core/sandbox/modules/import/core/ui/test.lua b/xmake/core/sandbox/modules/import/core/ui/test.lua index e4680a67f..6985c0ba1 100644 --- a/xmake/core/sandbox/modules/import/core/ui/test.lua +++ b/xmake/core/sandbox/modules/import/core/ui/test.lua @@ -26,10 +26,17 @@ local sandbox_core_ui_test = sandbox_core_ui_test or {} -- load modules +local log = require("ui/log") +local curses = require("ui/curses") local raise = require("sandbox/modules/raise") function test() + log:flush() + curses.init() + print(curses.color_pair("yellow", "red")) + + --[[ curses.init() local blines = 5 local olines = 10 @@ -70,7 +77,7 @@ function test() if (cmd == 'exit' or string.byte(cmd, 1, 1) == 4) then break end - end + end]] end function sandbox_core_ui_test.main() diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index e86eb2c08..bbd4c74c8 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -60,7 +60,7 @@ function sandbox_utils._print(...) utils._print(unpack(args)) -- write to the log file - log.printv(unpack(args)) + log:printv(unpack(args)) end -- print format string with newline @@ -83,7 +83,7 @@ function sandbox_utils.print(format, ...) utils._print(message) -- write to the log file - log.printv(message) + log:printv(message) end, catch { @@ -110,7 +110,7 @@ function sandbox_utils.printf(format, ...) utils._iowrite(message) -- write log to the log file - log.write(message) + log:write(message) end -- print format string, the builtin variables and colors with newline @@ -123,8 +123,8 @@ function sandbox_utils.cprint(format, ...) utils._print(colors.translate(message)) -- write log to the log file - if log.file() then - log.printv(colors.ignore(message)) + if log:file() then + log:printv(colors.ignore(message)) end end @@ -138,8 +138,8 @@ function sandbox_utils.cprintf(format, ...) utils._iowrite(colors.translate(message)) -- write log to the log file - if log.file() then - log.write(colors.ignore(message)) + if log:file() then + log:write(colors.ignore(message)) end end diff --git a/xmake/core/ui/canvas.lua b/xmake/core/ui/canvas.lua new file mode 100644 index 000000000..3b538308f --- /dev/null +++ b/xmake/core/ui/canvas.lua @@ -0,0 +1,143 @@ +--!A cross-platform 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 canvas.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio (tiago.dionizio AT gmail.com) +$Id: canvas.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- load modules +local object = require("ui/object") +local curses = require("ui/curses") + +-- define module +local canvas = canvas or object() + +-- new canvas instance +function canvas:new(view, window) + + -- create instance + self = self() + + -- save view and window + self._view = view + self._window = window + assert(view and window, "cannot new canvas instance without view and window!") + + -- TODO + self:attr() + return self +end + +-- clear canvas +function canvas:clear() + self._window:clear() + return self +end + +-- move canvas to the given position +function canvas:move(x, y) + self._window:move(y, x) + return self +end + +-- get canvas border +function canvas:border() + self._window:border() + return self +end + +-- write a character to canvas +function canvas:write_ch(ch) + self._window:addch(ch) + return self +end + +-- write an acs character +function canvas:write_acs(ch) + return self:write_ch(curses.acs(ch)) +end + +-- write a string to canvas +function canvas:write(str, len) + if type(str) == 'string' then + self._window:addstr(str, len) + else + self._window:addchstr(str._line, len) + end + return self +end + +function canvas:attr(attrs, modify) + local w = self._window + local apply = w.attron + local attr = curses.calc_attr(attrs) + + if modify == nil then + w:attrset(attr) + elseif modify == false then + w:attroff(attr) + else + w:attron(attr) + end + + apply(w, attr) + + return self +end + +--[[ +function canvas:line(length) + return Line:create(length) +end + + +function Line:create(length) + self = self() + self._length = length + self._line = curses.new_chstr(length) + return self +end + +function Line:__len() + return self._length +end + +function Line:ch(offset, char, attrs, length) + self._line:set_ch(offset, char, attrs and calc_attr(attrs), length) + return self +end + +function Line:acs(offset, char, attrs, length) + return self:ch(offset, acs(char), attrs, length) +end + +function Line:str(offset, str, attrs, rep) + self._line:set_str(offset, str, calc_attr(attrs), rep) + return self +end +]] + +-- return module +return canvas diff --git a/xmake/core/ui/curses.lua b/xmake/core/ui/curses.lua new file mode 100644 index 000000000..dc3ea0d06 --- /dev/null +++ b/xmake/core/ui/curses.lua @@ -0,0 +1,181 @@ +--!A cross-platform 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 curses.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio ([email protected]) +$Id: core.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- define module: curses +local curses = curses or {} + +-- load modules +local os = require("base/os") +local log = require("ui/log") + +-- get color from the given name +function curses.color(name) + if name == 'black' then return curses.COLOR_BLACK + elseif name == 'red' then return curses.COLOR_RED + elseif name == 'green' then return curses.COLOR_GREEN + elseif name == 'yellow' then return curses.COLOR_YELLOW + elseif name == 'blue' then return curses.COLOR_BLUE + elseif name == 'magenta' then return curses.COLOR_MAGENTA + elseif name == 'cyan' then return curses.COLOR_CYAN + elseif name == 'white' then return curses.COLOR_WHITE + else return curses.COLOR_BLACK + end +end + +-- get attr from the given name +function curses.attr(name) + if name == 'normal' then return curses.A_NORMAL + elseif name == 'standout' then return curses.A_STANDOUT + elseif name == 'underline' then return curses.A_UNDERLINE + elseif name == 'reverse' then return curses.A_REVERSE + elseif name == 'blink' then return curses.A_BLINK + elseif name == 'dim' then return curses.A_DIM + elseif name == 'bold' then return curses.A_BOLD + elseif name == 'protect' then return curses.A_PROTECT + elseif name == 'invis' then return curses.A_INVIS + elseif name == 'alt' then return curses.A_ALTCHARSET + else return curses.A_NORMAL + end +end + +-- get acs character from the given name +function curses.acs(name) + if char == 'block' then return curses.ACS_BLOCK + elseif char == 'board' then return curses.ACS_BOARD + elseif char == 'btee' then return curses.ACS_BTEE + elseif char == 'bullet' then return curses.ACS_BULLET + elseif char == 'ckboard' then return curses.ACS_CKBOARD + elseif char == 'darrow' then return curses.ACS_DARROW + elseif char == 'degree' then return curses.ACS_DEGREE + elseif char == 'diamond' then return curses.ACS_DIAMOND + elseif char == 'gequal' then return curses.ACS_GEQUAL + elseif char == 'hline' then return curses.ACS_HLINE + elseif char == 'lantern' then return curses.ACS_LANTERN + elseif char == 'larrow' then return curses.ACS_LARROW + elseif char == 'lequal' then return curses.ACS_LEQUAL + elseif char == 'llcorner' then return curses.ACS_LLCORNER + elseif char == 'lrcorner' then return curses.ACS_LRCORNER + elseif char == 'ltee' then return curses.ACS_LTEE + elseif char == 'nequal' then return curses.ACS_NEQUAL + elseif char == 'pi' then return curses.ACS_PI + elseif char == 'plminus' then return curses.ACS_PLMINUS + elseif char == 'plus' then return curses.ACS_PLUS + elseif char == 'rarrow' then return curses.ACS_RARROW + elseif char == 'rtee' then return curses.ACS_RTEE + elseif char == 's1' then return curses.ACS_S1 + elseif char == 's3' then return curses.ACS_S3 + elseif char == 's7' then return curses.ACS_S7 + elseif char == 's9' then return curses.ACS_S9 + elseif char == 'sterling' then return curses.ACS_STERLING + elseif char == 'ttee' then return curses.ACS_TTEE + elseif char == 'uarrow' then return curses.ACS_UARROW + elseif char == 'ulcorner' then return curses.ACS_ULCORNER + elseif char == 'urcorner' then return curses.ACS_URCORNER + elseif char == 'vline' then return curses.ACS_VLINE + elseif type(char) == 'string' and #char == 1 then + return char + else + return ' ' + end +end + +-- calculate attr from the attributes list +-- +-- local attr = curses.calc_attr("yellow") +-- local attr = curses.calc_attr{ curses.color_pair("yellow", "green"), 'bold' } +-- +function curses.calc_attr(attrs) + + -- curses.calc_attr(curses.A_BOLD) + local atype = type(attrs) + if atype == 'number' then + return attrs + -- curses.calc_attr("bold") + elseif atype == 'string' then + return curses.attr(attrs) + -- curses.calc_attr{ curses.color_pair("yellow", "green"), 'bold' } + elseif atype == 'table' then + local v = 0 + local set = {} + for _, a in pairs(attrs) do --TODO ipairs? + if not set[a] and a then + set[a] = true + if type(a) == 'number' then + v = v + a + else + v = v + curses.attr(a) + end + end + end + return v + else + return 0 + end +end + +-- get attr from the color pair +function curses.color_pair(fg, bg) + + -- get foreground and backround color + fg = curses.color(fg) + bg = curses.color(bg) + + -- attempt to get color from the cache first + local key = fg .. ':' .. bg + local colors = curses._COLORS or {} + if colors[key] then + return colors[key] + end + + -- no colors? + if not curses.has_colors() then + return 0 + end + + -- update the colors count + curses._NCOLORS = (curses._NCOLORS or 0) + 1 + + -- init the color pair + if not curses.init_pair(curses._NCOLORS, fg, bg) then + os.raise("failed to initialize color pair (%d, %s, %s)", curses._NCOLORS, fg, bg) + end + + -- get the color attr + local attr = curses.color_pair(curses._NCOLORS) + + -- save to cache + colors[key] = attr + curses._COLORS = colors + + -- ok + return attr +end + +-- return module: curses +return curses diff --git a/xmake/core/ui/log.lua b/xmake/core/ui/log.lua new file mode 100644 index 000000000..25cbfd7be --- /dev/null +++ b/xmake/core/ui/log.lua @@ -0,0 +1,48 @@ +--!A cross-platform 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 log.lua +-- + +-- get log +local log = log or (function () + + -- load modules + local os = require("base/os") + local path = require("base/path") + + -- get log directory + local logdir = nil + if os.isfile(os.projectfile()) then + logdir = path.join(os.projectdir(), ".xmake") + else + logdir = os.tmpdir() + end + + -- return module: log + local instance = require("base/log") + if instance then + instance._FILE = nil + instance._LOGFILE = path.join(logdir, "ui.log") + end + return instance +end)() +return log diff --git a/xmake/core/ui/object.lua b/xmake/core/ui/object.lua new file mode 100644 index 000000000..3e8aee7ab --- /dev/null +++ b/xmake/core/ui/object.lua @@ -0,0 +1,26 @@ +--!A cross-platform 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 object.lua +-- + +-- return module: object +return require("base/object") |
