Class: Spinner

Inherits:
Object
  • Object
show all
Defined in:
lib/gitpusshuten/helpers/spinner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &code) ⇒ Spinner

Loadify Method

* block of code you want to provide a loader for

Options

* :message  => 'Message to be displayed while processing'
* :complete => 'Message to be displayed after processing, regardless of the returned value'
* :return   => 'When set to true, it will display the returned value of the executed code'


13
14
15
16
17
18
19
20
21
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
64
65
66
67
68
69
70
71
72
# File 'lib/gitpusshuten/helpers/spinner.rb', line 13

def initialize(options = {}, &code)
  
  ##
  # Character to loop through (loader)
  characters = %w[| / - \\ | / - \\]

  ##
  # Create a new thread to run the provided block of code in
  thread = Thread.new do
    code.call
  end

  ##
  # Enter a while loop and stay in it until the thread
  # finished processing the provided code. This will display
  # and animate the loader meanwhile.
  while thread.alive?
    next_character = characters.shift
    message = "#{next_character} #{options[:message] || ''}"
    print message
    characters << next_character
    sleep 0.065
    print "\b" * message.length
  end

  ##
  # Extract the value from the dead thread
  returned_value = thread.value

  ##
  # Print Final Message
  print "#{options[:message]}  "

  ##
  # Print On Complete Message if options is set to true
  unless options[:complete].nil?
    print options[:complete]
    print " "
  end

  ##
  # Prints the returned value from the code block
  # that was executed if set to true
  if options[:return]
    if options[:put]
      puts "\n" + returned_value
    else
      print returned_value
    end
  end
  
  ##
  # Add a new line
  print "\n"
  
  ##
  # Return the value from the dead thread
  returned_value
  
end

Class Method Details

.configuring(options = {}, &code) ⇒ Object



86
87
88
# File 'lib/gitpusshuten/helpers/spinner.rb', line 86

def self.configuring(options = {}, &code)
  Spinner.new({:message => "Configuring..", :complete => 'Done!'.color(:green)}.merge(options), &code)
end

.installing(options = {}, &code) ⇒ Object



78
79
80
# File 'lib/gitpusshuten/helpers/spinner.rb', line 78

def self.installing(options = {}, &code)
  Spinner.new({:message => "Installing..", :complete => 'Done!'.color(:green)}.merge(options), &code)
end

.installing_a_while(options = {}, &code) ⇒ Object



82
83
84
# File 'lib/gitpusshuten/helpers/spinner.rb', line 82

def self.installing_a_while(options = {}, &code)
  Spinner.new({:message => "Installing, this may take a while..", :complete => 'Done!'.color(:green)}.merge(options), &code)
end

.loading(options = {}, &code) ⇒ Object



94
95
96
# File 'lib/gitpusshuten/helpers/spinner.rb', line 94

def self.loading(options = {}, &code)
  Spinner.new({:message => "Loading..", :complete => 'Done!'.color(:green)}.merge(options), &code)
end

.return(options = {}, &code) ⇒ Object



74
75
76
# File 'lib/gitpusshuten/helpers/spinner.rb', line 74

def self.return(options = {}, &code)
  Spinner.new({:return => true}.merge(options), &code)
end

.updating(options = {}, &code) ⇒ Object



90
91
92
# File 'lib/gitpusshuten/helpers/spinner.rb', line 90

def self.updating(options = {}, &code)
  Spinner.new({:message => "Updating..", :complete => 'Done!'.color(:green)}.merge(options), &code)
end