Sha256: 5274d1d3544f8b1525f2bdb85765f2e53334a68fbb523a212e97ed37f291cafd

Contents?: true

Size: 838 Bytes

Versions: 20

Compression:

Stored size: 838 Bytes

Contents

require 'iconv'

module Testbot::Runner
  class SafeResultText
    def self.clean(text)
      clean_escape_sequences(strip_invalid_utf8(text))
    end

    def self.strip_invalid_utf8(text)
      # http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
      ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
      ic.iconv(text + ' ')[0..-2]
    end

    def self.clean_escape_sequences(text)
      tail_marker = "^[[0m"
      tail = text.rindex(tail_marker) && text[text.rindex(tail_marker)+tail_marker.length..-1]
      if !tail
        text
      elsif tail.include?("^[[") && !tail.include?("m")
        text[0..text.rindex(tail_marker) + tail_marker.length - 1]
      elsif text.scan(/\[.*?m/).last != tail_marker
        text[0..text.rindex(tail_marker) + tail_marker.length - 1]
      else
        text
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
testbot-0.7.12 lib/runner/safe_result_text.rb
testbot-0.7.11 lib/runner/safe_result_text.rb
testbot-0.7.9 lib/runner/safe_result_text.rb
testbot_instructure-0.7.9 lib/runner/safe_result_text.rb
testbot_instructure-0.7.8 lib/runner/safe_result_text.rb
testbot-0.7.8 lib/runner/safe_result_text.rb
testbot-0.7.7 lib/runner/safe_result_text.rb
testbot-0.7.6 lib/runner/safe_result_text.rb
testbot-0.7.5 lib/runner/safe_result_text.rb
testbot-0.7.3 lib/runner/safe_result_text.rb
testbot-0.7.2 lib/runner/safe_result_text.rb
testbot-0.7.1 lib/runner/safe_result_text.rb
testbot-0.7.0 lib/runner/safe_result_text.rb
testbot-0.6.9 lib/runner/safe_result_text.rb
testbot-0.6.8 lib/runner/safe_result_text.rb
testbot-0.6.7 lib/runner/safe_result_text.rb
testbot-0.6.6 lib/runner/safe_result_text.rb
testbot-0.6.5 lib/runner/safe_result_text.rb
testbot-0.6.4 lib/runner/safe_result_text.rb
testbot-0.6.3 lib/runner/safe_result_text.rb