From e6271147512555e6d009d4ba1703ad08bafbda0d Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 10 Mar 2022 04:22:49 -0800 Subject: [PATCH] zet.tlv: first screen tests In the process I found a couple of bugs in fake screen primitives. --- src/teliva.c | 3 ++ template.tlv | 15 ++++++-- zet.tlv | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 111 insertions(+), 6 deletions(-) diff --git a/src/teliva.c b/src/teliva.c index b604524..65f2acc 100644 --- a/src/teliva.c +++ b/src/teliva.c @@ -1212,6 +1212,9 @@ static int run_tests(lua_State* L) { const char* key = lua_tostring(L, -2); if (strncmp("test_", key, strlen("test_")) != 0) continue; if (!lua_isfunction(L, -1)) continue; + /* uncomment these lines when it's not clear which test is failing */ +//? addstr(key); +//? addstr("|"); int status = lua_pcall(L, 0, 0, 0); if (status) { printw("E%d: %s", status, lua_tostring(L, -1)); diff --git a/template.tlv b/template.tlv index f36d526..31fa6b4 100644 --- a/template.tlv +++ b/template.tlv @@ -258,7 +258,7 @@ > return rawget(h, key) > end > h.attrset = function(self, x) - > table.insert(self.scr.attrs, x) + > self.scr.attrs = x > end > h.attron = function(self, x) > -- currently same as attrset since Lua 5.1 doesn't have bitwise operators @@ -296,13 +296,16 @@ > h.mvaddch = function(self, y, x, c) > self.scr.cursy = y > self.scr.cursx = x - > self.addch(c) + > self:addch(c) > end > h.mvaddstr = function(self, y, x, s) > self.scr.cursy = y > self.scr.cursx = x > self:addstr(s) > end + > h.clear = function(self) + > clear_scr(self.scr) + > end > return h >end - __teliva_timestamp: original @@ -319,6 +322,12 @@ >function scr(props) > props.cursx = 1 > props.cursy = 1 + > clear_scr(props) + > return props + >end +- __teliva_timestamp: original + clear_scr: + >function clear_scr(props) > for y=1,props.h do > props[y] = {} > for x=1,props.w do @@ -332,7 +341,7 @@ >function check_screen(window, contents, message) > local x, y = 1, 1 > for i=1,contents:len() do - > check_eq(contents[i], window.scr[y][x].data, message..'/'..y..','..x) + > check_eq(window.scr[y][x].data, contents[i], message..'/'..y..','..x) > x = x+1 > if x > window.scr.w then > y = y+1 diff --git a/zet.tlv b/zet.tlv index a38f80e..50ea2b7 100644 --- a/zet.tlv +++ b/zet.tlv @@ -260,7 +260,7 @@ > return rawget(h, key) > end > h.attrset = function(self, x) - > table.insert(self.scr.attrs, x) + > self.scr.attrs = x > end > h.attron = function(self, x) > -- currently same as attrset since Lua 5.1 doesn't have bitwise operators @@ -298,13 +298,16 @@ > h.mvaddch = function(self, y, x, c) > self.scr.cursy = y > self.scr.cursx = x - > self.addch(c) + > self:addch(c) > end > h.mvaddstr = function(self, y, x, s) > self.scr.cursy = y > self.scr.cursx = x > self:addstr(s) > end + > h.clear = function(self) + > clear_scr(self.scr) + > end > return h >end - __teliva_timestamp: original @@ -321,6 +324,12 @@ >function scr(props) > props.cursx = 1 > props.cursy = 1 + > clear_scr(props) + > return props + >end +- __teliva_timestamp: original + clear_scr: + >function clear_scr(props) > for y=1,props.h do > props[y] = {} > for x=1,props.w do @@ -334,7 +343,7 @@ >function check_screen(window, contents, message) > local x, y = 1, 1 > for i=1,contents:len() do - > check_eq(contents[i], window.scr[y][x].data, message..'/'..y..','..x) + > check_eq(window.scr[y][x].data, contents[i], message..'/'..y..','..x) > x = x+1 > if x > window.scr.w then > y = y+1 @@ -3726,3 +3735,87 @@ > outfile:send(json.encode(zettels)) > outfile:close() >end +- __teliva_timestamp: + >Thu Mar 10 04:21:28 2022 + render_zettel: + >function render_zettel(window, bg, indent, edge_label, starty, startx, zettel) + > window:attrset(curses.color_pair(bg)) + > for y=0,view_settings.height-1 do + > for x=0,view_settings.width-1 do + > window:mvaddch(y+starty, x+startx, ' ') + > end + > end + > if indent >= 2 then -- need at least 2 spaces to be able to print edge_label + > window:attrset(curses.color_pair(bg+1)) -- go from zettel color to its edge color + > window:mvaddstr(starty, startx+indent-1, edge_label) + > window:attrset(curses.color_pair(bg)) + > end + > local y, x = 0, indent+1 + > local data = '' + > if zettel then + > data = zettel.data + > end + > for i=1,#data do + > local c = data[i] + > if c == '\n' then + > y = y+1 + > x = indent+1 + > else + > window:mvaddstr(y+starty, x+startx, c) + > x = x+1 + > if x >= startx + view_settings.width then + > y = y+1 + > x = indent+1 + > end + > end + > if y >= view_settings.height then + > break + > end + > end + >end + > + >function test_render_zettel() + > local w = window{scr=scr{h=5, w=10}} + > -- + > render_zettel(w, 34, 1, -- color 34, indent 1 + > '*', 1, 1, -- startx, starty + > {data='abc'}) + > check_screen(w, ' abc '.. + > ' '.. + > ' '.. + > ' '.. + > ' ', + > 'test_render_zettel: single line, top-left') + > -- entire width is used by the single zettel + > -- column 1 = margin, column 2 = indent + > check_color(w, 34, '##########'.. + > '##########'.. + > '##########'.. + > ' '.. + > ' ', + > 'test_render_zettel: single line, top-left, background') + > -- + > w:clear() + > render_zettel(w, 34, 1, '*', 3, 4, {data='abc'}) -- startx=3, starty=4 + > check_screen(w, ' '.. + > ' '.. + > ' abc '.. + > ' '.. + > ' ', + > 'test_render_zettel: specific coord') + > check_color(w, 34, ' '.. + > ' '.. + > ' #######'.. + > ' #######'.. + > ' #######', + > 'test_render_zettel: specific coord, background') + > -- + > w:clear() + > render_zettel(w, 34, 2, '*', 3, 4, {data='abc'}) -- startx=3, starty=4 + > check_screen(w, ' '.. + > ' '.. + > ' * abc '.. + > ' '.. + > ' ', + > 'test_render_zettel: indent >= 2 prints edge label') + >end