about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-10-22 20:53:18 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-10-22 20:54:34 -0700
commit1445cbc5b190a93e1d2bfc468ef5958a0ad3d1d5 (patch)
tree222917c1fa5c232ea105427a4eede0d1d1b40766 /src
parentd92a484b915e8c5b6248bc553d9906dc1f6de2d0 (diff)
downloadteliva-1445cbc5b190a93e1d2bfc468ef5958a0ad3d1d5.tar.gz
ncurses I/O in lua.c
Currently working:
  > print(1)
  1
   > print(2)
  2

Print's newline doesn't return to column 0 yet.

Ctrl-d no longer works. Ctrl-c exits cleanly.
Diffstat (limited to 'src')
-rw-r--r--src/lua.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/src/lua.c b/src/lua.c
index 59be9de..7ab07d1 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -5,6 +5,7 @@
 */
 
 
+#include <ncurses.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -40,7 +41,7 @@ static void laction (int i) {
 
 
 static void print_usage (void) {
-  fprintf(stderr,
+  printw(
   "usage: %s [options] [script [args]].\n"
   "Available options are:\n"
   "  -e stat  execute string " LUA_QL("stat") "\n"
@@ -51,14 +52,14 @@ static void print_usage (void) {
   "  -        execute stdin and stop handling options\n"
   ,
   progname);
-  fflush(stderr);
+  refresh();
 }
 
 
 static void l_message (const char *pname, const char *msg) {
-  if (pname) fprintf(stderr, "%s: ", pname);
-  fprintf(stderr, "%s\n", msg);
-  fflush(stderr);
+  if (pname) printw("%s: ", pname);
+  printw("%s\n", msg);
+  refresh();
 }
 
 
@@ -179,10 +180,9 @@ static int pushline (lua_State *L, int firstline) {
   char *b = buffer;
   size_t l;
   const char *prmt = get_prompt(L, firstline);
-  fputs(prmt, stdout);
-  fflush(stdout);
-  if (fgets(buffer, LUA_MAXINPUT, stdin) == NULL)
-    return 0;  /* no input */
+  addstr(prmt);
+  refresh();
+  getnstr(b, LUA_MAXINPUT);
   l = strlen(b);
   if (l > 0 && b[l-1] == '\n')  /* line ends with newline? */
     b[l-1] = '\0';  /* remove it */
@@ -231,7 +231,7 @@ static void dotty (lua_State *L) {
   }
   lua_settop(L, 0);  /* clear stack */
   fputs("\n", stdout);
-  fflush(stdout);
+  refresh();
   progname = oldprogname;
 }
 
@@ -352,6 +352,7 @@ static int pmain (lua_State *L) {
   script = collectargs(argv, &has_i, &has_v, &has_e);
   if (script < 0) {  /* invalid args? */
     print_usage();
+    getch();
     s->status = 1;
     return 0;
   }
@@ -382,11 +383,15 @@ int main (int argc, char **argv) {
     l_message(argv[0], "cannot create state: not enough memory");
     return EXIT_FAILURE;
   }
+  initscr();
+  echo();
   s.argc = argc;
   s.argv = argv;
   status = lua_cpcall(L, &pmain, &s);
   report(L, status);
   lua_close(L);
+  getch();
+  endwin();
   return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
 }