Class: Cased::CLI::Asciinema::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/cased/cli/asciinema/writer.rb

Constant Summary collapse

VERSION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command: [], width: 80, height: 24) ⇒ Writer

Returns a new instance of Writer.



15
16
17
18
19
20
21
# File 'lib/cased/cli/asciinema/writer.rb', line 15

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

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



13
14
15
# File 'lib/cased/cli/asciinema/writer.rb', line 13

def command
  @command
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



13
14
15
# File 'lib/cased/cli/asciinema/writer.rb', line 13

def finished_at
  @finished_at
end

#heightObject

Returns the value of attribute height.



12
13
14
# File 'lib/cased/cli/asciinema/writer.rb', line 12

def height
  @height
end

#started_atObject (readonly)

Returns the value of attribute started_at.



13
14
15
# File 'lib/cased/cli/asciinema/writer.rb', line 13

def started_at
  @started_at
end

#streamObject (readonly)

Returns the value of attribute stream.



13
14
15
# File 'lib/cased/cli/asciinema/writer.rb', line 13

def stream
  @stream
end

#widthObject

Returns the value of attribute width.



12
13
14
# File 'lib/cased/cli/asciinema/writer.rb', line 12

def width
  @width
end

Instance Method Details

#<<(output) ⇒ Object



23
24
25
# File 'lib/cased/cli/asciinema/writer.rb', line 23

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

#headerObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cased/cli/asciinema/writer.rb', line 42

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

#timeObject



27
28
29
30
31
32
# File 'lib/cased/cli/asciinema/writer.rb', line 27

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

#to_castObject



34
35
36
37
38
39
40
# File 'lib/cased/cli/asciinema/writer.rb', line 34

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