summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-11-28 23:18:50 +0100
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>2015-01-25 17:00:11 +0900
commit5f6b5feac6d55e69c01858b314da6184c5f547c5 (patch)
treec99e15b494c4e8ad62457cd8a25d5c29883d43cf
parente2a4fde6de8e2173160746a5e687b9a5d2d387c9 (diff)
downloadmpv-5f6b5feac6d55e69c01858b314da6184c5f547c5.tar.bz2
mpv-5f6b5feac6d55e69c01858b314da6184c5f547c5.tar.xz
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.
-rw-r--r--DOCS/man/lua.rst4
-rw-r--r--TOOLS/lua/observe-all.lua10
-rw-r--r--player/lua/defaults.lua37
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 97b6616f4e..5dc101c75a 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 {}