module Terrestrial::Cli::TerminalUI

Public Class Methods

show_spinner(fps = 10) { || ... } click to toggle source
# File lib/terrestrial/cli/terminal_ui.rb, line 5
def self.show_spinner(fps = 10)
  chars = %w[| / - \\]
  delay = 1.0/fps
  iter = 0
  spinner = Thread.new do
    while iter do  # Keep spinning until told otherwise
      print chars[(iter+=1) % chars.length]
      sleep delay
      print "\b"
      print " "
      print "\b"
    end
  end
  yield.tap do     # After yielding to the block, save the return value
    iter = false   # Tell the thread to exit, cleaning up after itself…
    spinner.join   # …and wait for it to do so.
  end