Skip to content

Instantly share code, notes, and snippets.

@lucaslugao
Last active June 5, 2021 14:48
Show Gist options
  • Save lucaslugao/90104f316392d9e0c02b07d64eb48af4 to your computer and use it in GitHub Desktop.
Save lucaslugao/90104f316392d9e0c02b07d64eb48af4 to your computer and use it in GitHub Desktop.
Binary Search script for MPV
this.states = [{ left: 0, cur: 50, right: 100 }];
function printState(state) {
var line = "";
var width = 79;
var round = function (v) {
return Math.floor((v * width) / 100);
};
var visMap = {};
visMap[round(state.left)] = "[";
visMap[round(state.right)] = "]";
visMap[round(state.cur)] = "|";
for (var i = 0; i <= width; i++) line += visMap[i] || "-";
mp.msg.info(line);
}
function seek() {
this.states.forEach(printState);
var state = this.states.slice(-1)[0];
mp.set_property_number("percent-pos", state.cur);
}
function doBinary(left) {
var state = this.states.slice(-1)[0];
this.states.push({
left: left ? state.left : state.cur,
cur: ((left ? state.left : state.right) + state.cur) / 2,
right: left ? state.cur : state.right,
});
}
function leftBinary() {
doBinary(true);
seek();
}
function rightBinary() {
doBinary(false);
seek();
}
function backBinary() {
if (this.states.length > 1) this.states.pop();
seek();
}
function resetBinary() {
this.states = [this.states[0]];
seek();
}
mp.add_key_binding("Shift+Alt+LEFT", "leftBinary", leftBinary);
mp.add_key_binding("Shift+Alt+RIGHT", "rightBinary", rightBinary);
mp.add_key_binding("Shift+Alt+UP", "backBinary", backBinary);
mp.add_key_binding("Shift+Alt+DOWN", "resetBinary", resetBinary);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment