Class: Strobe::CLI::Ticker

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/cli/ticker.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTicker

Returns a new instance of Ticker.



12
13
14
15
# File 'lib/strobe/cli/ticker.rb', line 12

def initialize
  @thread
  @read, @write = IO.pipe
end

Class Method Details

.tick(msg, interval = 0.5) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/strobe/cli/ticker.rb', line 3

def self.tick(msg, interval = 0.5)
  ticker = new
  ticker.tick(msg, interval)
  return ticker unless block_given?
  yield
ensure
  ticker.stop
end

Instance Method Details

#stopObject



35
36
37
38
39
40
41
42
# File 'lib/strobe/cli/ticker.rb', line 35

def stop
  raise "Ticker is not ticking" unless @thread

  @write << '1'
  @write.close
  @thread.join
  true
end

#tick(msg, interval = 0.5) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/strobe/cli/ticker.rb', line 17

def tick(msg, interval = 0.5)
  @thread = Thread.new do
    print "#{msg}..."

    while true
      break if IO.select( [ @read ], nil, nil, interval )
      print "."
    end

    @read.read
    @read.close

    puts
  end

  true
end