From f964540c87b0efc3e938461b164270155b932b59 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 10 Feb 2024 21:02:32 +0000 Subject: print type of userdata, and separate multiple values with tab instead of newline --- pprint.lua | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pprint.lua b/pprint.lua index efb3e2e..ef2ce70 100644 --- a/pprint.lua +++ b/pprint.lua @@ -271,6 +271,23 @@ function pprint.pformat(obj, option, printer) end end + local function make_userdata_formatter(has_cache) + local function utype(v) + local mt = getmetatable(v) + if not mt then return '?' end + return mt.__name or '?' + end + if has_cache then + return function(v) + return string.format('[[userdata <%s> %d]]', utype(v), cache.userdata[v]) + end + else + return function (v) + return string.format('[[userdata <%s>]]', utype(v)) + end + end + end + local function string_formatter(s, force_long_quote) local s, quote = escape(s) local quote_len = force_long_quote and 4 or 2 @@ -438,7 +455,8 @@ function pprint.pformat(obj, option, printer) formatter['number'] = option.show_number and number_formatter or nop_formatter -- need to handle math.huge formatter['function'] = option.show_function and make_fixed_formatter('function', option.object_cache) or nop_formatter formatter['thread'] = option.show_thread and make_fixed_formatter('thread', option.object_cache) or nop_formatter - formatter['userdata'] = option.show_userdata and make_fixed_formatter('userdata', option.object_cache) or nop_formatter + -- formatter['userdata'] = option.show_userdata and make_fixed_formatter('userdata', option.object_cache) or nop_formatter + formatter['userdata'] = option.show_userdata and make_userdata_formatter(option.object_cache) or nop_formatter formatter['string'] = option.show_string and string_formatter or nop_formatter formatter['table'] = option.show_table and table_formatter or nop_formatter @@ -465,7 +483,10 @@ function pprint.pprint( ... ) local len = select('#', ...) for ix = 1,len do pprint.pformat(args[ix], nil, io.write) - io.write('\n') + io.write('\t') + end + if len > 0 then + io.write('\n') end end -- cgit v1.2.3