about summary refs log tree commit diff stats
path: root/src/menu.c
blob: 72f95ba020cf76c7bb6d277c6f571a5ead138ce9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <ncurses.h>
#include <string.h>

#include "lua.h"
#include "lauxlib.h"
#include "teliva.h"


int menu_column = 0;
void draw_string_on_menu (const char* s) {
  mvaddstr(LINES-1, menu_column, " ");
  ++menu_column;
  mvaddstr(LINES-1, menu_column, s);
  menu_column += strlen(s);
  mvaddstr(LINES-1, menu_column, " ");
  ++menu_column;
}
void draw_menu_item (const char* key, const char* name) {
  attroff(A_REVERSE);
  draw_string_on_menu(key);
  attron(A_REVERSE);
  draw_string_on_menu(name);
}

void draw_menu (lua_State *L) {
  attron(A_BOLD|A_REVERSE|COLOR_PAIR(COLOR_PAIR_MENU));
  for (int x = 0; x < COLS; ++x)
    mvaddch(LINES-1, x, ' ');
  menu_column = 2;
  draw_menu_item("^x", "exit");
  draw_menu_item("^e", "edit");

  /* render any app-specific items */
  lua_getglobal(L, "menu");
  int table = lua_gettop(L);
  if (lua_istable(L, -1)) {
    for (int i = 1; i <= luaL_getn(L, table); ++i) {
      lua_rawgeti(L, table, i);
      int menu_item = lua_gettop(L);
      lua_rawgeti(L, menu_item, 1);  /* key */
      lua_rawgeti(L, menu_item, 2);  /* value */
      draw_menu_item(lua_tostring(L, -2), lua_tostring(L, -1));
      lua_pop(L, 3);
    }
  }

  lua_pop(L, 1);
  attrset(A_NORMAL);
}