Added more functionality to Lua.
This commit is contained in:
parent
f14957613b
commit
a9dff092db
3
C/test.c
3
C/test.c
@ -9,9 +9,10 @@ int main(void)
|
||||
int x = 0, y = 0;
|
||||
while (!done) {
|
||||
const char *title = "BTUI C Demo";
|
||||
btui_set_attributes(bt, BTUI_FG_BLUE | BTUI_BOLD);
|
||||
btui_set_attributes(bt, BTUI_FG_BLUE | BTUI_BOLD | BTUI_UNDERLINE);
|
||||
btui_move_cursor(bt, (bt->width - (int)strlen(title)) / 2, 0);
|
||||
btui_printf(bt, "%s", title);
|
||||
btui_set_attributes(bt, BTUI_NORMAL);
|
||||
|
||||
btui_set_attributes(bt, BTUI_FG_NORMAL | BTUI_FAINT);
|
||||
btui_move_cursor(bt, 0, bt->height-1);
|
||||
|
198
Lua/lbtui.c
198
Lua/lbtui.c
@ -17,7 +17,7 @@
|
||||
#define lua_isinteger(L, i) lua_isnumber(L, i)
|
||||
#endif
|
||||
|
||||
const int BTUI_METATABLE;
|
||||
const int BTUI_METATABLE, BTUI_ATTRIBUTES, BTUI_INVERSE_ATTRIBUTES;
|
||||
|
||||
static int Lbtui_enable(lua_State *L)
|
||||
{
|
||||
@ -136,6 +136,68 @@ static int Lbtui_withbg(lua_State *L)
|
||||
return lua_gettop(L) - top;
|
||||
}
|
||||
|
||||
static int Lbtui_setattributes(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);
|
||||
lua_pushlightuserdata(L, (void*)&BTUI_ATTRIBUTES);
|
||||
int attr_table = lua_gettop(L);
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
lua_Unsigned attrs = 0;
|
||||
for (int i = 2; i <= top; i++) {
|
||||
lua_pushvalue(L, i);
|
||||
lua_gettable(L, attr_table);
|
||||
if (lua_isnil(L, -1))
|
||||
luaL_argerror(L, i, "invalid attribute");
|
||||
attrs |= (lua_Unsigned)lua_tointeger(L, -1);
|
||||
}
|
||||
btui_set_attributes(*bt, attrs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Lbtui_unsetattributes(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);
|
||||
lua_pushlightuserdata(L, (void*)&BTUI_INVERSE_ATTRIBUTES);
|
||||
int attr_table = lua_gettop(L);
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
lua_Unsigned attrs = 0;
|
||||
for (int i = 2; i <= top; i++) {
|
||||
lua_pushvalue(L, i);
|
||||
lua_gettable(L, attr_table);
|
||||
if (lua_isnil(L, -1))
|
||||
luaL_argerror(L, i, "invalid attribute");
|
||||
attrs |= (lua_Unsigned)lua_tointeger(L, -1);
|
||||
}
|
||||
btui_set_attributes(*bt, attrs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Lbtui_withattributes(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);
|
||||
lua_pushcfunction(L, Lbtui_setattributes);
|
||||
for (int i = 1; i < top; i++) {
|
||||
lua_pushvalue(L, i);
|
||||
}
|
||||
lua_call(L, top - 1, 0);
|
||||
lua_pushvalue(L, top);
|
||||
int status = lua_pcall(L, 0, LUA_MULTRET, 0);
|
||||
int top2 = lua_gettop(L);
|
||||
lua_pushcfunction(L, Lbtui_unsetattributes);
|
||||
for (int i = 1; i < top; i++) {
|
||||
lua_pushvalue(L, i);
|
||||
}
|
||||
lua_call(L, top - 1, 0);
|
||||
if (status != LUA_OK)
|
||||
lua_error(L);
|
||||
return top2 - top;
|
||||
}
|
||||
|
||||
static int Lbtui_width(lua_State *L)
|
||||
{
|
||||
@ -177,22 +239,132 @@ static int Lbtui_tostring(lua_State *L)
|
||||
|
||||
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},
|
||||
{ "width", Lbtui_width},
|
||||
{ "height", Lbtui_height},
|
||||
{ NULL, NULL}
|
||||
{"__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;
|
||||
} btui_attributes[] =
|
||||
{
|
||||
{"normal", BTUI_NORMAL},
|
||||
{"bold", BTUI_BOLD},
|
||||
{"faint", BTUI_FAINT},
|
||||
{"dim", BTUI_FAINT},
|
||||
{"italic", BTUI_ITALIC},
|
||||
{"underline", BTUI_UNDERLINE},
|
||||
{"blink_slow", BTUI_BLINK_SLOW},
|
||||
{"blink_fast", BTUI_BLINK_FAST},
|
||||
{"reverse", BTUI_REVERSE},
|
||||
{"conceal", BTUI_CONCEAL},
|
||||
{"strikethrough", BTUI_STRIKETHROUGH},
|
||||
{"fraktur", BTUI_FRAKTUR},
|
||||
{"double_underline", BTUI_DOUBLE_UNDERLINE},
|
||||
{"no_bold_or_faint", BTUI_NO_BOLD_OR_FAINT},
|
||||
{"no_italic_or_fraktur", BTUI_NO_ITALIC_OR_FRAKTUR},
|
||||
{"no_underline", BTUI_NO_UNDERLINE},
|
||||
{"no_blink", BTUI_NO_BLINK},
|
||||
{"no_reverse", BTUI_NO_REVERSE},
|
||||
{"no_conceal", BTUI_NO_CONCEAL},
|
||||
{"no_strikethrough", BTUI_NO_STRIKETHROUGH},
|
||||
{"fg_black", BTUI_FG_BLACK},
|
||||
{"fg_red", BTUI_FG_RED},
|
||||
{"fg_green", BTUI_FG_GREEN},
|
||||
{"fg_yellow", BTUI_FG_YELLOW},
|
||||
{"fg_blue", BTUI_FG_BLUE},
|
||||
{"fg_magenta", BTUI_FG_MAGENTA},
|
||||
{"fg_cyan", BTUI_FG_CYAN},
|
||||
{"fg_white", BTUI_FG_WHITE},
|
||||
{"fg_normal", BTUI_FG_NORMAL},
|
||||
{"bg_black", BTUI_BG_BLACK},
|
||||
{"bg_red", BTUI_BG_RED},
|
||||
{"bg_green", BTUI_BG_GREEN},
|
||||
{"bg_yellow", BTUI_BG_YELLOW},
|
||||
{"bg_blue", BTUI_BG_BLUE},
|
||||
{"bg_magenta", BTUI_BG_MAGENTA},
|
||||
{"bg_cyan", BTUI_BG_CYAN},
|
||||
{"bg_white", BTUI_BG_WHITE},
|
||||
{"bg_normal", BTUI_BG_NORMAL},
|
||||
{"framed", BTUI_FRAMED},
|
||||
{"encircled", BTUI_ENCIRCLED},
|
||||
{"overlined", BTUI_OVERLINED},
|
||||
{NULL, 0}
|
||||
};
|
||||
|
||||
static struct {
|
||||
const char* name;
|
||||
lua_Unsigned code;
|
||||
} btui_inverse_attributes[] =
|
||||
{
|
||||
{"normal", BTUI_NORMAL},
|
||||
{"bold", BTUI_NO_BOLD_OR_FAINT},
|
||||
{"faint", BTUI_NO_BOLD_OR_FAINT},
|
||||
{"dim", BTUI_NO_BOLD_OR_FAINT},
|
||||
{"italic", BTUI_NO_ITALIC_OR_FRAKTUR},
|
||||
{"underline", BTUI_NO_UNDERLINE},
|
||||
{"blink_slow", BTUI_NO_BLINK},
|
||||
{"blink_fast", BTUI_NO_BLINK},
|
||||
{"reverse", BTUI_NO_REVERSE},
|
||||
{"conceal", BTUI_NO_CONCEAL},
|
||||
{"strikethrough", BTUI_NO_STRIKETHROUGH},
|
||||
{"fraktur", BTUI_NO_ITALIC_OR_FRAKTUR},
|
||||
{"double_underline", BTUI_NO_UNDERLINE},
|
||||
{"fg_black", BTUI_FG_NORMAL},
|
||||
{"fg_red", BTUI_FG_NORMAL},
|
||||
{"fg_green", BTUI_FG_NORMAL},
|
||||
{"fg_yellow", BTUI_FG_NORMAL},
|
||||
{"fg_blue", BTUI_FG_NORMAL},
|
||||
{"fg_magenta", BTUI_FG_NORMAL},
|
||||
{"fg_cyan", BTUI_FG_NORMAL},
|
||||
{"fg_white", BTUI_FG_NORMAL},
|
||||
{"fg_normal", BTUI_FG_NORMAL},
|
||||
{"bg_black", BTUI_BG_NORMAL},
|
||||
{"bg_red", BTUI_BG_NORMAL},
|
||||
{"bg_green", BTUI_BG_NORMAL},
|
||||
{"bg_yellow", BTUI_BG_NORMAL},
|
||||
{"bg_blue", BTUI_BG_NORMAL},
|
||||
{"bg_magenta", BTUI_BG_NORMAL},
|
||||
{"bg_cyan", BTUI_BG_NORMAL},
|
||||
{"bg_white", BTUI_BG_NORMAL},
|
||||
{"bg_normal", BTUI_BG_NORMAL},
|
||||
{NULL, 0}
|
||||
};
|
||||
|
||||
LUALIB_API int luaopen_btui(lua_State *L)
|
||||
{
|
||||
// Set up attributes
|
||||
lua_pushlightuserdata(L, (void*)&BTUI_ATTRIBUTES);
|
||||
lua_createtable(L, 0, 50);
|
||||
for (int i = 0; btui_attributes[i].name; i++) {
|
||||
lua_pushinteger(L, (lua_Integer)btui_attributes[i].code);
|
||||
lua_setfield(L, -2, btui_attributes[i].name);
|
||||
}
|
||||
lua_settable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
// Inverse attributes
|
||||
lua_pushlightuserdata(L, (void*)&BTUI_INVERSE_ATTRIBUTES);
|
||||
lua_createtable(L, 0, 50);
|
||||
for (int i = 0; btui_inverse_attributes[i].name; i++) {
|
||||
lua_pushinteger(L, (lua_Integer)btui_inverse_attributes[i].code);
|
||||
lua_setfield(L, -2, btui_inverse_attributes[i].name);
|
||||
}
|
||||
lua_settable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
// Set up BTUI metatable
|
||||
lua_pushlightuserdata(L, (void*)&BTUI_METATABLE);
|
||||
lua_createtable(L, 0, 16);
|
||||
luaL_register(L, NULL, Rclass_metamethods);
|
||||
|
@ -14,9 +14,11 @@ btui(function(bt)
|
||||
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)
|
||||
bt:withattributes("bold", "underline", function()
|
||||
bt:withfg(100,200,255, function()
|
||||
bt:move(math.floor((bt:width()-#title)/2), 0)
|
||||
bt:print(title)
|
||||
end)
|
||||
end)
|
||||
|
||||
local s = ("Size: (%dx%d)"):format(bt:width(), bt:height())
|
||||
|
85
btui.h
85
btui.h
@ -30,7 +30,7 @@ int btui_getkey(btui_t *bt, int *mouse_x, int *mouse_y);
|
||||
int btui_move_cursor(btui_t *bt, int x, int y);
|
||||
char *btui_keyname(int key, char *buf);
|
||||
int btui_keynamed(const char *name);
|
||||
int btui_set_attributes(btui_t *bt, unsigned long attrs);
|
||||
int btui_set_attributes(btui_t *bt, unsigned long long attrs);
|
||||
int btui_set_fg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b);
|
||||
int btui_set_bg_rgb(btui_t *bt, unsigned char r, unsigned char g, unsigned char b);
|
||||
int btui_set_fg_hex(btui_t *bt, int hex);
|
||||
@ -506,50 +506,51 @@ int btui_set_bg_hex(btui_t *bt, int hex)
|
||||
(hex >> 16) & 0xFF, (hex >> 8) & 0xFF, hex & 0xFF);
|
||||
}
|
||||
|
||||
const unsigned long BTUI_NORMAL = 1ul << 0;
|
||||
const unsigned long BTUI_BOLD = 1ul << 1;
|
||||
const unsigned long BTUI_FAINT = 1ul << 2;
|
||||
const unsigned long BTUI_ITALIC = 1ul << 3;
|
||||
const unsigned long BTUI_UNDERLINE = 1ul << 4;
|
||||
const unsigned long BTUI_BLINK_SLOW = 1ul << 5;
|
||||
const unsigned long BTUI_BLINK_FAST = 1ul << 6;
|
||||
const unsigned long BTUI_REVERSE = 1ul << 7;
|
||||
const unsigned long BTUI_CONCEAL = 1ul << 8;
|
||||
const unsigned long BTUI_STRIKETHROUGH = 1ul << 9;
|
||||
const unsigned long BTUI_FRAKTUR = 1ul << 20;
|
||||
const unsigned long BTUI_DOUBLE_UNDERLINE = 1ul << 21;
|
||||
const unsigned long BTUI_NO_BOLD_OR_FAINT = 1ul << 22;
|
||||
const unsigned long BTUI_NO_ITALIC_OR_FRAKTUR = 1ul << 23;
|
||||
const unsigned long BTUI_NO_UNDERLINE = 1ul << 24;
|
||||
const unsigned long BTUI_NO_BLINK = 1ul << 25;
|
||||
const unsigned long BTUI_NO_REVERSE = 1ul << 27;
|
||||
const unsigned long BTUI_NO_CONCEAL = 1ul << 28;
|
||||
const unsigned long BTUI_NO_STRIKETHROUGH = 1ul << 29;
|
||||
const unsigned long BTUI_FG_BLACK = 1ul << 30;
|
||||
const unsigned long BTUI_FG_RED = 1ul << 31;
|
||||
const unsigned long BTUI_FG_GREEN = 1ul << 32;
|
||||
const unsigned long BTUI_FG_YELLOW = 1ul << 33;
|
||||
const unsigned long BTUI_FG_BLUE = 1ul << 34;
|
||||
const unsigned long BTUI_FG_MAGENTA = 1ul << 35;
|
||||
const unsigned long BTUI_FG_CYAN = 1ul << 36;
|
||||
const unsigned long BTUI_FG_WHITE = 1ul << 37;
|
||||
typedef unsigned long long attr_t;
|
||||
const attr_t BTUI_NORMAL = 1ul << 0;
|
||||
const attr_t BTUI_BOLD = 1ul << 1;
|
||||
const attr_t BTUI_FAINT = 1ul << 2;
|
||||
const attr_t BTUI_ITALIC = 1ul << 3;
|
||||
const attr_t BTUI_UNDERLINE = 1ul << 4;
|
||||
const attr_t BTUI_BLINK_SLOW = 1ul << 5;
|
||||
const attr_t BTUI_BLINK_FAST = 1ul << 6;
|
||||
const attr_t BTUI_REVERSE = 1ul << 7;
|
||||
const attr_t BTUI_CONCEAL = 1ul << 8;
|
||||
const attr_t BTUI_STRIKETHROUGH = 1ul << 9;
|
||||
const attr_t BTUI_FRAKTUR = 1ul << 20;
|
||||
const attr_t BTUI_DOUBLE_UNDERLINE = 1ul << 21;
|
||||
const attr_t BTUI_NO_BOLD_OR_FAINT = 1ul << 22;
|
||||
const attr_t BTUI_NO_ITALIC_OR_FRAKTUR = 1ul << 23;
|
||||
const attr_t BTUI_NO_UNDERLINE = 1ul << 24;
|
||||
const attr_t BTUI_NO_BLINK = 1ul << 25;
|
||||
const attr_t BTUI_NO_REVERSE = 1ul << 27;
|
||||
const attr_t BTUI_NO_CONCEAL = 1ul << 28;
|
||||
const attr_t BTUI_NO_STRIKETHROUGH = 1ul << 29;
|
||||
const attr_t BTUI_FG_BLACK = 1ul << 30;
|
||||
const attr_t BTUI_FG_RED = 1ul << 31;
|
||||
const attr_t BTUI_FG_GREEN = 1ul << 32;
|
||||
const attr_t BTUI_FG_YELLOW = 1ul << 33;
|
||||
const attr_t BTUI_FG_BLUE = 1ul << 34;
|
||||
const attr_t BTUI_FG_MAGENTA = 1ul << 35;
|
||||
const attr_t BTUI_FG_CYAN = 1ul << 36;
|
||||
const attr_t BTUI_FG_WHITE = 1ul << 37;
|
||||
// 38: 256/24bit color
|
||||
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;
|
||||
const unsigned long BTUI_BG_YELLOW = 1ul << 43;
|
||||
const unsigned long BTUI_BG_BLUE = 1ul << 44;
|
||||
const unsigned long BTUI_BG_MAGENTA = 1ul << 45;
|
||||
const unsigned long BTUI_BG_CYAN = 1ul << 46;
|
||||
const unsigned long BTUI_BG_WHITE = 1ul << 47;
|
||||
const attr_t BTUI_FG_NORMAL = 1ul << 39;
|
||||
const attr_t BTUI_BG_BLACK = 1ul << 40;
|
||||
const attr_t BTUI_BG_RED = 1ul << 41;
|
||||
const attr_t BTUI_BG_GREEN = 1ul << 42;
|
||||
const attr_t BTUI_BG_YELLOW = 1ul << 43;
|
||||
const attr_t BTUI_BG_BLUE = 1ul << 44;
|
||||
const attr_t BTUI_BG_MAGENTA = 1ul << 45;
|
||||
const attr_t BTUI_BG_CYAN = 1ul << 46;
|
||||
const attr_t BTUI_BG_WHITE = 1ul << 47;
|
||||
// 48: 256/24bit color
|
||||
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;
|
||||
const attr_t BTUI_BG_NORMAL = 1ul << 49;
|
||||
const attr_t BTUI_FRAMED = 1ul << 51;
|
||||
const attr_t BTUI_ENCIRCLED = 1ul << 52;
|
||||
const attr_t BTUI_OVERLINED = 1ul << 53;
|
||||
|
||||
int btui_set_attributes(btui_t *bt, unsigned long attrs)
|
||||
int btui_set_attributes(btui_t *bt, unsigned long long attrs)
|
||||
{
|
||||
int printed = fputs("\033[", bt->out);
|
||||
for (int i = 0; i < 64; i++) {
|
||||
|
Loading…
Reference in New Issue
Block a user