about summary refs log tree commit diff stats
path: root/src/lua.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-13 16:33:59 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-13 16:36:12 -0800
commitc34db20756736f5730c3b95b13747fd24544befc (patch)
tree6be658c48afd95ed7b6cb52e978c7eb4eedfafbb /src/lua.c
parent7a24bed432a480b797da710f939aa46bd7e608c5 (diff)
downloadteliva-c34db20756736f5730c3b95b13747fd24544befc.tar.gz
very rudimentary definition browser
Never shows definitions that were never called. Including non-functions.
Diffstat (limited to 'src/lua.c')
-rw-r--r--src/lua.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/lua.c b/src/lua.c
index 88052d0..500f64d 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -378,19 +378,41 @@ void load_editor_buffer_to_current_definition_in_image(lua_State *L) {
 }
 
 
-void editImage (lua_State *L) {
-  save_to_current_definition_and_editor_buffer(L, "main");
+void editImage (lua_State *L, const char *definition) {
+  save_to_current_definition_and_editor_buffer(L, definition);
   editBuffer(L, /*status message*/ "");
   load_editor_buffer_to_current_definition_in_image(L);
 }
 
 
+void browseImage (lua_State *L) {
+  clear();
+  luaL_newmetatable(L, "__teliva_call_graph_depth");
+  int cgt = lua_gettop(L);
+  int y = 2;
+  for (lua_pushnil(L); lua_next(L, cgt) != 0;) {
+    const char* function_name = lua_tostring(L, -2);
+    int depth = lua_tointeger(L, -1);
+    mvprintw(y, 0, "%s: %d", function_name, depth);
+    ++y;
+    lua_pop(L, 1);  // pop value, leave key on stack for next iteration
+  }
+  int maxy, maxx;
+  getmaxyx(stdscr, maxy, maxx);
+  (void)maxx;  // unused
+  mvaddstr(maxy-1, 0, "edit what? ");
+  char definition[64] = {0};
+  getnstr(definition, 60);
+  editImage(L, definition);
+}
+
+
 void switch_to_editor (lua_State *L, const char *message) {
   endwin();
   if (Script_name)
     edit(L, Script_name, message);
   else
-    editImage(L);
+    browseImage(L);
   execv(Argv[0], Argv);
   /* never returns */
 }