Module: Spill::Spinner

Defined in:
lib/spill/spinner.rb

Constant Summary collapse

FRAMES =
[ "🫖  spilling the tea", "🫖  spilling the tea.", "🫖  spilling the tea..", "🍵  spilling the tea..." ].freeze
INTERVAL =
0.15

Class Method Summary collapse

Class Method Details

.around(enabled:, out: $stderr) ⇒ Object



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

def around(enabled:, out: $stderr)
  return yield unless enabled

  thread = spin(out)
  begin
    yield
  ensure
    thread.kill
    thread.join
    out.print "\r\e[K"
  end
end

.spin(out) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/spill/spinner.rb', line 21

def spin(out)
  Thread.new do
    i = 0
    loop do
      out.print "\r\e[K#{FRAMES[i % FRAMES.size]}"
      i += 1
      sleep INTERVAL
    end
  end
end