mirror of https://github.com/mpv-player/mpv
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.
This commit is contained in:
parent
9666d48aa3
commit
3295caca3d
|
@ -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
|
||||
------
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {}
|
||||
|
|
Loading…
Reference in New Issue