Class: Timeru::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/timeru/timer.rb

Instance Method Summary collapse

Constructor Details

#initialize(hours = 0, minutes = 0, seconds) ⇒ Timer

Returns a new instance of Timer.



6
7
8
# File 'lib/timeru/timer.rb', line 6

def initialize hours=0, minutes=0, seconds
  @time = hours * 3600 + minutes * 60 + seconds
end

Instance Method Details

#create_display(seconds) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/timeru/timer.rb', line 23

def create_display seconds
  seconds_display = seconds % 60
  minutes_display = (seconds / 60) % 60
  hours_dispaly = (seconds / 60) / 60
  seconds_display = seconds_display.to_i
  minutes_display = minutes_display.to_i
  hours_dispaly = hours_dispaly.to_i

  string =  "%02d:" %[hours_dispaly]
  string << "%02d:" %[minutes_display]
  string << "%02d" %[seconds_display]

  return string
end

#startObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/timeru/timer.rb', line 10

def start
  puts "..."
  cursor = TTY::Cursor
  @time.downto 0 do |sec|
    print cursor.prev_line
    print cursor.clear_line
    puts create_display(sec)
    sleep 1
  end
  puts "done"
end