Class: AsciinemaWin::Asciicast::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/asciinema_win/asciicast.rb

Overview

Reader for playing back asciicast recordings

Examples:

Read a recording

File.open("recording.cast", "r") do |file|
  reader = Asciicast::Reader.new(file)
  puts "Recording: #{reader.header.title}"
  reader.each_event do |event|
    puts "#{event.time}: #{event.type}"
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Reader

Create a new reader

Parameters:

  • io (IO)

    Input stream

Raises:



412
413
414
415
416
# File 'lib/asciinema_win/asciicast.rb', line 412

def initialize(io)
  @io = io
  @header = read_header
  @events_started = false
end

Instance Attribute Details

#headerHeader (readonly)

Returns Recording header.

Returns:

  • (Header)

    Recording header



406
407
408
# File 'lib/asciinema_win/asciicast.rb', line 406

def header
  @header
end

Class Method Details

.info(path) ⇒ Hash

Get recording info from a file path

Parameters:

  • path (String)

    Path to recording file

Returns:

  • (Hash)

    Recording metadata

Raises:



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/asciinema_win/asciicast.rb', line 423

def self.info(path)
  File.open(path, "r", encoding: "UTF-8") do |file|
    reader = new(file)
    header = reader.header

    # Count events and calculate duration
    event_count = 0
    last_time = 0.0

    reader.each_event do |event|
      event_count += 1
      last_time = event.time
    end

    {
      version: header.version,
      width: header.width,
      height: header.height,
      timestamp: header.timestamp,
      duration: header.duration || last_time,
      idle_time_limit: header.idle_time_limit,
      command: header.command,
      title: header.title,
      env: header.env,
      theme: header.theme,
      event_count: event_count
    }
  end
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close the underlying input stream



498
499
500
# File 'lib/asciinema_win/asciicast.rb', line 498

def close
  @io.close if @io.respond_to?(:close) && !closed?
end

#closed?Boolean

Returns Whether the underlying stream is closed.

Returns:

  • (Boolean)

    Whether the underlying stream is closed



503
504
505
# File 'lib/asciinema_win/asciicast.rb', line 503

def closed?
  @io.respond_to?(:closed?) ? @io.closed? : false
end

#durationFloat

Get total duration of the recording

Returns:

  • (Float)

    Duration in seconds



486
487
488
489
490
491
492
493
# File 'lib/asciinema_win/asciicast.rb', line 486

def duration
  return @header.duration if @header.duration

  # Calculate from events
  last_time = 0.0
  each_event { |e| last_time = e.time }
  last_time
end

#each_event {|Event| ... } ⇒ Enumerator, void

Iterate over all events in the recording

Yields:

  • (Event)

    Each event in order

Returns:

  • (Enumerator, void)

    If no block given, returns an Enumerator



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/asciinema_win/asciicast.rb', line 457

def each_event
  return enum_for(:each_event) unless block_given?

  @events_started = true

  @io.each_line do |line|
    line = line.strip
    next if line.empty?

    begin
      event = Event.from_json(line)
      yield event
    rescue FormatError
      # Skip invalid lines (could be comments or garbage)
      next
    end
  end
end

#eventsArray<Event>

Read all events into an array

Returns:

  • (Array<Event>)

    All events



479
480
481
# File 'lib/asciinema_win/asciicast.rb', line 479

def events
  each_event.to_a
end