Expose stdout of old and new format asciicasts via Asciicast#stdout
parent
8f56d019ec
commit
dad5f40c17
@ -1,22 +0,0 @@
|
||||
class BufferedStdout < Stdout
|
||||
|
||||
MIN_FRAME_LENGTH = 1.0 / 60
|
||||
|
||||
def each
|
||||
buffered_delay, buffered_data = 0.0, []
|
||||
|
||||
super do |delay, data|
|
||||
if buffered_delay + delay < MIN_FRAME_LENGTH
|
||||
buffered_delay += delay
|
||||
buffered_data << data
|
||||
else
|
||||
yield(buffered_delay, buffered_data.join)
|
||||
buffered_delay = delay
|
||||
buffered_data = [data]
|
||||
end
|
||||
end
|
||||
|
||||
yield(buffered_delay, buffered_data.join) unless buffered_data.empty?
|
||||
end
|
||||
|
||||
end
|
@ -1,28 +1,98 @@
|
||||
class Stdout
|
||||
include Enumerable
|
||||
|
||||
attr_reader :data_path, :timing_path
|
||||
class SingleFile < self
|
||||
attr_reader :path
|
||||
|
||||
def initialize(path)
|
||||
@path = path
|
||||
end
|
||||
|
||||
def each(&blk)
|
||||
File.open(path, 'r') do |f|
|
||||
Oj.sc_parse(FrameIterator.new(blk), f)
|
||||
end
|
||||
end
|
||||
|
||||
class FrameIterator < ::Oj::ScHandler
|
||||
|
||||
def initialize(callback)
|
||||
@callback = callback
|
||||
end
|
||||
|
||||
def array_start
|
||||
if @top # we're already inside top level array
|
||||
[] # <- this will hold pair [delay, data]
|
||||
else
|
||||
@top = []
|
||||
end
|
||||
end
|
||||
|
||||
def array_append(a, v)
|
||||
if a.equal?(@top)
|
||||
@callback.call(*v)
|
||||
else
|
||||
a << v
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def initialize(data_path, timing_path)
|
||||
@data_path = data_path
|
||||
@timing_path = timing_path
|
||||
end
|
||||
|
||||
def each
|
||||
File.open(data_path, 'rb') do |file|
|
||||
File.foreach(timing_path) do |line|
|
||||
yield(*delay_and_data_for_line(file, line))
|
||||
class MultiFile < self
|
||||
attr_reader :data_path, :timing_path
|
||||
|
||||
def initialize(data_path, timing_path)
|
||||
@data_path = data_path
|
||||
@timing_path = timing_path
|
||||
end
|
||||
|
||||
def each
|
||||
File.open(data_path, 'rb') do |file|
|
||||
File.foreach(timing_path) do |line|
|
||||
yield(*delay_and_data_for_line(file, line))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def delay_and_data_for_line(file, line)
|
||||
delay, size = TimingParser.parse_line(line)
|
||||
data = file.read(size).to_s.force_encoding('utf-8')
|
||||
|
||||
[delay, data]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
class Buffered < self
|
||||
MIN_FRAME_LENGTH = 1.0 / 60
|
||||
|
||||
def delay_and_data_for_line(file, line)
|
||||
delay, size = TimingParser.parse_line(line)
|
||||
data = file.read(size).to_s.force_encoding('utf-8')
|
||||
attr_reader :stdout
|
||||
|
||||
def initialize(stdout)
|
||||
@stdout = stdout
|
||||
end
|
||||
|
||||
def each
|
||||
buffered_delay, buffered_data = 0.0, []
|
||||
|
||||
stdout.each do |delay, data|
|
||||
if buffered_delay + delay < MIN_FRAME_LENGTH || buffered_data.empty?
|
||||
buffered_delay += delay
|
||||
buffered_data << data
|
||||
else
|
||||
yield(buffered_delay, buffered_data.join)
|
||||
buffered_delay = delay
|
||||
buffered_data = [data]
|
||||
end
|
||||
end
|
||||
|
||||
yield(buffered_delay, buffered_data.join) unless buffered_data.empty?
|
||||
end
|
||||
|
||||
[delay, data]
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1 +0,0 @@
|
||||
abcdefghijklmnopqrstuwxyz
|
@ -1,13 +0,0 @@
|
||||
0.010000 1
|
||||
0.006000 1
|
||||
0.000600 1
|
||||
0.000060 1
|
||||
0.000006 1
|
||||
1.000000 1
|
||||
0.016665 1
|
||||
0.000002 1
|
||||
0.016664 1
|
||||
0.000002 1
|
||||
0.016666 1
|
||||
0.016667 1
|
||||
0.000001 1
|
@ -1,25 +0,0 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe BufferedStdout do
|
||||
|
||||
let(:stdout) { described_class.new('spec/fixtures/high-freq-stdout',
|
||||
'spec/fixtures/high-freq-stdout.time') }
|
||||
|
||||
describe '#each' do
|
||||
let(:yield_args) { [
|
||||
[0.016666, 'abcde'],
|
||||
[1.000000, 'f'],
|
||||
[0.016665, 'g'],
|
||||
[0.016666, 'hi'],
|
||||
[0.000002, 'j'],
|
||||
[0.016666, 'k'],
|
||||
[0.016667, 'l'],
|
||||
[0.000001, 'm']
|
||||
] }
|
||||
|
||||
it 'yields for each frame with delay and data at 60hz freq tops' do
|
||||
expect { |b| stdout.each(&b) }.to yield_successive_args(*yield_args)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue