diff options
| author | ruki <[email protected]> | 2017-12-12 22:48:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-12-12 10:50:35 +0800 |
| commit | fccce25811e0a02bca4ac81a9ecadea85b2f855f (patch) | |
| tree | 9688090cb2aaf82699559d3c8d02df5eebea5bfe /xmake/core/ui | |
| parent | a4729d44c64cd85e4c981e87ee3cf01bab6f16ac (diff) | |
init ui program
Diffstat (limited to 'xmake/core/ui')
| -rw-r--r-- | xmake/core/ui/application.lua | 80 | ||||
| -rw-r--r-- | xmake/core/ui/group.lua | 53 | ||||
| -rw-r--r-- | xmake/core/ui/point.lua | 104 | ||||
| -rw-r--r-- | xmake/core/ui/program.lua | 139 | ||||
| -rw-r--r-- | xmake/core/ui/rect.lua | 142 | ||||
| -rw-r--r-- | xmake/core/ui/view.lua | 97 |
6 files changed, 615 insertions, 0 deletions
diff --git a/xmake/core/ui/application.lua b/xmake/core/ui/application.lua new file mode 100644 index 000000000..2c993456c --- /dev/null +++ b/xmake/core/ui/application.lua @@ -0,0 +1,80 @@ +--!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 application.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio (tiago.dionizio AT gmail.com) +$Id: application.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- load modules +local os = require("base/os") +local curses = require("ui/curses") +local program = require("ui/program") + +-- define module +local application = application or program() + +-- init application +function application:init() + + -- init program + program.init(self) +end + +-- exit application +function application:exit() + + -- exit program + program.exit(self) +end + +-- run application +function application.run(...) + + -- init runner + local argv = {...} + local runner = function () + + -- new an application + local app = application:new() + if app then + app:run(argv) + app:exit() + end + end + + -- run application + local ok, errors = xpcall(runner, debug.traceback) + + -- exit curses + if not ok then + if not curses.isdone() then + curses.done() + end + os.raise(errors) + end +end + +-- return module +return application diff --git a/xmake/core/ui/group.lua b/xmake/core/ui/group.lua new file mode 100644 index 000000000..064ccfd76 --- /dev/null +++ b/xmake/core/ui/group.lua @@ -0,0 +1,53 @@ +--!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 group.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio (tiago.dionizio AT gmail.com) +$Id: group.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- load modules +local view = require("ui/view") +local curses = require("ui/curses") + +-- define module +local group = group or view() + +-- init group +function group:init(bounds) + view.init(self, bounds) +end + +-- exit group +function group:exit() + view.exit(self) +end + +-- execute group +function group:execute() +end + + +-- return module +return group diff --git a/xmake/core/ui/point.lua b/xmake/core/ui/point.lua new file mode 100644 index 000000000..56c586436 --- /dev/null +++ b/xmake/core/ui/point.lua @@ -0,0 +1,104 @@ +--!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 point.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio (tiago.dionizio AT gmail.com) +$Id: point.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- load modules +local object = require("ui/object") + +-- define module +local point = point or object { _init = {"x", "y"} } + +-- add delta x and y +function point:addxy(dx, dy) + self.x = self.x + dx + self.y = self.y + dy + return self +end + +-- add point +function point:add(p) + return self:addxy(p.x, p.y) +end + +-- sub delta x and y +function point:subxy(dx, dy) + return self:addxy(-dx, -dy) +end + +-- sub point +function point:sub(p) + return self:addxy(-p.x, -p.y) +end + +-- p1 + p2 +function point:__add(p) + local np = self() + np.x = np.x + p.x + np.y = np.y + p.y + return np +end + +-- p1 - p2 +function point:__sub(p) + local np = self() + np.x = np.x - p.x + np.y = np.y - p.y + return np +end + +-- -p +function point:__unm() + local p = self() + p.x = -p.x + p.y = -p.y + return p +end + +-- p1 == p2? +function point:__eq(p) + return self.x == p.x and self.y == p.y +end + +-- tostring(p) +function point:__tostring() + return '(' .. self.x .. ',' .. self.y .. ')' +end + +-- p1 .. p2 +function point.__concat(op1, op2) + if type(op1) == 'string' then + return op1 .. op2:__tostring() + elseif type(op2) == 'string' then + return op1:__tostring() .. op2 + else + return op1:__tostring() .. op2:__tostring() + end +end + +-- return module +return point diff --git a/xmake/core/ui/program.lua b/xmake/core/ui/program.lua new file mode 100644 index 000000000..a1ec01edf --- /dev/null +++ b/xmake/core/ui/program.lua @@ -0,0 +1,139 @@ +--!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 program.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio (tiago.dionizio AT gmail.com) +$Id: program.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- load modules +local rect = require("ui/rect") +local group = require("ui/group") +local curses = require("ui/curses") + +-- define module +local program = program or group() + +-- init program +function program:init() + + -- init main window + local main_window = self:main_window() + + -- disable echo + curses.echo(false) + + -- disable input cache + curses.cbreak(true) + + -- disable newline + curses.nl(false) + + -- to filter characters being output to the screen + -- this will filter all characters where a chtype or chstr is used + curses.map_output(true) + + -- on WIN32 ALT keys need to be mapped, so to make sure you get the wanted keys, + -- only makes sense when using keypad(true) and echo(false) + curses.map_keyboard(true) + + -- init colors + if (curses.has_colors()) then + curses.start_color() + end + +-- init_keymap() + + -- disable main window cursor + main_window:leaveok(false) + + -- enable special key map + main_window:keypad(true) + + -- non-block for getch() + main_window:nodelay(true) + + -- get 8-bits character for getch() + main_window:meta(true) + + -- init group + group.init(self, rect {0, 0, curses.columns(), curses.lines()}) + + --[[ + self:set_state('selected', true) + self:set_state('focused', true) + ]] +end + +-- exit program +function program:exit() + + -- exit group + group.exit(self) + + -- (attempt to) make sure the screen will be cleared + -- if not restored by the curses driver + self:main_window():clear() + self:main_window():noutrefresh() + curses.doupdate() + + -- exit curses + assert(not curses.isdone()) + curses.done() +end + +-- get the main window +function program:main_window() + + -- init main window if not exists + local main_window = self._MAIN_WINDOW + if not main_window then + + -- init main window + main_window = curses.init() + assert(main_window, "cannot init main window!") + + -- save main window + self._MAIN_WINDOW = main_window + end + return main_window +end + +-- get the command arguments +function program:argv() + return self._ARGV +end + +-- run program +function program:run(argv) + + -- save the current arguments + self._ARGV = argv + + -- execute group + self:execute() +end + +-- return module +return program diff --git a/xmake/core/ui/rect.lua b/xmake/core/ui/rect.lua new file mode 100644 index 000000000..6264ad67c --- /dev/null +++ b/xmake/core/ui/rect.lua @@ -0,0 +1,142 @@ + +--!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 rect.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio (tiago.dionizio AT gmail.com) +$Id: rect.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- load modules +local point = require("ui/point") +local object = require("ui/object") + +-- define module +local rect = rect or object { _init = {"sx", "sy", "ex", "ey"} } + +-- get rect size +function rect:size() + return point { self.ex - self.sx, self.ey - self.sy } +end + +-- move rect +function rect:move(dx, dy) + self.sx = self.sx + dx + self.sy = self.sy + dy + self.ex = self.ex + dx + self.ey = self.ey + dy + return self +end + +-- move top right corner of the rect +function rect:moves(dx, dy) + self.sx = self.sx + dx + self.sy = self.sy + dy + return self +end + +-- move bottom left corner of the rect +function rect:movee(dx, dy) + self.ex = self.ex + dx + self.ey = self.ey + dy + return self +end + +-- expand rect area +function rect:grow(dx, dy) + self.sx = self.sx - dx + self.sy = self.sy - dy + self.ex = self.ex + dx + self.ey = self.ey + dy + return self +end + +-- set rect with shared area between this rect and a given rect +function rect:intersect(r) + self.sx = math.max(self.sx, r.sx) + self.sy = math.max(self.sy, r.sy) + self.ex = math.min(self.ex, r.ex) + self.ey = math.min(self.ey, r.ey) + return self +end + +-- get rect with shared area between two rects: local rect_new = r1 / r2 +function rect:__div(r) + return self():intersect(r) +end + +-- set union rect +function rect:union(r) + self.sx = math.min(self.sx, r.sx) + self.sy = math.min(self.sy, r.sy) + self.ex = math.max(self.ex, r.ex) + self.ey = math.max(self.ey, r.ey) + return self +end + +-- get union rect: local rect_new = r1 + r2 +function rect:__add(r) + return self():union(r) +end + +-- r1 == r1? +function rect:__eq(r) + return + self.sx == r.sx and + self.sy == r.sy and + self.ex == r.ex and + self.ey == r.ey +end + +-- contains the given point in rect? +function rect:contains(x, y) + return x >= self.sx and x < self.ex and y >= self.sy and y < self.ey +end + +-- empty rect? +function rect:empty() + return self.sx >= self.ex or self.sy >= self.ey +end + +-- tostring(r) +function rect:__tostring() + if self:empty() then + return '[empty]' + end + return '[(' .. self.sx .. ',' .. self.sy .. '),(' .. self.ex .. ',' .. self.ey .. ')]' +end + +-- r1 .. r2 +function rect.__concat(op1, op2) + if type(op1) == 'string' then + return op1 .. op2:__tostring() + elseif type(op2) == 'string' then + return op1:__tostring() .. op2 + else + return op1:__tostring() .. op2:__tostring() + end +end + +-- return module +return rect diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua new file mode 100644 index 000000000..e8e38a9f3 --- /dev/null +++ b/xmake/core/ui/view.lua @@ -0,0 +1,97 @@ +--!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 view.lua +-- + +--[[ Console User Interface (cui) ]----------------------------------------- +Author: Tiago Dionizio (tiago.dionizio AT gmail.com) +$Id: view.lua 18 2007-06-21 20:43:52Z tngd $ +--------------------------------------------------------------------------]] + +-- load modules +local object = require("ui/object") +local curses = require("ui/curses") + +-- define module +local view = view or object() + +-- new view instance +function view:new(bounds) + + -- create instance + self = self() + + -- init view + self:init(bounds) + + -- done + return self +end + +-- init view +function view:init(bounds) + + -- check + assert(type(bounds) == 'table') + + -- init bounds + self:bounds_set(bounds) +end + +-- exit view +function view:exit() + + -- close window + if self._window then + self._window:close() + self._window = nil + end +end + +-- set window bounds +function view:bounds_set(bounds) + + -- close the previous windows first + if self._window then + self._window:close() + end + + -- init bounds + self._bounds = bounds() + + -- create a new window + local size = bounds:size() + self._window = curses.new_pad(size.y > 0 and size.y or 1, size.x > 0 and size.x or 1) + assert(self._window, "cannot create window!") + + -- disable cursor + self._window:leaveok(true) +end + +-- get window bounds +function view:bounds() + return self._bounds() +end + + +-- return module +return view |
