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 19:49:36 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-11-05 19:50:12 -0700
commit5da9f270790060e01f6e0067a9852fb21e8e612c (patch)
tree1e61d505fcd0161f746f2f115ec71cb40c00e85d /src/lcurseslib.c
parentee3f6e8a22d95836df438f4fc6419120b0a0ae20 (diff)
downloadteliva-5da9f270790060e01f6e0067a9852fb21e8e612c.tar.gz
menu entry: cleanly exit
Diffstat (limited to 'src/lcurseslib.c')
-rw-r--r--src/lcurseslib.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/lcurseslib.c b/src/lcurseslib.c
index 6ec7dfe..4bdbee1 100644
--- a/src/lcurseslib.c
+++ b/src/lcurseslib.c
@@ -1,4 +1,5 @@
 #include <ncurses.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "lua.h"
@@ -7,15 +8,28 @@
 #include "lauxlib.h"
 #include "lualib.h"
 
+static void cleanup(void) {
+  if (!isendwin())
+  {
+    wclear(stdscr);
+    wrefresh(stdscr);
+    endwin();
+  }
+}
+
 
 void draw_menu (void) {
   attron(A_BOLD|A_REVERSE);
   for (int x = 0; x < COLS; ++x)
     mvaddch(LINES-1, x, ' ');
   attroff(A_REVERSE);
-  mvaddstr(LINES-1, 2, " ^e ");
+  mvaddstr(LINES-1, 2, " ^x ");
   attron(A_REVERSE);
-  mvaddstr(LINES-1, 6, " edit ");
+  mvaddstr(LINES-1, 6, " exit ");
+  attroff(A_REVERSE);
+  mvaddstr(LINES-1, 12, " ^e ");
+  attron(A_REVERSE);
+  mvaddstr(LINES-1, 16, " edit ");
   attroff(A_BOLD|A_REVERSE);
 }
 
@@ -76,9 +90,11 @@ static int Pcolor_pair (lua_State *L)
 
 static int Pgetch (lua_State *L) {
   int c = wgetch(stdscr);
-  // TODO: handle menu here
   if (c == ERR)
     return 0;
+  if (c == 24)  /* ctrl-x */
+    exit(0);
+  /* TODO: handle other standard menu hotkeys here */
   lua_pushinteger(L, c);
   return 1;
 }
@@ -331,6 +347,8 @@ LUALIB_API int luaopen_curses (lua_State *L) {
 
   lua_pushvalue(L, -2);
   register_curses_constants(L);
+
+  atexit(cleanup);
   return 1;
 }