Module: Progressor::Formatting

Included in:
Progressor, LimitedSequence, UnlimitedSequence
Defined in:
lib/progressor/formatting.rb

Instance Method Summary collapse

Instance Method Details

#format_float(value) ⇒ Object

:nodoc:



35
36
37
# File 'lib/progressor/formatting.rb', line 35

def format_float(value)
  sprintf("%0.2f", value)
end

#format_int(value) ⇒ Object

:nodoc:



30
31
32
# File 'lib/progressor/formatting.rb', line 30

def format_int(value)
  sprintf("%02d", value)
end

#format_time(time) ⇒ Object

Formats the given time in seconds to something human readable. Examples:

  • 1 second: 1.00s

  • 0.123 seconds: 123.00ms

  • 100 seconds: 01m:40s

  • 101.5 seconds: 01m:41s

  • 3661 seconds: 01h:01m:01s



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/progressor/formatting.rb', line 10

def format_time(time)
  return "?s" if time.nil?

  if time < 1
    "#{format_float((time * 1000).round(2))}ms"
  elsif time < 60
    "#{format_float(time.round(2))}s"
  elsif time < 3600
    minutes = time.to_i / 60
    seconds = (time - minutes * 60).round(2)
    "#{format_int(minutes)}m:#{format_int(seconds)}s"
  else
    hours = time.to_i / 3600
    minutes = (time.to_i % 3600) / 60
    seconds = (time - (hours * 3600 + minutes * 60)).round(2)
    "#{format_int(hours)}h:#{format_int(minutes)}m:#{format_int(seconds)}s"
  end
end