Skip to content

Commit

Permalink
Catching all user output to prevent spinner failure
Browse files Browse the repository at this point in the history
  • Loading branch information
cheef committed Dec 29, 2012
1 parent 93140db commit 14b7a66
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 45 deletions.
File renamed without changes.
94 changes: 58 additions & 36 deletions lib/shell-spinner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,80 @@

module ShellSpinner

def self.wrap text = nil, &block
with_message text do
join_spinner_thread(block)
class Runner
def initialize
require 'stringio'
@buffer, @original_output = StringIO.new, $stdout
end
end

private
def wrap_block text = nil, &block
with_message text do
with_spinner &block
end
end

def self.with_message text = nil
require 'colorize'
private

begin
print "#{text}... " unless text.nil?
def catch_user_output
$stdout = @buffer
yield
print "done\n".colorize(:green) unless text.nil?
rescue Exception => e
print "fail\n".colorize(:red) unless text.nil?
re_raise_exception e
ensure
$stdout = @original_output
end
end

def self.join_spinner_thread proc
chars = %w{ | / - \\ }
thread = Thread.new { proc.call }
def with_message text = nil
require 'colorize'

while thread.alive?
print chars[0]
sleep 0.1
print "\b"
begin
print "#{text}... " unless text.nil?
catch_user_output { yield }
print "done\n".colorize(:green) unless text.nil?
print user_output.colorize(:blue)
rescue Exception => e
print "fail\n".colorize(:red) unless text.nil?
print user_output.colorize(:blue)
re_raise_exception e
end
end

chars.push chars.shift
def user_output
@buffer.rewind
@buffer.read
end

thread.join
end
def with_spinner
chars = %w{ | / - \\ }
thread = Thread.new { yield }

while thread.alive?
@original_output.print chars[0]
sleep 0.1
@original_output.print "\b"

def self.re_raise_exception e
raise begin
new_exception = build_new_exception(e)
new_exception.set_backtrace e.backtrace
new_exception
chars.push chars.shift
end

thread.join
end
end

# Needs for cases when custom exceptions needs a several required arguments
def self.build_new_exception e
e.class.new(e.message)
rescue
Exception.new e.message
def re_raise_exception e
raise begin
new_exception = build_new_exception(e)
new_exception.set_backtrace e.backtrace
new_exception
end
end

# Needs for cases when custom exceptions needs a several required arguments
def build_new_exception e
e.class.new(e.message)
rescue
Exception.new e.message
end
end
end

def ShellSpinner text = nil, &block
ShellSpinner.wrap text, &block
runner = ShellSpinner::Runner.new
runner.wrap_block text, &block
end
2 changes: 1 addition & 1 deletion lib/shell-spinner/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ShellSpinner
VERSION = "1.0.3"
VERSION = "1.0.4"
end
16 changes: 8 additions & 8 deletions shell-spinner.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ $:.push File.expand_path("../lib", __FILE__)
require "shell-spinner/version"

Gem::Specification.new do |s|
s.name = "shell-spinner"
s.version = ShellSpinner::VERSION
s.authors = ["Ivan Garmatenko"]
s.email = %w(cheef.che@gmail.ru)
s.homepage = "https://github.com/cheef/shell-spinner"
s.summary = %q{Animated spinner for shell}
s.description = %q{Gem provides animated spinner for UNIX shell and could be used with rake tasks and any console scripts}
s.name = "shell-spinner"
s.version = ShellSpinner::VERSION
s.authors = ["Ivan Garmatenko"]
s.email = %w(cheef.che@gmail.ru)
s.homepage = "https://github.com/cheef/shell-spinner"
s.summary = %q{Animated spinner for shell}
s.description = %q{Gem provides animated spinner for UNIX shell and could be used with rake tasks and any console scripts}

s.files = `git ls-files`.split("\n")
s.files = Dir['README.md', 'lib/**/*']
s.require_paths = %w(lib)

s.add_runtime_dependency "colorize"
Expand Down

0 comments on commit 14b7a66

Please sign in to comment.