shell-spinner 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/lib/shell-spinner.rb CHANGED
@@ -2,58 +2,80 @@ require "shell-spinner/version"
2
2
 
3
3
  module ShellSpinner
4
4
 
5
- def self.wrap text = nil, &block
6
- with_message text do
7
- join_spinner_thread(block)
5
+ class Runner
6
+ def initialize
7
+ require 'stringio'
8
+ @buffer, @original_output = StringIO.new, $stdout
8
9
  end
9
- end
10
10
 
11
- private
11
+ def wrap_block text = nil, &block
12
+ with_message text do
13
+ with_spinner &block
14
+ end
15
+ end
12
16
 
13
- def self.with_message text = nil
14
- require 'colorize'
17
+ private
15
18
 
16
- begin
17
- print "#{text}... " unless text.nil?
19
+ def catch_user_output
20
+ $stdout = @buffer
18
21
  yield
19
- print "done\n".colorize(:green) unless text.nil?
20
- rescue Exception => e
21
- print "fail\n".colorize(:red) unless text.nil?
22
- re_raise_exception e
22
+ ensure
23
+ $stdout = @original_output
23
24
  end
24
- end
25
25
 
26
- def self.join_spinner_thread proc
27
- chars = %w{ | / - \\ }
28
- thread = Thread.new { proc.call }
26
+ def with_message text = nil
27
+ require 'colorize'
29
28
 
30
- while thread.alive?
31
- print chars[0]
32
- sleep 0.1
33
- print "\b"
29
+ begin
30
+ print "#{text}... " unless text.nil?
31
+ catch_user_output { yield }
32
+ print "done\n".colorize(:green) unless text.nil?
33
+ print user_output.colorize(:blue)
34
+ rescue Exception => e
35
+ print "fail\n".colorize(:red) unless text.nil?
36
+ print user_output.colorize(:blue)
37
+ re_raise_exception e
38
+ end
39
+ end
34
40
 
35
- chars.push chars.shift
41
+ def user_output
42
+ @buffer.rewind
43
+ @buffer.read
36
44
  end
37
45
 
38
- thread.join
39
- end
46
+ def with_spinner
47
+ chars = %w{ | / - \\ }
48
+ thread = Thread.new { yield }
49
+
50
+ while thread.alive?
51
+ @original_output.print chars[0]
52
+ sleep 0.1
53
+ @original_output.print "\b"
40
54
 
41
- def self.re_raise_exception e
42
- raise begin
43
- new_exception = build_new_exception(e)
44
- new_exception.set_backtrace e.backtrace
45
- new_exception
55
+ chars.push chars.shift
56
+ end
57
+
58
+ thread.join
46
59
  end
47
- end
48
60
 
49
- # Needs for cases when custom exceptions needs a several required arguments
50
- def self.build_new_exception e
51
- e.class.new(e.message)
52
- rescue
53
- Exception.new e.message
61
+ def re_raise_exception e
62
+ raise begin
63
+ new_exception = build_new_exception(e)
64
+ new_exception.set_backtrace e.backtrace
65
+ new_exception
66
+ end
67
+ end
68
+
69
+ # Needs for cases when custom exceptions needs a several required arguments
70
+ def build_new_exception e
71
+ e.class.new(e.message)
72
+ rescue
73
+ Exception.new e.message
74
+ end
54
75
  end
55
76
  end
56
77
 
57
78
  def ShellSpinner text = nil, &block
58
- ShellSpinner.wrap text, &block
79
+ runner = ShellSpinner::Runner.new
80
+ runner.wrap_block text, &block
59
81
  end
@@ -1,3 +1,3 @@
1
1
  module ShellSpinner
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shell-spinner
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-31 00:00:00.000000000 Z
12
+ date: 2012-12-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colorize
@@ -35,13 +35,9 @@ executables: []
35
35
  extensions: []
36
36
  extra_rdoc_files: []
37
37
  files:
38
- - .gitignore
39
- - Gemfile
40
- - README.markdown
41
- - Rakefile
42
- - lib/shell-spinner.rb
38
+ - README.md
43
39
  - lib/shell-spinner/version.rb
44
- - shell-spinner.gemspec
40
+ - lib/shell-spinner.rb
45
41
  homepage: https://github.com/cheef/shell-spinner
46
42
  licenses: []
47
43
  post_install_message:
data/.gitignore DELETED
@@ -1,5 +0,0 @@
1
- *.gem
2
- .bundle
3
- Gemfile.lock
4
- pkg/*
5
- .idea
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in shell-spinner.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1 +0,0 @@
1
- require "bundler/gem_tasks"
@@ -1,18 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "shell-spinner/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "shell-spinner"
7
- s.version = ShellSpinner::VERSION
8
- s.authors = ["Ivan Garmatenko"]
9
- s.email = %w(cheef.che@gmail.ru)
10
- s.homepage = "https://github.com/cheef/shell-spinner"
11
- s.summary = %q{Animated spinner for shell}
12
- s.description = %q{Gem provides animated spinner for UNIX shell and could be used with rake tasks and any console scripts}
13
-
14
- s.files = `git ls-files`.split("\n")
15
- s.require_paths = %w(lib)
16
-
17
- s.add_runtime_dependency "colorize"
18
- end