Class: ScripTTY::Apps::TermTestApp

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ TermTestApp

Returns a new instance of TermTestApp.



26
27
28
# File 'lib/scriptty/apps/term_test_app.rb', line 26

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

Instance Method Details

#log(message) ⇒ Object



30
31
32
33
# File 'lib/scriptty/apps/term_test_app.rb', line 30

def log(message)
  @log_buffer << message
  @log_file.puts(message) if @log_file
end

#mainObject



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
90
91
92
93
# File 'lib/scriptty/apps/term_test_app.rb', line 35

def main
  @log_buffer = []
  @log_file = @options[:log] && File.open(@options[:log], "w")
  @term = ScripTTY::Term.new(@options[:term])
  @term.on_unknown_sequence do |seq|
    log "Unknown escape sequence: #{seq.inspect}"
  end
  $stdout.print "\ec" # Reset terminal; clear the screen
  $stdout.flush
  time_multiplier = @options[:rate] > 0 ? 1.0/@options[:rate] : 0
  @options[:input_files].each do |inp|
    File.open(inp[:path], "rb") do |input_file|
      time0 = Time.now
      timestamp0 = timestamp = nil
      reader = Util::Transcript::Reader.new
      until input_file.eof?
        # Read a chunk of the input file
        case inp[:format]
        when :binary
          c = input_file.read(1)
          @term.feed_byte(c)
        when :capture
          timestamp, type, args = reader.parse_line(input_file.readline)
          next unless type == :from_server
          bytes = args[0]

          # Wait until the time specified in the timestamp passes
          timestamp0 = timestamp unless timestamp0   # treat the first timestamp as time 0
          sleep_time = (timestamp-timestamp0)*time_multiplier - (Time.now - time0)
          sleep(sleep_time) if sleep_time > 0

          @term.feed_bytes(bytes)
        else
          raise "BUG: Invalid format: #{inp[:format]}"
        end

        # Output the screen contents
        screen_lines = []
        screen_lines << "Timestamp: #{timestamp}" if timestamp
        screen_lines << "Cursor position: #{@term.cursor_pos.inspect}"
        screen_lines += @term.debug_info if @term.respond_to?(:debug_info)
        screen_lines << "+" + "-"*@term.width + "+"
        @term.text.each do |line|
          screen_lines << "|#{line}|"
        end
        screen_lines << "+" + "-"*@term.width + "+"
        screen_lines << "--- Log ---"
        ([0,@log_buffer.length-10].max..@log_buffer.length-1).each do |i|
          screen_lines << sprintf("%3d: %s", i+1, @log_buffer[i])
        end
        screen_lines << "--- End of log ---"
        screen_lines << ""
        $stdout.puts "\e[H" + screen_lines.map{|line| "\e[2K" + line}.join("\n")
        $stdout.flush
      end
    end
  end
  @log_file.close if @log_file
end