Class: Envirobly::Duration

Inherits:
Object show all
Extended by:
Colorize
Defined in:
lib/envirobly/duration.rb

Constant Summary

Constants included from Colorize

Colorize::BLUE, Colorize::BOLD, Colorize::FAINT, Colorize::GREEN, Colorize::RED, Colorize::RESET, Colorize::YELLOW

Class Method Summary collapse

Methods included from Colorize

faint, green, yellow

Class Method Details

.format_duration(tms) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/envirobly/duration.rb', line 21

def format_duration(tms)
  ms = (tms.real * 1000).to_i

  if ms >= 60_000
    minutes = ms / 60_000
    seconds = (ms % 60_000) / 1000
    sprintf("%dm%ds", minutes, seconds)
  elsif ms >= 1000
    seconds = ms / 1000
    sprintf("%ds", seconds)
  else
    sprintf("%dms", ms)
  end
end

.measure(message = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/envirobly/duration.rb', line 7

def measure(message = nil)
  measurement = Benchmark.measure do
    yield
  end

  duration = format_duration(measurement)

  if message.nil?
    puts [ "", green("✔"), faint(duration) ].join(" ")
  else
    puts sprintf(message, duration)
  end
end