Sha256: 4842de5b0b8c8ba2cc780aec35b3cf947477544c28d55ea37edc18125a23c49d

Contents?: true

Size: 1004 Bytes

Versions: 9

Compression:

Stored size: 1004 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 - length(value.to_s)
      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

    def length(str)
      if TablePrint::Config.multibyte
        str.each_char.collect{|c| c.bytesize == 1 ? 1 : 2}.inject(0, &:+)
      else
        str.length
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
table_print-1.5.7 lib/table_print/formatter.rb
table_print-1.5.6 lib/table_print/formatter.rb
table_print-1.5.5 lib/table_print/formatter.rb
table_print-1.5.4 lib/table_print/formatter.rb
table_print-1.5.3 lib/table_print/formatter.rb
table_print-1.5.2 lib/table_print/formatter.rb
table_print-1.5.1 lib/table_print/formatter.rb
table_print-1.5.0 lib/table_print/formatter.rb
table_print-1.4.1 lib/table_print/formatter.rb