Class: AsciinemaWin::Asciicast::Event

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

Overview

Single recording event

Examples:

Output event

event = Event.new(0.5, EventType::OUTPUT, "Hello, World!")

Resize event

event = Event.new(1.0, EventType::RESIZE, "120x40")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, type, data) ⇒ Event

Create a new event

Parameters:

  • time (Float)

    Time offset in seconds

  • type (String)

    Event type

  • data (String)

    Event data



214
215
216
217
218
# File 'lib/asciinema_win/asciicast.rb', line 214

def initialize(time, type, data)
  @time = time.to_f
  @type = type.to_s
  @data = data.to_s
end

Instance Attribute Details

#dataString (readonly)

Returns Event data.

Returns:

  • (String)

    Event data



207
208
209
# File 'lib/asciinema_win/asciicast.rb', line 207

def data
  @data
end

#timeFloat (readonly)

Returns Time offset in seconds from recording start.

Returns:

  • (Float)

    Time offset in seconds from recording start



201
202
203
# File 'lib/asciinema_win/asciicast.rb', line 201

def time
  @time
end

#typeString (readonly)

Returns Event type (o, i, r, m).

Returns:

  • (String)

    Event type (o, i, r, m)



204
205
206
# File 'lib/asciinema_win/asciicast.rb', line 204

def type
  @type
end

Class Method Details

.from_json(json_str) ⇒ Event

Parse event from JSON string

Parameters:

  • json_str (String)

    JSON array string

Returns:

  • (Event)

    Parsed event

Raises:



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/asciinema_win/asciicast.rb', line 232

def self.from_json(json_str)
  data = JSON.parse(json_str)

  unless data.is_a?(Array) && data.length >= 3
    raise FormatError, "Event must be a JSON array with 3 elements"
  end

  new(data[0], data[1], data[2])
rescue JSON::ParserError => e
  raise FormatError, "Invalid event JSON: #{e.message}"
end

Instance Method Details

#input?Boolean

Check if this is an input event

Returns:

  • (Boolean)


252
253
254
# File 'lib/asciinema_win/asciicast.rb', line 252

def input?
  @type == EventType::INPUT
end

#marker?Boolean

Check if this is a marker event

Returns:

  • (Boolean)


264
265
266
# File 'lib/asciinema_win/asciicast.rb', line 264

def marker?
  @type == EventType::MARKER
end

#output?Boolean

Check if this is an output event

Returns:

  • (Boolean)


246
247
248
# File 'lib/asciinema_win/asciicast.rb', line 246

def output?
  @type == EventType::OUTPUT
end

#resize?Boolean

Check if this is a resize event

Returns:

  • (Boolean)


258
259
260
# File 'lib/asciinema_win/asciicast.rb', line 258

def resize?
  @type == EventType::RESIZE
end

#resize_dimensionsArray<Integer>?

Get resize dimensions (for resize events)

Returns:

  • (Array<Integer>, nil)
    width, height

    or nil if not a resize event



270
271
272
273
274
275
276
277
# File 'lib/asciinema_win/asciicast.rb', line 270

def resize_dimensions
  return nil unless resize?

  parts = @data.split("x")
  return nil unless parts.length == 2

  [parts[0].to_i, parts[1].to_i]
end

#to_hHash

Returns Event as a hash.

Returns:

  • (Hash)

    Event as a hash



280
281
282
# File 'lib/asciinema_win/asciicast.rb', line 280

def to_h
  { time: @time, type: @type, data: @data }
end

#to_json(*_args) ⇒ String

Serialize event to JSON array string

Returns:

  • (String)

    JSON array representation



223
224
225
# File 'lib/asciinema_win/asciicast.rb', line 223

def to_json(*_args)
  JSON.generate([@time, @type, @data])
end