summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-02-10 21:02:32 +0000
committerubq323 <ubq323@ubq323.website>2024-02-10 21:02:32 +0000
commitf964540c87b0efc3e938461b164270155b932b59 (patch)
treeb75ee01e71b32fcb394cd4b463e9d66bfa6c8b9a
parentf16720db3060a0e9d1f069bd0e7b900809db90cf (diff)
print type of userdata, and separate multiple values with tab instead of newlineHEADtrunk
-rw-r--r--pprint.lua25
1 files 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