module CMess::GuessEncoding::Manual

Outputs given string (or line), being encoded in target encoding, encoded in various test encodings, thus allowing to identify the (seemingly) correct encoding by visually comparing the input string with its desired appearance.

Constants

CANDIDATES

Likely candidates to suggest to the user

ENCODINGS

Default encodings to try

Public Instance Methods

display(options) click to toggle source
# File lib/cmess/guess_encoding/manual.rb, line 83
def display(options)
  input, target = CMess.ensure_options!(options, :input, :target_encoding)

  encodings = (options[:encodings] || ENCODINGS) +
              (options[:additional_encodings] || [])

  encodings.concat(all_encodings) if encodings.delete('__ALL__')
  encodings.concat(CANDIDATES)    if encodings.delete('__COMMON__')

  # uniq with additional encodings staying at the end
  encodings.runiq!

  # move target encoding to front
  encodings.in_order!(target)

  max_length, reverse = encodings.max(:length), options[:reverse]

  encodings.each { |encoding|
    args = [target, encoding]
    args.reverse! if reverse

    converted = begin
      input.encode(*args)
    rescue Encoding::UndefinedConversionError => err
      "ILLEGAL INPUT SEQUENCE: #{err.error_char}"
    rescue Encoding::ConverterNotFoundError => err
      err.to_s
    end

    puts "%-#{max_length}s : %s" % [encoding, converted]
  }
end