Coffize player.js

This commit is contained in:
Marcin Kulik 2011-11-27 17:02:53 +01:00
parent ec7f138dcc
commit ef08c5079c
5 changed files with 61 additions and 72 deletions

View File

@ -8,6 +8,7 @@
//= require jquery_ujs
//= require utf8
//= require helpers
//= require namespace
//= require player
//= require ansi-interpreter
//= require terminal

View File

@ -0,0 +1,2 @@
window.AsciiIo = {};
window.SP = {};

View File

@ -1,71 +0,0 @@
var SP = {};
var speed = 1.0;
var minDelay = 0.01;
SP.Player = function(cols, lines, data, time) {
this.terminal = new SP.Terminal(cols, lines);
this.interpreter = new SP.AnsiInterpreter(this.terminal);
this.data = data;
this.time = time;
this.dataIndex = 0;
this.frame = 0;
this.currentData = "";
console.log("started");
this.nextFrame();
};
SP.Player.prototype = {
nextFrame: function() {
var timing = this.time[this.frame];
if (!timing) {
console.log("finished");
return;
}
this.terminal.restartCursorBlink();
var run = function() {
var rest = this.interpreter.feed(this.currentData);
this.terminal.renderDirtyLines();
var n = timing[1];
if (rest.length > 0)
console.log('rest: ' + rest);
this.currentData = rest + this.data.slice(this.dataIndex, this.dataIndex + n);
this.dataIndex += n;
this.frame += 1;
if (rest.length > 20) {
var s = rest.slice(0, 10);
var hex = '';
for (i=0; i<s.length; i++) {
hex += '0x' + s[i].charCodeAt(0).toString(16) + ',';
}
console.log("failed matching: '" + s + "' (" + hex + ")");
return;
}
if (!window.stopped) {
this.nextFrame();
}
}.bind(this);
if (timing[0] > minDelay) {
setTimeout(run, timing[0] * 1000 * (1.0 / speed));
} else {
run();
}
}
}
$(function() {
$(window).bind('keyup', function(event) {
if (event.keyCode == 27) {
window.stopped = true;
}
});
});

View File

@ -0,0 +1,57 @@
class AsciiIo.Player
constructor: (cols, lines, data, time) ->
@minDelay = 0.01
@speed = 1.0
@terminal = new SP.Terminal(cols, lines)
@interpreter = new SP.AnsiInterpreter(@terminal)
@data = data
@time = time
@dataIndex = 0
@frame = 0
@currentData = ""
console.log "started"
@nextFrame()
nextFrame: () ->
timing = @time[@frame]
unless timing
console.log "finished"
return
@terminal.restartCursorBlink()
run = () =>
rest = @interpreter.feed(@currentData)
@terminal.renderDirtyLines()
n = timing[1]
if rest.length > 0
console.log 'rest: ' + rest
@currentData = rest + @data.slice(@dataIndex, @dataIndex + n)
@dataIndex += n
@frame += 1
if rest.length > 20
head = rest.slice(0, 10)
hex = ("0x#{c.charCodeAt(0).toString(16)}" for c in head)
console.log "failed matching: '" + head + "' (" + hex.join() + ")"
return
unless window.stopped
@nextFrame()
if timing[0] > @minDelay
setTimeout(run, timing[0] * 1000 * (1.0 / @speed))
else
run()
# $(function() {
# $(window).bind('keyup', function(event) {
# if (event.keyCode == 27) {
# window.stopped = true
# }
# })
# })

View File

@ -19,7 +19,7 @@ module AsciicastsHelper
var time = #{j var_time};
var cols = #{asciicast.terminal_columns};
var lines = #{asciicast.terminal_lines};
$(function() { new SP.Player(cols, lines, data, time); });
$(function() { new AsciiIo.Player(cols, lines, data, time); });
</script>
EOS
end