From 9a4d5574a89c4a7842211238d8b9b30b905cc056 Mon Sep 17 00:00:00 2001 From: ame Date: Mon, 18 Dec 2023 13:55:13 -0600 Subject: config and io functions --- src/io.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'src/io.c') diff --git a/src/io.c b/src/io.c index 6d07a44..9b84804 100644 --- a/src/io.c +++ b/src/io.c @@ -1,9 +1,9 @@ #include "io.h" #include "stdlib.h" #include "stdio.h" -#include "string.h" +#include "config.h" #include "stdint.h" -#include "unistd.h" +#include "table.h" int l_readfile(lua_State* L){ size_t len; @@ -75,3 +75,54 @@ int l_error(lua_State* L){ return 0; } +void print_indentation(int i){ + for(int z = 0; z < i; z++) printf(" "); +} + +void i_pprint(lua_State* L, int indent){ + int last_idx = lua_gettop(L); + const char* type = lua_typename(L,lua_type(L,-1)); + int t = lua_type(L,-1); + switch(lua_type(L,-1)){ + case LUA_TTABLE: + print_indentation(indent); + if(indent >= _max_depth && _max_depth >= 0) {printf("{"color_gray"..."color_reset"}"); break;} + int skip = i_len(L,last_idx) < _start_nl_at; + printf("{"); + if(!skip) printf("\n"); + lua_pushnil(L); + for(;lua_next(L,last_idx) != 0;){ + if(lua_type(L,-2) == LUA_TSTRING){ + print_indentation(indent); + printf(" %s"color_gray":"color_reset, lua_tostring(L,-2)); + } + i_pprint(L,indent+1); + printf(","); + if(!skip) printf("\n"); + + lua_pop(L,1); + } + + print_indentation(indent); + printf("}"); + break; + case LUA_TSTRING: + print_indentation(indent); + printf("\"%s\"", lua_tostring(L,-1)); + break; + default: + print_indentation(indent); + printf(color_yellow"%s"color_reset, lua_tostring(L,-1)); + } + + if(_print_type){ + if(lua_istable(L,last_idx)) printf(color_gray" : <%s:%lu>"color_reset,type,i_len(L,last_idx)); + else printf(color_gray" : <%s>"color_reset,type); + } + +} +int l_pprint(lua_State* L){ + i_pprint(L,0); + printf("\n"); + return 0; +} -- cgit v1.2.3