Class: Asciinema::Rails::Convertor

Inherits:
Object
  • Object
show all
Defined in:
lib/asciinema/rails/convertor.rb

Class Method Summary collapse

Class Method Details

.asciicast_to_playback(asciicast_path, options = {}) ⇒ Object

Params:

  • asciicast_path: path to the asciicast file

  • options: Additional options hash:

    • :playback_path: file to write the playback data to

Returns:

  • An array containing:

    • the playback file content as a string

    • a snapshot string

    • a hash containing the original width (cols), height (rows) and duration of the asciicast.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/asciinema/rails/convertor.rb', line 73

def self.asciicast_to_playback(asciicast_path, options = {})
  playback_path = options[:playback_path] # playback data will be written here

  json = JSON.parse(File.read(asciicast_path)) # load the asciicast data into a hash 
  asciicast = Asciicast.new(json['width'], json['height'], json['duration'], asciicast_path) # create an Asciicast object
  AsciicastFramesFileUpdater.new.update(asciicast, playback_path) # set playback data in Asciicast object, write to file also
  AsciicastSnapshotUpdater.new.update(asciicast) # set snapshot data in Asciicast object.
  
  [asciicast.stdout_frames, ActiveSupport::JSON.encode(asciicast.snapshot), {width: json['width'], height: json['height'], duration: json['duration']} ]

end

.sudosh_to_asciicast(sudosh_timing_file_path, sudosh_script_file_path, options = {}) ⇒ Object

Params:

  • sudosh_timing_file_path: path to the Sudosh timing file

  • sudosh_script_file_path: path to the Sudosh script file

  • options: Additional options hash:

    • :original_terminal_cols: width of the Sudosh session terminal

    • :original_terminal_rows: height of the Sudosh session terminal

    • :asciicast_path: file to write output to

Returns:

  • A File object pointing to the generated asciicast.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/asciinema/rails/convertor.rb', line 24

def self.sudosh_to_asciicast(sudosh_timing_file_path, sudosh_script_file_path, options = {})
  original_terminal_cols = options[:original_terminal_cols] || 180
  original_terminal_rows = options[:original_terminal_rows] || 43
  asciicast_path = options[:asciicast_path]
  
  timings = []
  byte_offsets = []

  # Split the sudosh timing file into individual time splits and data byte offsets.
  # When paired with the Sudosh data file, each time split can be though of as an animation 'frame'
  File.open(sudosh_timing_file_path) do |f|
    f.each_line.each do |line|
      split = line.split(' ')
      timings << split[0].to_f
      byte_offsets << split[1].to_i
    end
  end
  duration = timings.inject(:+) ## add all of the time splits to get the total duration

  # Split the script file into segments as defined by the byte offsets in the timing file.
  # TODO: Write stdout directly to file rather than into memory first.
  stdout = []
  File.open(sudosh_script_file_path, 'rb') do |file|
    until file.eof?
      timestamp = '%.5f' % timings.shift.to_f
      terminal_chunk = file.read(byte_offsets.shift)
      stdout << [Float(timestamp), terminal_chunk]
    end
  end

  json = {version: 1, width: original_terminal_cols, height: original_terminal_rows, duration: duration}
  json[:stdout] = stdout 

  self.to_file(JSON.pretty_generate(json), asciicast_path)
  
end