From f14957613be813f33e12455b161bdcd51784cbfb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 18 Apr 2020 21:39:33 -0700 Subject: [PATCH] Standardized demos a bit. --- C/test.c | 16 +++++++++++----- Lua/lbtui.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ Lua/test.lua | 13 ++++++++++++- btui.h | 6 +++--- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/C/test.c b/C/test.c index 1396b43..82c65b9 100644 --- a/C/test.c +++ b/C/test.c @@ -7,10 +7,16 @@ int main(void) if (!bt) return 1; int done = 0; int x = 0, y = 0; - int i = 0; while (!done) { + const char *title = "BTUI C Demo"; + btui_set_attributes(bt, BTUI_FG_BLUE | BTUI_BOLD); + btui_move_cursor(bt, (bt->width - (int)strlen(title)) / 2, 0); + btui_printf(bt, "%s", title); + + btui_set_attributes(bt, BTUI_FG_NORMAL | BTUI_FAINT); btui_move_cursor(bt, 0, bt->height-1); - btui_printf(bt, "Update %d, size = %dx%d", i++, bt->width, bt->height); + btui_printf(bt, "Size = %dx%d", bt->width, bt->height); + btui_set_attributes(bt, BTUI_NORMAL); btui_flush(bt); int mouse_x = -1, mouse_y = -1; @@ -33,9 +39,9 @@ int main(void) char buf[256] = {0}; btui_keyname(key, buf); btui_move_cursor(bt, x, y); - //btui_set_attributes(bt, BTUI_FG_YELLOW | BTUI_BOLD); - btui_set_fg_hex(bt, 0xacff40); - btui_printf(bt, "Pressed: %s", buf); + btui_set_bg_hex(bt, 0xacff40); + btui_set_attributes(bt, BTUI_FG_BLACK); + btui_printf(bt, " Pressed: %s ", buf); btui_set_attributes(bt, BTUI_NORMAL); fflush(bt->out); break; diff --git a/Lua/lbtui.c b/Lua/lbtui.c index 01292a6..01f7f8c 100644 --- a/Lua/lbtui.c +++ b/Lua/lbtui.c @@ -94,6 +94,49 @@ static int Lbtui_move(lua_State *L) return 0; } +static int Lbtui_withfg(lua_State *L) +{ + btui_t **bt = (btui_t**)lua_touserdata(L, 1); + if (bt == NULL) luaL_error(L, "Not a BTUI object"); + if (lua_gettop(L) < 5) luaL_error(L, "Expected r,g,b values and a function"); + int isnum; + int r = lua_tointegerx(L, 2, &isnum); + if (!isnum) luaL_error(L, "Expected integer r value"); + int g = lua_tointegerx(L, 3, &isnum); + if (!isnum) luaL_error(L, "Expected integer g value"); + int b = lua_tointegerx(L, 4, &isnum); + if (!isnum) luaL_error(L, "Expected integer b value"); + btui_set_fg_rgb(*bt, r, g, b); + int top = lua_gettop(L); + int status = lua_pcall(L, 0, LUA_MULTRET, 0); + btui_set_attributes(*bt, BTUI_FG_NORMAL); + if (status != LUA_OK) + lua_error(L); + return lua_gettop(L) - top; +} + +static int Lbtui_withbg(lua_State *L) +{ + btui_t **bt = (btui_t**)lua_touserdata(L, 1); + if (bt == NULL) luaL_error(L, "Not a BTUI object"); + if (lua_gettop(L) < 5) luaL_error(L, "Expected r,g,b values and a function"); + int isnum; + int r = lua_tointegerx(L, 2, &isnum); + if (!isnum) luaL_error(L, "Expected integer r value"); + int g = lua_tointegerx(L, 3, &isnum); + if (!isnum) luaL_error(L, "Expected integer g value"); + int b = lua_tointegerx(L, 4, &isnum); + if (!isnum) luaL_error(L, "Expected integer b value"); + btui_set_bg_rgb(*bt, r, g, b); + int top = lua_gettop(L); + int status = lua_pcall(L, 0, LUA_MULTRET, 0); + btui_set_attributes(*bt, BTUI_BG_NORMAL); + if (status != LUA_OK) + lua_error(L); + return lua_gettop(L) - top; +} + + static int Lbtui_width(lua_State *L) { btui_t **bt = (btui_t**)lua_touserdata(L, 1); @@ -141,6 +184,8 @@ static const luaL_Reg Rclass_metamethods[] = { "print", Lbtui_print}, { "clear", Lbtui_clear}, { "move", Lbtui_move}, + { "withfg", Lbtui_withfg}, + { "withbg", Lbtui_withbg}, { "width", Lbtui_width}, { "height", Lbtui_height}, { NULL, NULL} diff --git a/Lua/test.lua b/Lua/test.lua index 81590ae..6e962f2 100644 --- a/Lua/test.lua +++ b/Lua/test.lua @@ -6,8 +6,19 @@ btui(function(bt) while key ~= "q" and key ~= "Ctrl-c" do bt:clear() bt:move(x, y) - bt:print("Pressed: ", key) + bt:withbg(150,225,80, function() + bt:withfg(0,0,0, function() + bt:print(" Pressed: ", key, " ") + end) + end) if key == "e" then error("ERR MESSAGE") end + + local title = "Lua BTUI Demo" + bt:withfg(100,200,255, function() + bt:move(math.floor((bt:width()-#title)/2), 0) + bt:print(title) + end) + local s = ("Size: (%dx%d)"):format(bt:width(), bt:height()) bt:move(bt:width()-#s, bt:height()-1) bt:print(s) diff --git a/btui.h b/btui.h index 41eca23..2967278 100644 --- a/btui.h +++ b/btui.h @@ -57,7 +57,7 @@ static btui_t current_bt; #define T_OFF(opt) "\033[?" opt "l" static const char *TUI_ENTER = T_OFF(T_SHOW_CURSOR ";" T_WRAP) T_ON(T_ALT_SCREEN ";" T_MOUSE_XY ";" T_MOUSE_CELL ";" T_MOUSE_SGR); -static const char *TUI_LEAVE = T_ON(T_SHOW_CURSOR ";" T_WRAP) T_OFF(T_ALT_SCREEN ";" T_MOUSE_XY ";" T_MOUSE_CELL ";" T_MOUSE_SGR); +static const char *TUI_LEAVE = T_ON(T_SHOW_CURSOR ";" T_WRAP) T_OFF(T_ALT_SCREEN ";" T_MOUSE_XY ";" T_MOUSE_CELL ";" T_MOUSE_SGR) "\033[0m"; // Maximum time in milliseconds between double clicks #ifndef DOUBLECLICK_THRESHOLD @@ -534,7 +534,7 @@ const unsigned long BTUI_FG_MAGENTA = 1ul << 35; const unsigned long BTUI_FG_CYAN = 1ul << 36; const unsigned long BTUI_FG_WHITE = 1ul << 37; // 38: 256/24bit color -const unsigned long BTUI_NO_FG = 1ul << 39; +const unsigned long BTUI_FG_NORMAL = 1ul << 39; const unsigned long BTUI_BG_BLACK = 1ul << 40; const unsigned long BTUI_BG_RED = 1ul << 41; const unsigned long BTUI_BG_GREEN = 1ul << 42; @@ -544,7 +544,7 @@ const unsigned long BTUI_BG_MAGENTA = 1ul << 45; const unsigned long BTUI_BG_CYAN = 1ul << 46; const unsigned long BTUI_BG_WHITE = 1ul << 47; // 48: 256/24bit color -const unsigned long BTUI_NO_BG = 1ul << 49; +const unsigned long BTUI_BG_NORMAL = 1ul << 49; const unsigned long BTUI_FRAMED = 1ul << 51; const unsigned long BTUI_ENCIRCLED = 1ul << 52; const unsigned long BTUI_OVERLINED = 1ul << 53;