From 5cc98b887250bf9ce4d45a71cd5dbbe83f80d3d5 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 18 Apr 2020 22:33:00 -0700 Subject: [PATCH] Cleaned up clearing logic and added some more API to Lua --- C/test.c | 2 +- Lua/lbtui.c | 88 ++++++++++++++++++++++++++++++++++++++++------------ Lua/test.lua | 9 +++++- btui.h | 5 +-- 4 files changed, 80 insertions(+), 24 deletions(-) diff --git a/C/test.c b/C/test.c index 2de940f..5f23363 100644 --- a/C/test.c +++ b/C/test.c @@ -22,7 +22,7 @@ int main(void) int mouse_x = -1, mouse_y = -1; int key = btui_getkey(bt, &mouse_x, &mouse_y); - btui_clear(bt); + btui_clear_screen(bt); switch (key) { case 'q': case KEY_CTRL_C: done = 1; break; case -1: break; diff --git a/Lua/lbtui.c b/Lua/lbtui.c index b2c0e24..f8939df 100644 --- a/Lua/lbtui.c +++ b/Lua/lbtui.c @@ -38,6 +38,28 @@ static int Lbtui_disable(lua_State *L) return 0; } +static int Lbtui_withdisabled(lua_State *L) +{ + btui_t **bt = (btui_t**)lua_touserdata(L, 1); + if (bt == NULL) luaL_error(L, "Not a BTUI object"); + int top = lua_gettop(L); + if (top < 2) luaL_error(L, "No function provided"); + + lua_pushcfunction(L, Lbtui_disable); + lua_pushvalue(L, 1); + lua_call(L, 1, 0); + + lua_pushvalue(L, 2); + lua_call(L, 0, LUA_MULTRET); + int top2 = lua_gettop(L); + + lua_pushcfunction(L, Lbtui_enable); + lua_pushvalue(L, 1); + lua_call(L, 1, 0); + + return top2 - top; +} + static int Lbtui_getkey(lua_State *L) { btui_t **bt = (btui_t**)lua_touserdata(L, 1); @@ -74,7 +96,31 @@ static int Lbtui_clear(lua_State *L) { btui_t **bt = (btui_t**)lua_touserdata(L, 1); if (bt == NULL) luaL_error(L, "Not a BTUI object"); - btui_clear(*bt); + const char *cleartype = luaL_optlstring(L, 2, "screen", NULL); + if (strcmp(cleartype, "screen") == 0) { + btui_clear_screen(*bt); + } else if (strcmp(cleartype, "below") == 0) { + btui_clear_below(*bt); + } else if (strcmp(cleartype, "above") == 0) { + btui_clear_above(*bt); + } else if (strcmp(cleartype, "right") == 0) { + btui_clear_right(*bt); + } else if (strcmp(cleartype, "left") == 0) { + btui_clear_left(*bt); + } else if (strcmp(cleartype, "line") == 0) { + btui_clear_line(*bt); + } else { + luaL_argerror(L, 2, "unknown clear type"); + } + btui_flush(*bt); + return 0; +} + +static int Lbtui_flush(lua_State *L) +{ + btui_t **bt = (btui_t**)lua_touserdata(L, 1); + if (bt == NULL) luaL_error(L, "Not a BTUI object"); + btui_clear_screen(*bt); btui_flush(*bt); return 0; } @@ -237,25 +283,6 @@ static int Lbtui_tostring(lua_State *L) return 1; } -static const luaL_Reg Rclass_metamethods[] = -{ - {"__tostring", Lbtui_tostring}, - {"enable", Lbtui_enable}, - {"disable", Lbtui_disable}, - {"getkey", Lbtui_getkey}, - {"print", Lbtui_print}, - {"clear", Lbtui_clear}, - {"move", Lbtui_move}, - {"withfg", Lbtui_withfg}, - {"withbg", Lbtui_withbg}, - {"withattributes", Lbtui_withattributes}, - {"setattributes", Lbtui_setattributes}, - {"unsetattributes", Lbtui_unsetattributes}, - {"width", Lbtui_width}, - {"height", Lbtui_height}, - {NULL, NULL} -}; - static struct { const char* name; lua_Unsigned code; @@ -344,6 +371,27 @@ static struct { {NULL, 0} }; +static const luaL_Reg Rclass_metamethods[] = +{ + {"__tostring", Lbtui_tostring}, + {"enable", Lbtui_enable}, + {"disable", Lbtui_disable}, + {"withdisabled", Lbtui_withdisabled}, + {"getkey", Lbtui_getkey}, + {"print", Lbtui_print}, + {"clear", Lbtui_clear}, + {"flush", Lbtui_flush}, + {"move", Lbtui_move}, + {"withfg", Lbtui_withfg}, + {"withbg", Lbtui_withbg}, + {"withattributes", Lbtui_withattributes}, + {"setattributes", Lbtui_setattributes}, + {"unsetattributes", Lbtui_unsetattributes}, + {"width", Lbtui_width}, + {"height", Lbtui_height}, + {NULL, NULL} +}; + LUALIB_API int luaopen_btui(lua_State *L) { // Set up attributes diff --git a/Lua/test.lua b/Lua/test.lua index 0180afb..6fab0f0 100644 --- a/Lua/test.lua +++ b/Lua/test.lua @@ -4,6 +4,14 @@ btui(function(bt) local key = nil local x, y = 0, 0 while key ~= "q" and key ~= "Ctrl-c" do + if key == "?" then + bt:withdisabled(function() + io.write("OK? ") + io.flush() + io.read() + end) + end + bt:clear() bt:move(x, y) bt:withbg(150,225,80, function() @@ -11,7 +19,6 @@ btui(function(bt) bt:print(" Pressed: ", key, " ") end) end) - if key == "e" then error("ERR MESSAGE") end local title = "Lua BTUI Demo" bt:withattributes("bold", "underline", function() diff --git a/btui.h b/btui.h index fc4b706..f1ac6a8 100644 --- a/btui.h +++ b/btui.h @@ -38,10 +38,11 @@ int btui_set_bg_hex(btui_t *bt, int hex); #define btui_printf(bt, ...) fprintf((bt)->out, __VA_ARGS__) #define btui_puts(bt, s) fputs(s, (bt)->out) #define btui_flush(bt) fflush((bt)->out) -#define btui_clear(bt) fputs("\033[2J", (bt)->out) #define btui_clear_below(bt) fputs("\033[J", (bt)->out) #define btui_clear_above(bt) fputs("\033[1J", (bt)->out) -#define btui_clear_eol(bt) fputs("\033[K", (bt)->out) +#define btui_clear_screen(bt) fputs("\033[2J", (bt)->out) +#define btui_clear_right(bt) fputs("\033[K", (bt)->out) +#define btui_clear_left(bt) fputs("\033[1K", (bt)->out) #define btui_clear_line(bt) fputs("\033[2K", (bt)->out) static btui_t current_bt;