diff options
author | wm4 <wm4@nowhere> | 2014-11-28 23:18:50 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-11-29 00:03:46 +0100 |
commit | 3295caca3d21b20ccab19ab3420d07a139b4f200 (patch) | |
tree | b75ff7040553c889a9e98c84fc862765a148f963 /player | |
parent | 9666d48aa332a034d7df72aee320385fb549464d (diff) | |
download | mpv-3295caca3d21b20ccab19ab3420d07a139b4f200.tar.bz2 mpv-3295caca3d21b20ccab19ab3420d07a139b4f200.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.
Diffstat (limited to 'player')
-rw-r--r-- | player/lua/defaults.lua | 37 |
1 files changed, 21 insertions, 16 deletions
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 {} |