Sha256: c0d574693e49201dcf26569121c1a166fcba7f2fdda568f45991391cd249ca31

Contents?: true

Size: 877 Bytes

Versions: 5

Compression:

Stored size: 877 Bytes

Contents

module TablePrint
  class TimeFormatter
    def initialize(time_format=nil)
      @format = time_format
      @format ||= TablePrint::Config.time_format
    end

    def format(value)
      return value unless value.is_a? Time
      value.strftime @format
    end
  end

  class NoNewlineFormatter
    def format(value)
      value.to_s.gsub(/\r\n/, "\n").gsub(/\n/, " ")
    end
  end

  class FixedWidthFormatter
    attr_accessor :width

    def initialize(width)
      self.width = width
    end

    def format(value)
      padding = width - value.to_s.each_char.collect{|c| c.bytesize == 1 ? 1 : 2}.inject(0, &:+)
      truncate(value) + (padding < 0 ? '' : " " * padding)
    end

    private
    def truncate(value)
      return "" unless value

      value = value.to_s
      return value unless value.length > width

      "#{value[0..width-4]}..."
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
table_print-1.4.0 lib/table_print/formatter.rb
table_print-1.3.3 lib/table_print/formatter.rb
table_print-1.3.2 lib/table_print/formatter.rb
table_print-1.3.1 lib/table_print/formatter.rb
table_print-1.3.0 lib/table_print/formatter.rb