diff options
| author | ruki <[email protected]> | 2017-12-14 00:46:35 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-12-13 22:39:31 +0800 |
| commit | 42cc615918ee1e4cc3ce435172e9f91dc1df1086 (patch) | |
| tree | 323c0dbb450f137d1b15eb31a0f279dcd2675309 | |
| parent | 2694f86469cdd776ba2caba9bcd7ec261457aa2c (diff) | |
fix log and application bug
| -rw-r--r-- | tests/ui/hello.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/log.lua | 25 | ||||
| -rw-r--r-- | xmake/core/ui/application.lua | 25 | ||||
| -rw-r--r-- | xmake/core/ui/group.lua | 27 | ||||
| -rw-r--r-- | xmake/core/ui/point.lua | 2 | ||||
| -rw-r--r-- | xmake/core/ui/program.lua | 8 | ||||
| -rw-r--r-- | xmake/core/ui/rect.lua | 4 | ||||
| -rw-r--r-- | xmake/core/ui/view.lua | 21 |
8 files changed, 91 insertions, 23 deletions
diff --git a/tests/ui/hello.lua b/tests/ui/hello.lua index e2330ddb0..847d75ded 100644 --- a/tests/ui/hello.lua +++ b/tests/ui/hello.lua @@ -27,5 +27,5 @@ import("core.ui.application") -- main entry function main(...) - application.run(...) + application.run("hello", ...) end diff --git a/xmake/core/base/log.lua b/xmake/core/base/log.lua index 3ba6589a0..332d267bb 100644 --- a/xmake/core/base/log.lua +++ b/xmake/core/base/log.lua @@ -28,6 +28,11 @@ local log = log or {} -- get the log file function log:file() + -- disable? + if self._ENABLE ~= nil and not self._ENABLE then + return + end + -- get the output file if self._FILE == nil then local outputfile = self:outputfile() @@ -63,9 +68,21 @@ function log:outputfile() return self._LOGFILE end +-- clear log +function log:clear(state) + if os.isfile(self:outputfile()) then + io.writefile(self:outputfile(), "") + end +end + +-- enable log +function log:enable(state) + self._ENABLE = state +end + -- flush log to file function log:flush() - local file = log:file() + local file = self:file() if file then io.flush(file) end @@ -73,7 +90,7 @@ end -- close the log file function log:close() - local file = log:file() + local file = self:file() if file then file:close() end @@ -81,7 +98,7 @@ end -- print log to the log file function log:print(...) - local file = log:file() + local file = self:file() if file then file:write(string.format(...) .. "\n") end @@ -89,7 +106,7 @@ end -- print variables to the log file function log:printv(...) - local file = log:file() + local file = self:file() if file then local values = {...} for i, v in ipairs(values) do diff --git a/xmake/core/ui/application.lua b/xmake/core/ui/application.lua index 2c993456c..fb7063667 100644 --- a/xmake/core/ui/application.lua +++ b/xmake/core/ui/application.lua @@ -29,6 +29,7 @@ $Id: application.lua 18 2007-06-21 20:43:52Z tngd $ -- load modules local os = require("base/os") +local log = require("ui/log") local curses = require("ui/curses") local program = require("ui/program") @@ -36,10 +37,20 @@ local program = require("ui/program") local application = application or program() -- init application -function application:init() +function application:init(name) + + -- init log + log:clear() +-- log:enable(false) + + -- trace + log:print("<application: %s>: init ..", name) -- init program - program.init(self) + program.init(self, name) + + -- trace + log:print("<application: %s>: init ok", name) end -- exit application @@ -47,19 +58,22 @@ function application:exit() -- exit program program.exit(self) + + -- flush log + log:flush() end -- run application -function application.run(...) +function application.run(name, ...) -- init runner local argv = {...} local runner = function () -- new an application - local app = application:new() + local app = application:new(name) if app then - app:run(argv) + app:loop(argv) app:exit() end end @@ -72,6 +86,7 @@ function application.run(...) if not curses.isdone() then curses.done() end + log:flush() os.raise(errors) end end diff --git a/xmake/core/ui/group.lua b/xmake/core/ui/group.lua index 064ccfd76..a1f6d3bc7 100644 --- a/xmake/core/ui/group.lua +++ b/xmake/core/ui/group.lua @@ -30,20 +30,43 @@ $Id: group.lua 18 2007-06-21 20:43:52Z tngd $ -- load modules local view = require("ui/view") local curses = require("ui/curses") +local dlist = require("base/dlist") -- define module local group = group or view() -- init group -function group:init(bounds) - view.init(self, bounds) +function group:init(name, bounds) + + -- init view + view.init(self, name, bounds) + + -- init child views + self._VIEWS = dlist() end -- exit group function group:exit() + + -- exit view view.exit(self) end +-- get all child views +function group:views() + return self._VIEWS +end + +-- insert view +function group:insert(v) + self:views():push(v) +end + +-- remove view +function group:remove(v) + self:views():remove(v) +end + -- execute group function group:execute() end diff --git a/xmake/core/ui/point.lua b/xmake/core/ui/point.lua index 56c586436..54e41a925 100644 --- a/xmake/core/ui/point.lua +++ b/xmake/core/ui/point.lua @@ -86,7 +86,7 @@ end -- tostring(p) function point:__tostring() - return '(' .. self.x .. ',' .. self.y .. ')' + return '(' .. self.x .. ', ' .. self.y .. ')' end -- p1 .. p2 diff --git a/xmake/core/ui/program.lua b/xmake/core/ui/program.lua index da4edabc7..75c08fc79 100644 --- a/xmake/core/ui/program.lua +++ b/xmake/core/ui/program.lua @@ -36,7 +36,7 @@ local curses = require("ui/curses") local program = program or group() -- init program -function program:init() +function program:init(name) -- init main window local main_window = self:main_window() @@ -76,7 +76,7 @@ function program:init() main_window:meta(true) -- init group - group.init(self, rect {0, 0, curses.columns(), curses.lines()}) + group.init(self, name, rect {0, 0, curses.columns(), curses.lines()}) end -- exit program @@ -118,8 +118,8 @@ function program:argv() return self._ARGV end --- run program -function program:run(argv) +-- run program loop +function program:loop(argv) -- save the current arguments self._ARGV = argv diff --git a/xmake/core/ui/rect.lua b/xmake/core/ui/rect.lua index 6264ad67c..c7a11330f 100644 --- a/xmake/core/ui/rect.lua +++ b/xmake/core/ui/rect.lua @@ -122,9 +122,9 @@ end -- tostring(r) function rect:__tostring() if self:empty() then - return '[empty]' + return '[]' end - return '[(' .. self.sx .. ',' .. self.sy .. '),(' .. self.ex .. ',' .. self.ey .. ')]' + return string.format("[%d, %d, %d, %d]", self.sx, self.sy, self.ex, self.ey) end -- r1 .. r2 diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua index 4008ce547..bf7bd8f45 100644 --- a/xmake/core/ui/view.lua +++ b/xmake/core/ui/view.lua @@ -36,23 +36,23 @@ local curses = require("ui/curses") local view = view or object() -- new view instance -function view:new(bounds) +function view:new(name, bounds) -- create instance self = self() -- init view - self:init(bounds) + self:init(name, bounds) -- done return self end -- init view -function view:init(bounds) +function view:init(name, bounds) -- check - assert(type(bounds) == 'table') + assert(name and type(bounds) == 'table') -- init state local state = object() @@ -65,6 +65,9 @@ function view:init(bounds) state.modal = false -- is modal view? self._STATE = state + -- init name + self._NAME = name + -- init bounds and window self:bounds_set(bounds) end @@ -79,6 +82,11 @@ function view:exit() end end +-- get view name +function view:name() + return self._NAME +end + -- set window bounds function view:bounds_set(bounds) @@ -181,5 +189,10 @@ function view:state_set(name, enable) end end +-- tostring(view) +function view:__tostring() + return string.format("<view: %s %s>", self:name(), tostring(self:bounds())) +end + -- return module return view |
