about summary refs log tree commit diff stats
path: root/src/lcurseslib.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-05 10:26:47 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-11-05 10:30:07 -0700
commit8552ad4ced8ccd0cf5276bf6d03f0c43028be8af (patch)
tree1de9c8d61dac52cdb11e9103bbb197bf85f4f99c /src/lcurseslib.c
parent37b05c2957657ca618dfc183a909705b32b7adc5 (diff)
downloadteliva-8552ad4ced8ccd0cf5276bf6d03f0c43028be8af.tar.gz
starting on curses library
First piece of working new vocabulary:
  print(curses:cols())

Just pulling in code from lcurses for the most part. But I'm trying to
understand its internals as I gradually add them in, after my more blunt
first approach of packaging up lcurses/ext failed.

Overall plan for Teliva's API:
- start out with a 'curses' library that does what people who are used
  to ncurses/lcurses expect.
- over time create a more opinionated library called 'screen' or
  'window' or something.
Diffstat (limited to 'src/lcurseslib.c')
-rw-r--r--src/lcurseslib.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/lcurseslib.c b/src/lcurseslib.c
new file mode 100644
index 0000000..d8667fc
--- /dev/null
+++ b/src/lcurseslib.c
@@ -0,0 +1,40 @@
+#include <ncurses.h>
+
+#include "lua.h"
+
+#include "lauxlib.h"
+#include "lualib.h"
+
+
+static int Pcols(lua_State *L) {
+  lua_pushinteger(L, COLS);
+  return 1;
+}
+
+
+static const struct luaL_Reg curseslib [] = {
+  {"cols", Pcols},
+  {NULL, NULL}
+};
+
+
+static void curses_newwin (lua_State *L, WINDOW *nw) {
+  if (nw) {
+    WINDOW **w = lua_newuserdata(L, sizeof(WINDOW*));
+    luaL_getmetatable(L, "meta.window");
+    lua_setmetatable(L, -2);
+    *w = nw;
+  }
+  else {
+    lua_pushliteral(L, "failed to create window");
+    lua_error(L);
+  }
+}
+
+
+LUALIB_API int luaopen_curses (lua_State *L) {
+  luaL_register(L, "curses", curseslib);
+  curses_newwin(L, stdscr);
+  return 1;
+}
+