summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-01-05 00:47:12 +0800
committerruki <[email protected]>2018-01-04 22:43:13 +0800
commitc5435a4c499fbf1ff2d09b4250881a04c351f705 (patch)
tree5515294859b7bdbab66035aab619b716427f3644
parent4b51f53356778c4fa3242814e79fd493cbc729ad (diff)
draw border with line for window
-rw-r--r--xmake/core/ui/canvas.lua57
-rw-r--r--xmake/core/ui/curses.lua68
-rw-r--r--xmake/core/ui/label.lua2
-rw-r--r--xmake/core/ui/view.lua2
-rw-r--r--xmake/core/ui/window.lua56
5 files changed, 113 insertions, 72 deletions
diff --git a/xmake/core/ui/canvas.lua b/xmake/core/ui/canvas.lua
index b02864cea..33e33b267 100644
--- a/xmake/core/ui/canvas.lua
+++ b/xmake/core/ui/canvas.lua
@@ -22,16 +22,13 @@
-- @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 log = require("ui/log")
local object = require("ui/object")
local curses = require("ui/curses")
-- define module
+local line = line or object()
local canvas = canvas or object()
-- new canvas instance
@@ -64,33 +61,45 @@ function canvas:move(x, y)
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
+-- get the current position
+function canvas:pos(x, y)
+ local y, x = self._window:getyx()
+ return x, y
end
--- write an acs character
-function canvas:write_acs(ch)
- return self:write_ch(curses.acs(ch))
-end
+-- put character to canvas
+function canvas:putchar(ch, n, vertical)
+
+ -- acs character?
+ if type(ch) == "string" and #ch > 1 then
+ 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)
+ -- draw characters
+ n = n or 1
+ if vertical then
+ local x, y = self:pos()
+ while n > 0 do
+ self:move(x, y)
+ self._window:addch(ch)
+ n = n - 1
+ y = y + 1
+ end
else
- self._window:addchstr(str._line, len)
+ while n > 0 do
+ self._window:addch(ch)
+ n = n - 1
+ end
end
return self
end
+-- put a string to canvas
+function canvas:puts(str, len)
+ self._window:addstr(str, len)
+ return self
+end
+
-- set canvas attributes
--
-- set attr: canvas:attr("bold")
diff --git a/xmake/core/ui/curses.lua b/xmake/core/ui/curses.lua
index ac7a2473c..efe51c580 100644
--- a/xmake/core/ui/curses.lua
+++ b/xmake/core/ui/curses.lua
@@ -72,40 +72,40 @@ 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
+ if name == 'block' then return curses.ACS_BLOCK
+ elseif name == 'board' then return curses.ACS_BOARD
+ elseif name == 'btee' then return curses.ACS_BTEE
+ elseif name == 'bullet' then return curses.ACS_BULLET
+ elseif name == 'ckboard' then return curses.ACS_CKBOARD
+ elseif name == 'darrow' then return curses.ACS_DARROW
+ elseif name == 'degree' then return curses.ACS_DEGREE
+ elseif name == 'diamond' then return curses.ACS_DIAMOND
+ elseif name == 'gequal' then return curses.ACS_GEQUAL
+ elseif name == 'hline' then return curses.ACS_HLINE
+ elseif name == 'lantern' then return curses.ACS_LANTERN
+ elseif name == 'larrow' then return curses.ACS_LARROW
+ elseif name == 'lequal' then return curses.ACS_LEQUAL
+ elseif name == 'llcorner' then return curses.ACS_LLCORNER
+ elseif name == 'lrcorner' then return curses.ACS_LRCORNER
+ elseif name == 'ltee' then return curses.ACS_LTEE
+ elseif name == 'nequal' then return curses.ACS_NEQUAL
+ elseif name == 'pi' then return curses.ACS_PI
+ elseif name == 'plminus' then return curses.ACS_PLMINUS
+ elseif name == 'plus' then return curses.ACS_PLUS
+ elseif name == 'rarrow' then return curses.ACS_RARROW
+ elseif name == 'rtee' then return curses.ACS_RTEE
+ elseif name == 's1' then return curses.ACS_S1
+ elseif name == 's3' then return curses.ACS_S3
+ elseif name == 's7' then return curses.ACS_S7
+ elseif name == 's9' then return curses.ACS_S9
+ elseif name == 'sterling' then return curses.ACS_STERLING
+ elseif name == 'ttee' then return curses.ACS_TTEE
+ elseif name == 'uarrow' then return curses.ACS_UARROW
+ elseif name == 'ulcorner' then return curses.ACS_ULCORNER
+ elseif name == 'urcorner' then return curses.ACS_URCORNER
+ elseif name == 'vline' then return curses.ACS_VLINE
+ elseif type(name) == 'string' and #name == 1 then
+ return name
else
return ' '
end
diff --git a/xmake/core/ui/label.lua b/xmake/core/ui/label.lua
index 94b25e833..aea099e0e 100644
--- a/xmake/core/ui/label.lua
+++ b/xmake/core/ui/label.lua
@@ -56,7 +56,7 @@ function label:draw()
local str = self:text()
if str and #str > 0 and textattr then
str = string.sub(str, 1, self:width())
- self:canvas():attr(textattr):move(0, 0):write(str)
+ self:canvas():attr(textattr):move(0, 0):puts(str)
end
end
diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua
index 701e3f95b..e9f0ccb82 100644
--- a/xmake/core/ui/view.lua
+++ b/xmake/core/ui/view.lua
@@ -184,7 +184,7 @@ function view:draw()
local background = self:background()
if background then
background = curses.color_pair(background, background)
- self:canvas():attr(background):move(0, 0):write(string.rep(' ', self:width() * self:height()))
+ self:canvas():attr(background):move(0, 0):putchar(' ', self:width() * self:height())
else
self:canvas():clear()
end
diff --git a/xmake/core/ui/window.lua b/xmake/core/ui/window.lua
index 4a3344057..41f637967 100644
--- a/xmake/core/ui/window.lua
+++ b/xmake/core/ui/window.lua
@@ -38,7 +38,8 @@ function window:init(name, bounds, title)
-- init panel
panel.init(self, name, bounds)
- -- TODO check bounds
+ -- check bounds
+ assert(self:width() > 4 and self:height() > 3, string.format("%s: too small!", self))
-- init title
if title then
@@ -48,6 +49,12 @@ function window:init(name, bounds, title)
-- insert frame
self:insert(self:frame())
+
+ -- init shadow
+ self:shadow_set("black")
+
+ -- init border
+ self:border_set("black")
end
-- draw window
@@ -57,20 +64,23 @@ function window:draw()
panel.draw(self)
-- draw shadow
- local shadow = curses.color_pair("black", "black")
- self:canvas():attr(shadow):move(2, self:height() - 1):write(string.rep(' ', self:width() - 2))
- for y = 1, self:height() - 1 do
- self:canvas():move(self:width() - 2, y):write(' ')
+ local shadow = self:shadow()
+ if shadow then
+ self:canvas():attr(curses.color_pair(shadow, shadow))
+ self:canvas():move(2, self:height() - 1):putchar(' ', self:width() - 2)
+ self:canvas():move(self:width() - 2, 1):putchar(' ', self:height() - 1, true)
+ self:canvas():move(self:width() - 1, 1):putchar(' ', self:height() - 1, true)
end
- -- TODO draw line
-- draw border
- local border = curses.color_pair("black", "white")
- self:canvas():attr(border):move(0, self:height() - 2):write(' ')
- self:canvas():move(1, self:height() - 2):write(string.rep('-', self:width() - 4))
- self:canvas():move(self:width() - 3, 0):write('-')
- for y = 1, self:height() - 2 do
- self:canvas():move(self:width() - 3, y):write('|')
+ local border = self:border()
+ if border then
+ self:canvas():attr(curses.color_pair(border, self:frame():background()))
+ self:canvas():move(0, self:height() - 2):putchar(' ')
+ self:canvas():move(1, self:height() - 2):putchar("hline", self:width() - 4)
+ self:canvas():move(self:width() - 3, 0):putchar('urcorner')
+ self:canvas():move(self:width() - 3, 1):putchar('vline', self:height() - 3, true)
+ self:canvas():move(self:width() - 3, self:height() - 2):putchar('lrcorner')
end
end
@@ -89,5 +99,27 @@ function window:title()
return self._TITLE
end
+-- get shadow
+function window:shadow()
+ return self._SHADOW
+end
+
+-- set shadow
+function window:shadow_set(shadow)
+ self._SHADOW = shadow
+ self:invalidate()
+end
+
+-- get border
+function window:border()
+ return self._BORDER
+end
+
+-- set border
+function window:border_set(border)
+ self._BORDER = border
+ self:invalidate()
+end
+
-- return module
return window