Module: AsciinemaWin::Asciicast

Defined in:
lib/asciinema_win/asciicast.rb

Overview

Asciicast v2 file format handling

This module implements the asciicast v2 specification for terminal recordings. Format: newline-delimited JSON (NDJSON)

  • Line 1: Header object with metadata

  • Lines 2+: Event arrays [time, type, data]

Defined Under Namespace

Modules: EventType Classes: Event, Header, Reader, Writer

Constant Summary collapse

VERSION =

Asciicast format version

2
EXTENSION =

File extension

".cast"
MIME_TYPE =

MIME type

"application/x-asciicast"

Class Method Summary collapse

Class Method Details

.create(path, width:, height:, **kwargs) {|Writer| ... } ⇒ void

This method returns an undefined value.

Create a recording from a file path with a block

Parameters:

  • path (String)

    Output file path

  • width (Integer)

    Terminal width

  • height (Integer)

    Terminal height

  • kwargs (Hash)

    Additional header options

Yields:

  • (Writer)

    Writer for adding events



529
530
531
532
533
534
535
536
537
# File 'lib/asciinema_win/asciicast.rb', line 529

def self.create(path, width:, height:, **kwargs)
  header = Header.new(width: width, height: height, **kwargs)

  File.open(path, "w", encoding: "UTF-8") do |file|
    writer = Writer.new(file, header)
    yield writer if block_given?
    writer.close
  end
end

.load(path) {|Reader| ... } ⇒ Reader, Object

Load a recording from a file path

With a block, the reader is yielded and the file is closed automatically when the block returns. Without a block, the open Reader is returned and the caller is responsible for calling AsciinemaWin::Asciicast::Reader#close.

Parameters:

  • path (String)

    Input file path

Yields:

  • (Reader)

    Reader for the recording

Returns:

  • (Reader, Object)

    The Reader (no block) or the block’s result

Raises:



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/asciinema_win/asciicast.rb', line 549

def self.load(path)
  file = File.open(path, "r", encoding: "UTF-8")
  begin
    reader = Reader.new(file)
  rescue StandardError
    file.close
    raise
  end

  return reader unless block_given?

  begin
    yield reader
  ensure
    reader.close
  end
end