js: implement mp.options.read_options

This commit is contained in:
Avi Halachmi (:avih) 2017-12-14 21:22:56 +02:00 committed by Kevin Mitchell
parent 9eadc068fa
commit b04f0cad43
No known key found for this signature in database
GPG Key ID: 559A34B46A917232
2 changed files with 60 additions and 8 deletions

View File

@ -27,16 +27,16 @@ otherwise, the documented Lua options, script directories, loading, etc apply to
JavaScript files too.
Script initialization and lifecycle is the same as with Lua, and most of the Lua
functions at the modules ``mp``, ``mp.utils`` and ``mp.msg`` are available to
JavaScript with identical APIs - including running commands, getting/setting
properties, registering events/key-bindings/property-changes/hooks, etc.
functions at the modules ``mp``, ``mp.utils``, ``mp.msg`` and ``mp.options`` are
available to JavaScript with identical APIs - including running commands,
getting/setting properties, registering events/key-bindings/hooks, etc.
Differences from Lua
--------------------
No need to load modules. ``mp``, ``mp.utils`` and ``mp.msg`` are preloaded, and
you can use e.g. ``var cwd = mp.utils.getcwd();`` without prior setup.
``mp.options`` is currently not implemented, but ``mp.get_opt(...)`` is.
No need to load modules. ``mp``, ``mp.utils``, ``mp.msg`` and ``mp.options``
are preloaded, and you can use e.g. ``var cwd = mp.utils.getcwd();`` without
prior setup.
Errors are slightly different. Where the Lua APIs return ``nil`` for error,
the JavaScript ones return ``undefined``. Where Lua returns ``something, error``
@ -87,8 +87,6 @@ Unsupported Lua APIs and their JS alternatives
``mp.dispatch_events([allow_wait])`` see event loop below.
``mp.options`` module is not implemented currently for JS.
Scripting APIs - identical to Lua
---------------------------------
@ -184,6 +182,8 @@ Otherwise, where the Lua APIs return ``nil`` on error, JS returns ``undefined``.
``mp.add_hook(type, priority, fn)``
``mp.options.read_options(obj [, identifier])`` (types: string/boolean/number)
Additional utilities
--------------------

View File

@ -419,6 +419,58 @@ function new_require(base_id) {
g.require = new_require(SCRIPTDIR_META + "/" + main_script[1]);
/**********************************************************************
* mp.options
*********************************************************************/
function read_options(opts, id) {
id = String(typeof id != "undefined" ? id : mp.get_script_name());
mp.msg.debug("reading options for " + id);
var conf, fname = "~~/script-opts/" + id + ".conf";
try {
conf = mp.utils.read_file(fname);
} catch (e) {
mp.msg.verbose(fname + " not found.");
}
// data as config file lines array, or empty array
var data = conf ? conf.replace(/\r\n/g, "\n").split("\n") : [],
conf_len = data.length; // before we append script-opts below
// Append relevant script-opts as <key-sans-id>=<value> to data
var sopts = mp.get_property_native("options/script-opts"),
prefix = id + "-";
for (var key in sopts) {
if (key.indexOf(prefix) == 0)
data.push(key.substring(prefix.length) + "=" + sopts[key]);
}
// Update opts from data
data.forEach(function(line, i) {
if (line[0] == "#" || line.trim() == "")
return;
var key = line.substring(0, line.indexOf("=")),
val = line.substring(line.indexOf("=") + 1),
type = typeof opts[key],
info = i < conf_len ? fname + ":" + (i + 1) // 1-based line number
: "script-opts:" + prefix + key;
if (!opts.hasOwnProperty(key))
mp.msg.warn(info, "Ignoring unknown key '" + key + "'");
else if (type == "string")
opts[key] = val;
else if (type == "boolean" && (val == "yes" || val == "no"))
opts[key] = (val == "yes");
else if (type == "number" && val.trim() != "" && !isNaN(val))
opts[key] = Number(val);
else
mp.msg.error(info, "Error: can't convert '" + val + "' to " + type);
});
}
mp.options = { read_options: read_options };
/**********************************************************************
* various
*********************************************************************/