Module: Fixy::Formatter::Alphanumeric

Defined in:
lib/fixy/formatter/alphanumeric.rb

Instance Method Summary collapse

Instance Method Details

#format_alphanumeric(input, byte_width) ⇒ Object

Alphanumeric Formatter

Only contains printable characters and is left-justified and filled with spaces.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fixy/formatter/alphanumeric.rb', line 12

def format_alphanumeric(input, byte_width)
  input_string = String.new(input.to_s).tr "#{self.class::LINE_ENDING_CRLF}#{line_ending}", ''
  result = ''

  if input_string.bytesize <= byte_width
    result << input_string
  else
    input_string.each_char do |char|
      if result.bytesize + char.bytesize <= byte_width
        result << char
      else
        break
      end
    end
  end

  result << ' ' * (byte_width - result.bytesize)
end