From 3295caca3d21b20ccab19ab3420d07a139b4f200 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 28 Nov 2014 23:18:50 +0100 Subject: lua: add a function that formats Lua values as strings Yep, Lua is so crappy that the stdlib doesn't provide anything like this. Repurposes the undocumented mp.format_table() function and moves it to mp.utils. --- DOCS/man/lua.rst | 4 ++++ TOOLS/lua/observe-all.lua | 10 ++-------- player/lua/defaults.lua | 37 +++++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/DOCS/man/lua.rst b/DOCS/man/lua.rst index 3c0d104e40..57c1c35e5d 100644 --- a/DOCS/man/lua.rst +++ b/DOCS/man/lua.rst @@ -623,6 +623,10 @@ strictly part of the guaranteed API. trailing text is returned as 3rd return value. (The 3rd return value is always there, but with ``trail`` set, no error is raised.) +``utils.to_string(v)`` + Turn the given value into a string. Formats tables and their contents. This + doesn't do anything special; it is only needed because Lua is terrible. + Events ------ diff --git a/TOOLS/lua/observe-all.lua b/TOOLS/lua/observe-all.lua index 68e09773c6..9bfdf44992 100644 --- a/TOOLS/lua/observe-all.lua +++ b/TOOLS/lua/observe-all.lua @@ -1,16 +1,10 @@ -- Test script for property change notification mechanism. -function format_property_val(v) - if type(v) == "table" then - return mp.format_table(v) -- undocumented function; might be removed - else - return tostring(v) - end -end +local utils = require("mp.utils") for i,name in ipairs(mp.get_property_native("property-list")) do mp.observe_property(name, "native", function(name, val) print("property '" .. name .. "' changed to '" .. - format_property_val(val) .. "'") + utils.to_string(val) .. "'") end) end diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index ff70352011..0608526d0e 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -485,7 +485,9 @@ function mp.add_hook(name, pri, cb) mp.commandv("hook_add", name, id, pri) end -function mp.format_table(t, set) +local mp_utils = package.loaded["mp.utils"] + +function mp_utils.format_table(t, set) if not set then set = { [t] = true } end @@ -508,30 +510,33 @@ function mp.format_table(t, set) vals[#keys] = v end end - local function fmtval(v) - if type(v) == "string" then - return "\"" .. v .. "\"" - elseif type(v) == "table" then - if set[v] then - return "[cycle]" - end - set[v] = true - return mp.format_table(v, set) - else - return tostring(v) - end - end for i = 1, #keys do if #res > 1 then res = res .. ", " end if i > arr then - res = res .. fmtval(keys[i]) .. " = " + res = res .. mp_utils.to_string(keys[i], set) .. " = " end - res = res .. fmtval(vals[i]) + res = res .. mp_utils.to_string(vals[i], set) end res = res .. "}" return res end +function mp_utils.to_string(v, set) + if type(v) == "string" then + return "\"" .. v .. "\"" + elseif type(v) == "table" then + if set then + if set[v] then + return "[cycle]" + end + set[v] = true + end + return mp_utils.format_table(v, set) + else + return tostring(v) + end +end + return {} -- cgit v1.2.3