Class: ScripTTY::Apps::TranscriptParseApp

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptty/apps/transcript_parse_app.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ TranscriptParseApp

Returns a new instance of TranscriptParseApp.



27
28
29
# File 'lib/scriptty/apps/transcript_parse_app.rb', line 27

def initialize(argv)
  @options = parse_options(argv)
end

Instance Method Details

#mainObject



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/scriptty/apps/transcript_parse_app.rb', line 31

def main
  File.open(@options[:output], "w") do |output_file|
    @writer = ScripTTY::Util::Transcript::Writer.new(output_file)
    @options[:input_files].each do |input_filename|
      File.open(input_filename, "rb") do |input_file|
        reader = ScripTTY::Util::Transcript::Reader.new
        @last_printable = nil
        parser_class = ScripTTY::Term.class_by_name(@options[:term]).parser_class

        # Set up server-side FSM
        @server_parser = parser_class.new :callback => Proc.new { |event_name, fsm|
          if event_name == :t_printable
            # Merge printable character sequences together, rather than writing individual "t_printable" events.
            @last_server_printable ||= ""
            @last_server_printable += fsm.input_sequence.join
          else
            flush_server_printable
            @writer.server_parsed(event_name, fsm.input_sequence.join)
          end
        }
        @server_parser.on_unknown_sequence { |seq| @writer.server_parsed("?", seq) }

        # Set up client-side FSM
        if @options[:client]
          @client_parser = parser_class.new :client => true, :callback => Proc.new { |event_name, fsm|
            if event_name == :t_printable
              # Merge printable character sequences together, rather than writing individual "t_printable" events.
              @last_client_printable ||= ""
              @last_client_printable += fsm.input_sequence.join
            else
              flush_client_printable
              @writer.client_parsed(event_name, fsm.input_sequence.join)
            end
          }
          @client_parser.on_unknown_sequence { |seq| @writer.client_parsed("?", seq) }
        end

        until input_file.eof?
          timestamp, type, args = reader.parse_line(input_file.readline)
          @writer.override_timestamp = timestamp
          if type == :from_server
            @writer.send(type, *args) if @options[:keep]
            bytes = args[0]
            @server_parser.feed_bytes(bytes)
          elsif type == :from_client and @client_parser
            @writer.send(type, *args) if @options[:keep]
            bytes = args[0]
            @client_parser.feed_bytes(bytes)
          else
            @writer.send(type, *args)
          end
        end
        flush_server_printable
        flush_client_printable if @client_parser
      end
    end
    @writer.close
  end
end