Class: Rbtclk::CountdownTimer

Inherits:
Object
  • Object
show all
Includes:
ColorCode
Defined in:
lib/rbtclk/countdown_timer.rb

Instance Method Summary collapse

Methods included from ColorCode

#translate

Constructor Details

#initialize(font: "clb8x8", format: "%X", color: "black", time: 180) ⇒ CountdownTimer

Returns a new instance of CountdownTimer.



14
15
16
17
18
19
20
# File 'lib/rbtclk/countdown_timer.rb', line 14

def initialize(font: "clb8x8", format: "%X", color: "black", time: 180)
  @artii = Artii::Base.new(font: font)
  @format = format
  @color = color
  @time = time.to_i
  @elapsed = 0
end

Instance Method Details

#showObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbtclk/countdown_timer.rb', line 22

def show
  Curses.init_screen
  Curses.start_color
  Curses.use_default_colors
  Curses.init_pair(1, translate(@color), -1)
  Curses.init_pair(2, translate("magenta"), translate("black"))
  Curses.curs_set(0)

  @start_time = Time.now + 1

  begin
    view_thread = Thread.new do
      while @elapsed < @time
        refresh
        @elapsed += 1
        sleep 1
      end

      toggle_marker = true
      loop do
        blink(toggle_marker)
        is_even = !is_even
        sleep 1
      end
    end

    input_thread = Thread.new do
      loop do
        case Curses.getch
        when "q"
          Thread.kill(view_thread)
          Thread.kill(input_thread)
        end
      end
    end

    view_thread.join
    input_thread.join
  ensure
    Curses.close_screen
  end
end