Class: Spinner
- Inherits:
-
Object
- Object
- Spinner
- Defined in:
- lib/gitpusshuten/helpers/spinner.rb
Class Method Summary collapse
- .configuring(options = {}, &code) ⇒ Object
- .installing(options = {}, &code) ⇒ Object
- .installing_a_while(options = {}, &code) ⇒ Object
- .loading(options = {}, &code) ⇒ Object
- .return(options = {}, &code) ⇒ Object
- .updating(options = {}, &code) ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}, &code) ⇒ Spinner
constructor
Loadify Method * block of code you want to provide a loader for.
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( = {}, &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 = "#{next_character} #{[:message] || ''}" print characters << next_character sleep 0.065 print "\b" * .length end ## # Extract the value from the dead thread returned_value = thread.value ## # Print Final Message print "#{[:message]} " ## # Print On Complete Message if options is set to true unless [:complete].nil? print [:complete] print " " end ## # Prints the returned value from the code block # that was executed if set to true if [:return] if [: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( = {}, &code) Spinner.new({:message => "Configuring..", :complete => 'Done!'.color(:green)}.merge(), &code) end |
.installing(options = {}, &code) ⇒ Object
78 79 80 |
# File 'lib/gitpusshuten/helpers/spinner.rb', line 78 def self.installing( = {}, &code) Spinner.new({:message => "Installing..", :complete => 'Done!'.color(:green)}.merge(), &code) end |
.installing_a_while(options = {}, &code) ⇒ Object
82 83 84 |
# File 'lib/gitpusshuten/helpers/spinner.rb', line 82 def self.installing_a_while( = {}, &code) Spinner.new({:message => "Installing, this may take a while..", :complete => 'Done!'.color(:green)}.merge(), &code) end |
.loading(options = {}, &code) ⇒ Object
94 95 96 |
# File 'lib/gitpusshuten/helpers/spinner.rb', line 94 def self.loading( = {}, &code) Spinner.new({:message => "Loading..", :complete => 'Done!'.color(:green)}.merge(), &code) end |