Sha256: dae19947dc1ceb27ecd3ca964228b48857b35213637db5ed65796182645cb6b1

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require 'time'

module Travis
  module Tools
    class Formatter
      DAY         = 24 * 60 * 60
      TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
      CONFIG_KEYS = %w[rvm gemfile env jdk otp_release php node_js perl python scala
                       compiler os].freeze

      def duration(seconds, suffix = nil)
        return 'none' if seconds.nil?

        seconds          = (Time.now - seconds).to_i if seconds.is_a? Time
        output           = []
        minutes, seconds = seconds.divmod(60)
        hours, minutes   = minutes.divmod(60)
        output << "#{hours} hrs" if hours.positive?
        output << "#{minutes} min" if minutes.positive?
        output << "#{seconds} sec" if seconds.positive? || output.empty?
        output << suffix           if suffix
        output.join(' ')
      end

      def file_size(input, human: true)
        return "#{input} B" unless human

        format = 'B'
        iec    = %w[KiB MiB GiB TiB PiB EiB ZiB YiB]
        while human && (input > 512) && iec.any?
          input /= 1024.0
          format = iec.shift
        end
        input = input.round(2) if input.is_a? Float
        "#{input} #{format}"
      end

      def time(time)
        return 'not yet' if time.nil? # or time > Time.now

        # return duration(time, "ago") if Time.now - time < DAY
        time.localtime.strftime(TIME_FORMAT)
      end

      def job_config(config)
        output = []
        config.each_pair do |key, value|
          output << "#{key}: #{value}" if CONFIG_KEYS.include? key
        end
        output.join(', ')
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
travis-1.14.0 lib/travis/tools/formatter.rb
travis-1.13.3 lib/travis/tools/formatter.rb
travis-1.13.2 lib/travis/tools/formatter.rb
travis-1.13.1 lib/travis/tools/formatter.rb
travis-1.13.0 lib/travis/tools/formatter.rb
travis-1.12.0 lib/travis/tools/formatter.rb