Simplified AsciiIo.Player

openid
Marcin Kulik 13 years ago
parent 3fd50d500e
commit c9eca61c05

@ -138,6 +138,7 @@ class AsciiIo.AnsiInterpreter
feed: (data) -> feed: (data) ->
match = undefined match = undefined
handler = undefined handler = undefined
while data.length > 0 while data.length > 0
match = handler = null match = handler = null
i = 0 i = 0
@ -149,9 +150,12 @@ class AsciiIo.AnsiInterpreter
handler = pattern[1] handler = pattern[1]
break break
i++ i++
if handler if handler
handler.call this, data, match handler.call this, data, match
data = data.slice(match[0].length) data = data.slice(match[0].length)
else else
return data break
""
@terminal.render()
data

@ -1,58 +1,59 @@
class AsciiIo.Player class AsciiIo.Player
constructor: (cols, lines, data, time) -> MIN_DELAY: 0.01
@minDelay = 0.01 SPEED: 1.0
@speed = 1.0
constructor: (cols, lines, data, timing) ->
@terminal = new AsciiIo.Terminal(cols, lines) @terminal = new AsciiIo.Terminal(cols, lines)
@interpreter = new AsciiIo.AnsiInterpreter(@terminal) @interpreter = new AsciiIo.AnsiInterpreter(@terminal)
@data = data @data = data
@time = time @timing = timing
@dataIndex = 0 @dataIndex = 0
@frame = 0 @frameNo = 0
@currentData = "" @currentData = ""
console.log "started" console.log "started"
@startTime = (new Date()).getTime() @startTime = (new Date()).getTime()
@nextFrame() @nextFrame()
nextFrame: () -> nextFrame: () ->
timing = @time[@frame] frame = @timing[@frameNo]
unless timing unless frame
@terminal.stopCursorBlink()
console.log "finished in #{((new Date()).getTime() - @startTime) / 1000} seconds" console.log "finished in #{((new Date()).getTime() - @startTime) / 1000} seconds"
return return
@terminal.restartCursorBlink() @frameNo += 1
run = () => [delay, count] = frame
rest = @interpreter.feed(@currentData)
@terminal.render()
n = timing[1]
if rest.length > 0 if delay > @MIN_DELAY
console.log 'rest: ' + Utf8.decode(rest) realDelay = delay * 1000 * (1.0 / @SPEED)
@currentData = rest + @data.slice(@dataIndex, @dataIndex + n) setTimeout(
@dataIndex += n =>
@frame += 1 @terminal.restartCursorBlink()
@processFrame(count)
@nextFrame()
realDelay
)
else
@processFrame(count)
@nextFrame()
if rest.length > 100 processFrame: (count) ->
head = rest.slice(0, 100) @currentData += @data.slice(@dataIndex, @dataIndex + count)
hex = ("0x#{c.charCodeAt(0).toString(16)}" for c in head) @dataIndex += count
console.log "failed matching: '" + Utf8.decode(head) + "' (" + hex.join() + ") [pos: " + (@dataIndex - n) + "]"
return
unless window.stopped @currentData = @interpreter.feed(@currentData)
@nextFrame()
if timing[0] > @minDelay if @currentData.length > 0
setTimeout(run, timing[0] * 1000 * (1.0 / @speed)) @logStatus()
else
run()
logStatus: ->
console.log 'rest: ' + Utf8.decode(@currentData)
# $(function() { if @currentData.length > 100
# $(window).bind('keyup', function(event) { head = @currentData.slice(0, 100)
# if (event.keyCode == 27) { hex = ("0x#{c.charCodeAt(0).toString(16)}" for c in head)
# window.stopped = true console.log "failed matching: '" + Utf8.decode(head) + "' (" + hex.join() + ") [pos: " + (@dataIndex - count) + "]"
# } return
# })
# })

Loading…
Cancel
Save