diff options
author | ubq323 <ubq323@ubq323.website> | 2024-02-10 21:02:32 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-02-10 21:02:32 +0000 |
commit | f964540c87b0efc3e938461b164270155b932b59 (patch) | |
tree | b75ee01e71b32fcb394cd4b463e9d66bfa6c8b9a | |
parent | f16720db3060a0e9d1f069bd0e7b900809db90cf (diff) |
print type of userdata, and separate multiple values with tab instead of newline
-rw-r--r-- | pprint.lua | 25 |
1 files changed, 23 insertions, 2 deletions
@@ -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 |