Sha256: 1814fe86410c31196130b7b35015e97197b74686b271ad17884ef54696a393b1

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require 'json'

module Cased
  module CLI
    # Spec: https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v2.md
    module Asciinema
      class Writer
        VERSION = 2

        attr_accessor :width, :height
        attr_reader :command, :stream, :started_at, :finished_at

        def initialize(command: [], width: 80, height: 24)
          @command = command
          @width = width
          @height = height
          @stream = []
          @started_at = Time.now
        end

        def <<(output)
          stream << [Time.now - started_at, 'o', output]
        end

        def time
          @started_at = Time.now
          ret = yield
          @finished_at = Time.now
          ret
        end

        def to_cast
          # In the event we didn't run the writer in a #time block, we should
          # set the finished time if it isn't set.
          @finished_at ||= Time.now

          File.new(header, stream).to_cast
        end

        def header
          {
            'version' => VERSION,
            'env' => {
              'SHELL' => ENV['SHELL'],
              'TERM' => ENV['TERM'],
            },
            'width' => width,
            'height' => height,
            'command' => command.join(' '),
          }.tap do |h|
            if started_at
              h['timestamp'] = started_at.to_i
            end

            if started_at && finished_at
              h['duration'] = finished_at - started_at
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cased-ruby-0.8.0 lib/cased/cli/asciinema/writer.rb
cased-ruby-0.7.1 lib/cased/cli/asciinema/writer.rb
cased-ruby-0.7.0 lib/cased/cli/asciinema/writer.rb
cased-ruby-0.6.1 lib/cased/cli/asciinema/writer.rb