From ef08c5079c20653d1d5d83fd517d35ddbf9cb960 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Sun, 27 Nov 2011 17:02:53 +0100 Subject: [PATCH] Coffize player.js --- app/assets/javascripts/application.js | 1 + app/assets/javascripts/namespace.js | 2 + app/assets/javascripts/player.js | 71 ------------------------- app/assets/javascripts/player.js.coffee | 57 ++++++++++++++++++++ app/helpers/asciicasts_helper.rb | 2 +- 5 files changed, 61 insertions(+), 72 deletions(-) create mode 100644 app/assets/javascripts/namespace.js delete mode 100644 app/assets/javascripts/player.js create mode 100644 app/assets/javascripts/player.js.coffee diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index b1b5e03..490b6a9 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -8,6 +8,7 @@ //= require jquery_ujs //= require utf8 //= require helpers +//= require namespace //= require player //= require ansi-interpreter //= require terminal diff --git a/app/assets/javascripts/namespace.js b/app/assets/javascripts/namespace.js new file mode 100644 index 0000000..6352222 --- /dev/null +++ b/app/assets/javascripts/namespace.js @@ -0,0 +1,2 @@ +window.AsciiIo = {}; +window.SP = {}; diff --git a/app/assets/javascripts/player.js b/app/assets/javascripts/player.js deleted file mode 100644 index 32d3d66..0000000 --- a/app/assets/javascripts/player.js +++ /dev/null @@ -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 minDelay) { - setTimeout(run, timing[0] * 1000 * (1.0 / speed)); - } else { - run(); - } - } -} - -$(function() { - $(window).bind('keyup', function(event) { - if (event.keyCode == 27) { - window.stopped = true; - } - }); -}); diff --git a/app/assets/javascripts/player.js.coffee b/app/assets/javascripts/player.js.coffee new file mode 100644 index 0000000..081e5a3 --- /dev/null +++ b/app/assets/javascripts/player.js.coffee @@ -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 +# } +# }) +# }) diff --git a/app/helpers/asciicasts_helper.rb b/app/helpers/asciicasts_helper.rb index 57553f2..4ec8ece 100644 --- a/app/helpers/asciicasts_helper.rb +++ b/app/helpers/asciicasts_helper.rb @@ -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); }); EOS end