about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-05 19:38:43 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-11-05 19:43:40 -0700
commitee3f6e8a22d95836df438f4fc6419120b0a0ae20 (patch)
tree9d17a4edf7cd240ad3f8363b29cc48e8c72c4efc
parent6c9e26e07caa24e6f306cbf948255fc95c5ee7e4 (diff)
downloadteliva-ee3f6e8a22d95836df438f4fc6419120b0a0ae20.tar.gz
colors: init_pair/color_pair
-rw-r--r--hanoi.lua17
-rw-r--r--src/hanoi.lua16
-rw-r--r--src/lcurseslib.c26
-rw-r--r--src/lua.c1
4 files changed, 52 insertions, 8 deletions
diff --git a/hanoi.lua b/hanoi.lua
index 7174bc5..0ced2c7 100644
--- a/hanoi.lua
+++ b/hanoi.lua
@@ -1,7 +1,7 @@
 local curses = require "curses"
 
 
-tower = {{5, 4, 3, 2, 1}, {}, {}}
+tower = {{6, 5, 4, 3, 2}, {}, {}}
 
 
 local function len(array)
@@ -31,7 +31,9 @@ end
 local function render_disk(window, line, col, size)
   col = col-size+1
   for i=1,size do
-    window:mvaddstr(line, col, "--")
+    window:attron(curses.color_pair(1))
+    window:mvaddstr(line, col, "  ")
+    window:attroff(curses.color_pair(1))
     col = col+2
   end
 end
@@ -40,13 +42,17 @@ local function render_tower(window, line, col, tower_index, tower)
   window:attron(curses.A_BOLD)
   window:mvaddch(line+2, col, string.char(96+tower_index))
   window:attroff(curses.A_BOLD)
-  window:mvaddstr(line+1, col-3, "========")
+  window:attron(curses.color_pair(2))
+  window:mvaddstr(line+1, col-6, "              ")
+  window:attroff(curses.color_pair(2))
   for i, n in ipairs(tower) do
     render_disk(window, line, col, n)
     line = line - 1
   end
   for i=1,5-len(tower) do
-    window:mvaddstr(line, col, "||")
+    window:attron(curses.color_pair(2))
+    window:mvaddstr(line, col, "  ")
+    window:attroff(curses.color_pair(2))
     line = line - 1
   end
 end
@@ -83,6 +89,9 @@ end
 
 local function main()
   local window = curses.initscr()
+  curses.start_color()
+  curses.init_pair(1, 0, 2)
+  curses.init_pair(2, 0, 8)
 
   while true do
     render(window)
diff --git a/src/hanoi.lua b/src/hanoi.lua
index 4b3134f..09c69ce 100644
--- a/src/hanoi.lua
+++ b/src/hanoi.lua
@@ -1,7 +1,7 @@
 local curses = require "curses"
 
 
-tower = {{5, 4, 3, 2, 1}, {}, {}}
+tower = {{6, 5, 4, 3, 2}, {}, {}}
 
 
 local function len(array)
@@ -31,7 +31,9 @@ end
 local function render_disk(window, line, col, size)
   col = col-size+1
   for i=1,size do
-    window:mvaddstr(line, col, "--")
+    window:attron(curses.color_pair(1))
+    window:mvaddstr(line, col, "  ")
+    window:attroff(curses.color_pair(1))
     col = col+2
   end
 end
@@ -40,13 +42,17 @@ local function render_tower(window, line, col, tower_index, tower)
   window:attron(curses.A_BOLD)
   window:mvaddch(line+2, col, string.char(96+tower_index))
   window:attroff(curses.A_BOLD)
-  window:mvaddstr(line+1, col-3, "========")
+  window:attron(curses.color_pair(2))
+  window:mvaddstr(line+1, col-6, "              ")
+  window:attroff(curses.color_pair(2))
   for i, n in ipairs(tower) do
     render_disk(window, line, col, n)
     line = line - 1
   end
   for i=1,5-len(tower) do
-    window:mvaddstr(line, col, "||")
+    window:attron(curses.color_pair(2))
+    window:mvaddstr(line, col, "  ")
+    window:attroff(curses.color_pair(2))
     line = line - 1
   end
 end
@@ -83,6 +89,8 @@ end
 
 local function main()
   local window = curses.stdscr()
+  curses.init_pair(1, 0, 2)
+  curses.init_pair(2, 0, 8)
 
   while true do
     render(window)
diff --git a/src/lcurseslib.c b/src/lcurseslib.c
index e2ac021..6ec7dfe 100644
--- a/src/lcurseslib.c
+++ b/src/lcurseslib.c
@@ -51,6 +51,29 @@ static int Pstdscr (lua_State *L) {
 }
 
 
+static int Pcolor_pairs (lua_State *L) {
+  lua_pushinteger(L, COLOR_PAIRS);
+  return 1;
+}
+
+
+static int Pinit_pair (lua_State *L) {
+  int pair = checkinteger(L, 1, "int");
+  short f = checkinteger(L, 2, "int");
+  short b = checkinteger(L, 3, "int");
+  init_pair(pair, f, b);
+  return 1;
+}
+
+
+static int Pcolor_pair (lua_State *L)
+{
+  int n = checkinteger(L, 1, "int");
+  lua_pushinteger(L, COLOR_PAIR(n));
+  return 1;
+}
+
+
 static int Pgetch (lua_State *L) {
   int c = wgetch(stdscr);
   // TODO: handle menu here
@@ -62,7 +85,10 @@ static int Pgetch (lua_State *L) {
 
 
 static const struct luaL_Reg curseslib [] = {
+  {"color_pairs", Pcolor_pairs},
+  {"color_pair", Pcolor_pair},
   {"getch", Pgetch},
+  {"init_pair", Pinit_pair},
   {"refresh", Prefresh},
   {"stdscr", Pstdscr},
   {NULL, NULL}
diff --git a/src/lua.c b/src/lua.c
index ef44444..f773607 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -470,6 +470,7 @@ int main (int argc, char **argv) {
   luaL_register(L, NULL, array_methods);  /* register array_methods in metatable */
   luaL_register(L, "array", arraylib_functions);
   initscr();
+  start_color();
   draw_menu();
   echo();
   s.argc = argc;