From 3c4ce79c929a16602eecbb2a6600e1cea871439f Mon Sep 17 00:00:00 2001 From: ame Date: Tue, 26 Dec 2023 12:53:21 -0600 Subject: better pretty print --- .gitignore | 1 + docs/io.md | 2 +- src/config.c | 9 ++++++++- src/config.h | 2 ++ src/io.c | 30 ++++++++++++++++++++---------- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 140f8cf..4c0ec0e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.so +*.lua diff --git a/docs/io.md b/docs/io.md index 74fe3a6..443c978 100644 --- a/docs/io.md +++ b/docs/io.md @@ -18,7 +18,7 @@ llib.io.pprint({a = 5, b = {9, 9, 22}}) - max_depth (2) - maximum depth that will not be collapsed - start_nl_at (3) - maximum depth that will be kept in-line - collapse_all (0 | false) - skip all newlines - +- collapse_to_memory (1 | true) - print memory address instead of ... ### error/warn/log/debug 'accepts a string diff --git a/src/config.c b/src/config.c index 25919d1..0c8a813 100644 --- a/src/config.c +++ b/src/config.c @@ -6,6 +6,7 @@ int _print_type = 0; int _max_depth = 2; int _start_nl_at = 3; int _collapse_all = 0; +int _collapse_to_memory = 1; int _file_malloc_chunk = 512; @@ -24,7 +25,13 @@ int l_set(lua_State* L) { int ind = get_config_map(lua_tostring(L,-2)); if(ind != -1) { - *config_map[ind].value = lua_tonumber(L,-1); + int key; + if(lua_isboolean(L, -1)){ + key = lua_toboolean(L, -1); + } else { + key = lua_tonumber(L, -1); + } + *config_map[ind].value = key; lua_pushnumber(L, 1); return 1; } diff --git a/src/config.h b/src/config.h index d50774d..8de4399 100644 --- a/src/config.h +++ b/src/config.h @@ -5,6 +5,7 @@ extern int _print_type; extern int _max_depth; extern int _start_nl_at; extern int _collapse_all; +extern int _collapse_to_memory; extern int _file_malloc_chunk; @@ -19,6 +20,7 @@ static struct str_to_int config_map[] = { {"max_depth", &_max_depth}, {"collapse_all", &_collapse_all}, {"start_nl_at", &_start_nl_at}, + {"collapse_to_memory", &_collapse_to_memory}, {NULL,NULL} }; diff --git a/src/io.c b/src/io.c index ed59dfa..997b4cd 100644 --- a/src/io.c +++ b/src/io.c @@ -87,39 +87,49 @@ void print_indentation(int i){ for(int z = 0; z < i; z++) printf(" "); } -void i_pprint(lua_State* L, int indent){ +void i_pprint(lua_State* L, int indent, int skip_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;} + printf(" "); + if(indent >= _max_depth && _max_depth >= 0) { + printf("{"color_gray); + if(_collapse_to_memory) printf(" %p ",lua_topointer(L, -1)); + else printf(" ... "); + printf(color_reset"}"); + break; + } + int skip = i_len(L,last_idx) < _start_nl_at || _collapse_all; 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)); + if(!skip) print_indentation(indent); + printf(" %s"color_gray": "color_reset, lua_tostring(L,-2)); } - i_pprint(L,indent+1); + i_pprint(L,indent+1,1); printf(","); if(!skip) printf("\n"); lua_pop(L,1); } - print_indentation(indent); printf("}"); break; case LUA_TSTRING: - print_indentation(indent); + if(!skip_indent) print_indentation(indent); printf("\"%s\"", lua_tostring(L,-1)); break; + case LUA_TFUNCTION: + if(!skip_indent) print_indentation(indent); + printf(color_yellow"(fn)"color_reset); + break; default: - print_indentation(indent); + if(!skip_indent) print_indentation(indent); printf(color_yellow"%s"color_reset, lua_tostring(L,-1)); } @@ -130,7 +140,7 @@ void i_pprint(lua_State* L, int indent){ } int l_pprint(lua_State* L){ - i_pprint(L,0); + i_pprint(L,0,0); printf("\n"); return 0; } -- cgit v1.2.3