Class: AsciinemaWin::Asciicast::Reader
- Inherits:
-
Object
- Object
- AsciinemaWin::Asciicast::Reader
- Defined in:
- lib/asciinema_win/asciicast.rb
Overview
Reader for playing back asciicast recordings
Instance Attribute Summary collapse
-
#header ⇒ Header
readonly
Recording header.
Class Method Summary collapse
-
.info(path) ⇒ Hash
Get recording info from a file path.
Instance Method Summary collapse
-
#close ⇒ void
Close the underlying input stream.
-
#closed? ⇒ Boolean
Whether the underlying stream is closed.
-
#duration ⇒ Float
Get total duration of the recording.
-
#each_event {|Event| ... } ⇒ Enumerator, void
Iterate over all events in the recording.
-
#events ⇒ Array<Event>
Read all events into an array.
-
#initialize(io) ⇒ Reader
constructor
Create a new reader.
Constructor Details
#initialize(io) ⇒ Reader
Create a new reader
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
#header ⇒ Header (readonly)
Returns 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
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., 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
#close ⇒ void
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.
503 504 505 |
# File 'lib/asciinema_win/asciicast.rb', line 503 def closed? @io.respond_to?(:closed?) ? @io.closed? : false end |
#duration ⇒ Float
Get total duration of the recording
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
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 |
#events ⇒ Array<Event>
Read all events into an array
479 480 481 |
# File 'lib/asciinema_win/asciicast.rb', line 479 def events each_event.to_a end |