History

js: implement mp.options.read_options

Avi Halachmi (:avih) committed 8 years ago

Commit b04f0ca1 parent 9eadc062 files · +60 −8Browse files

Showing the changes to player/javascript (1 of 2 files). Show all

Unified · Split

player/javascript/defaults.js+52−0
@@ -419,6 +419,58 @@ function new_require(base_id) { · 418 unmodified lines
419419
420420 g.require = new_require(SCRIPTDIR_META + "/" + main_script[1]);
421421
422+/**********************************************************************
423+ * mp.options
424+ *********************************************************************/
425+function read_options(opts, id) {
426+ id = String(typeof id != "undefined" ? id : mp.get_script_name());
427+ mp.msg.debug("reading options for " + id);
428+
429+ var conf, fname = "~~/script-opts/" + id + ".conf";
430+ try {
431+ conf = mp.utils.read_file(fname);
432+ } catch (e) {
433+ mp.msg.verbose(fname + " not found.");
434+ }
435+
436+ // data as config file lines array, or empty array
437+ var data = conf ? conf.replace(/\r\n/g, "\n").split("\n") : [],
438+ conf_len = data.length; // before we append script-opts below
439+
440+ // Append relevant script-opts as <key-sans-id>=<value> to data
441+ var sopts = mp.get_property_native("options/script-opts"),
442+ prefix = id + "-";
443+ for (var key in sopts) {
444+ if (key.indexOf(prefix) == 0)
445+ data.push(key.substring(prefix.length) + "=" + sopts[key]);
446+ }
447+
448+ // Update opts from data
449+ data.forEach(function(line, i) {
450+ if (line[0] == "#" || line.trim() == "")
451+ return;
452+
453+ var key = line.substring(0, line.indexOf("=")),
454+ val = line.substring(line.indexOf("=") + 1),
455+ type = typeof opts[key],
456+ info = i < conf_len ? fname + ":" + (i + 1) // 1-based line number
457+ : "script-opts:" + prefix + key;
458+
459+ if (!opts.hasOwnProperty(key))
460+ mp.msg.warn(info, "Ignoring unknown key '" + key + "'");
461+ else if (type == "string")
462+ opts[key] = val;
463+ else if (type == "boolean" && (val == "yes" || val == "no"))
464+ opts[key] = (val == "yes");
465+ else if (type == "number" && val.trim() != "" && !isNaN(val))
466+ opts[key] = Number(val);
467+ else
468+ mp.msg.error(info, "Error: can't convert '" + val + "' to " + type);
469+ });
470+}
471+
472+mp.options = { read_options: read_options };
473+
422474 /**********************************************************************
423475 * various
424476 *********************************************************************/

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t