Sha256: fa05c17c1280512a7bce7e50d39a8f909477ec39a65084943746537b1d59780d

Contents?: true

Size: 759 Bytes

Versions: 3

Compression:

Stored size: 759 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)
      "%-#{width}s" % truncate(value)
    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

3 entries across 3 versions & 1 rubygems

Version Path
table_print-1.2.0 lib/table_print/formatter.rb
table_print-1.1.5 lib/table_print/formatter.rb
table_print-1.1.4 lib/table_print/formatter.rb