spinner.rb 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +18 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/{MIT-LICENSE → LICENSE.txt} +3 -1
- data/lib/spinner.rb +20 -18
- data/spinner.gemspec +23 -0
- data/test/spinner_test.rb +42 -0
- metadata +19 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bda3e4e335db85c46e9609b2c5c5da9b34ecc62b
|
4
|
+
data.tar.gz: 82646380d9692331f3e3e1d1a38e2afa9b6b86dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35af4ca9508c56716f3a402f5734de401e22082d4fbccfd932a56b957baf408b3d7d45e746074be0dc77b10321e45317f7027ab5c0f1d9f75c810a3c197599c8
|
7
|
+
data.tar.gz: 180d71fd9959853b43821693b6c045a5c78339d4d6766d987fd62902dfee74294cad129aca78b2e323d7c910fd37e1c6ce2aec8f1463a451f2e7b491b5313dd0
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
spinner
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0
|
data/{MIT-LICENSE → LICENSE.txt}
RENAMED
data/lib/spinner.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
-
require 'bundler/setup'
|
2
1
|
require 'rake'
|
3
2
|
require 'stringio'
|
4
3
|
|
5
4
|
class Spinner
|
5
|
+
VERSION = "0.3.0".freeze
|
6
|
+
|
6
7
|
attr_reader :queue
|
8
|
+
|
7
9
|
def initialize(*tasks)
|
8
10
|
@queue = tasks
|
9
11
|
@width = 0
|
10
12
|
@chars = %w{ | / - \\ }
|
11
13
|
end
|
12
|
-
|
14
|
+
|
13
15
|
# Injects a new task into the queue.
|
14
16
|
def task(title=nil, task_name=nil, &block)
|
15
17
|
# Handle no task being supplied.
|
@@ -25,7 +27,7 @@ class Spinner
|
|
25
27
|
elsif task_name
|
26
28
|
task_block = Rake::Task[task_name]
|
27
29
|
end
|
28
|
-
|
30
|
+
|
29
31
|
# Inject the task into the queue.
|
30
32
|
@queue << [ title, task_block ]
|
31
33
|
|
@@ -37,57 +39,57 @@ class Spinner
|
|
37
39
|
# Return the new list of tasks.
|
38
40
|
@queue.map(&:first)
|
39
41
|
end
|
40
|
-
|
42
|
+
|
41
43
|
# Starts executing the queued tasks.
|
42
44
|
def spin!
|
43
45
|
# Handle no tasks in the queue.
|
44
46
|
return unless @queue.any?
|
45
|
-
|
47
|
+
|
46
48
|
# Mark the current time in order to calculate the total duration.
|
47
49
|
start_time = Time.now
|
48
50
|
|
49
51
|
# Pluralize the number of tasks in the queue.
|
50
52
|
task_counter = "#{@queue.size} task" << (@queue.size == 1 ? '' : 's')
|
51
|
-
|
53
|
+
|
52
54
|
# Update the print width.
|
53
55
|
@width = (@width + (@queue.size.to_s.length + 2) * 2) + 1
|
54
|
-
|
56
|
+
|
55
57
|
# Execute each task in sequence.
|
56
58
|
@queue.each_with_index do |task, i|
|
57
59
|
run_task(task, i+1)
|
58
60
|
end
|
59
|
-
|
61
|
+
|
60
62
|
# Reset this spinner instance so that it can be reused.
|
61
63
|
reset!
|
62
64
|
|
63
65
|
# Mark the completion time and calculate the duration.
|
64
66
|
end_time = Time.now
|
65
67
|
time_taken = distance_of_time_in_words(start_time, end_time)
|
66
|
-
|
68
|
+
|
67
69
|
# Print the completion message.
|
68
70
|
print("Done! #{task_counter} completed in #{time_taken} :-)\n")
|
69
71
|
end
|
70
|
-
|
72
|
+
|
71
73
|
private
|
72
|
-
|
74
|
+
|
73
75
|
# Outputs to the console.
|
74
76
|
def print(*args)
|
75
77
|
STDOUT.print(*args)
|
76
78
|
end
|
77
|
-
|
79
|
+
|
78
80
|
# Clears the current printed output.
|
79
81
|
def clear
|
80
82
|
print("\r")
|
81
83
|
print(" ".ljust(@width + 5))
|
82
84
|
print("\r")
|
83
85
|
end
|
84
|
-
|
86
|
+
|
85
87
|
# Reset this spinner instance to defaults.
|
86
88
|
def reset!
|
87
89
|
@width = 0
|
88
90
|
@queue = []
|
89
91
|
end
|
90
|
-
|
92
|
+
|
91
93
|
# Executes a single task.
|
92
94
|
def run_task(item, counter)
|
93
95
|
# Extract the title & task block.
|
@@ -95,7 +97,7 @@ class Spinner
|
|
95
97
|
|
96
98
|
# Print the task counter and title.
|
97
99
|
print("#{counter}/#{queue.size}: #{title}".ljust(@width, '.') + '... ')
|
98
|
-
|
100
|
+
|
99
101
|
# Begin a new thread to update the printed output while
|
100
102
|
# the task runs.
|
101
103
|
t = Thread.new {
|
@@ -107,14 +109,14 @@ class Spinner
|
|
107
109
|
task.call
|
108
110
|
end
|
109
111
|
}
|
110
|
-
|
112
|
+
|
111
113
|
# Run the spinner for the duration of the task,
|
112
114
|
# then clear the output.
|
113
115
|
spin while t.alive?
|
114
116
|
t.join
|
115
117
|
clear
|
116
118
|
end
|
117
|
-
|
119
|
+
|
118
120
|
# Update the position of the spinner.
|
119
121
|
def spin
|
120
122
|
print(@chars[0]) # Print the next character...
|
@@ -144,4 +146,4 @@ class Spinner
|
|
144
146
|
else "over an hour"
|
145
147
|
end
|
146
148
|
end
|
147
|
-
end
|
149
|
+
end
|
data/spinner.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'spinner.rb'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "spinner.rb"
|
8
|
+
spec.version = Spinner::VERSION
|
9
|
+
spec.authors = ["Mike Fulcher"]
|
10
|
+
spec.email = ["mike@drawingablank.me"]
|
11
|
+
spec.summary = "Ruby progress spinner for tasks with an unknown duration"
|
12
|
+
spec.description = "Ruby progress spinner for tasks with an unknown duration."
|
13
|
+
spec.homepage = "http://drawingablank.me/blog/spinner-a-ruby-gem-for-tasks-with-unknown-duration.html"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "minitest"
|
23
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require File.join(__FILE__, '../../lib/spinner')
|
3
|
+
|
4
|
+
class SpinnerTest < Minitest::Test
|
5
|
+
|
6
|
+
GLOBAL = []
|
7
|
+
|
8
|
+
def setup
|
9
|
+
STDOUT.puts("\n")
|
10
|
+
@spinner = Spinner.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_adding_block_tasks
|
14
|
+
@spinner.task { sleep(0.1) }
|
15
|
+
assert @spinner.queue.size == 1
|
16
|
+
assert @spinner.queue.first[1].respond_to?(:call)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_adding_rake_tasks
|
20
|
+
@spinner.task(nil, "test")
|
21
|
+
assert @spinner.queue.size == 1
|
22
|
+
assert Rake::Task.tasks.include?(@spinner.queue.first[1])
|
23
|
+
Rake::Task.clear
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_adding_initial_tasks
|
27
|
+
tasks = []
|
28
|
+
tasks << [ "One", lambda { sleep(0.1) } ]
|
29
|
+
spinner = Spinner.new(*tasks)
|
30
|
+
assert spinner.queue.first[0] == "One"
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_executing_tasks
|
34
|
+
@spinner.task { GLOBAL << '1' }
|
35
|
+
@spinner.task { GLOBAL << '2' }
|
36
|
+
@spinner.task { GLOBAL << '3' }
|
37
|
+
@spinner.spin!
|
38
|
+
assert GLOBAL.size == 3
|
39
|
+
GLOBAL.slice!(0, 3)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spinner.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Fulcher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -39,18 +39,26 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: Ruby progress spinner for tasks with an unknown duration.
|
42
|
-
email:
|
42
|
+
email:
|
43
|
+
- mike@drawingablank.me
|
43
44
|
executables: []
|
44
45
|
extensions: []
|
45
46
|
extra_rdoc_files: []
|
46
47
|
files:
|
47
|
-
-
|
48
|
-
-
|
49
|
-
-
|
48
|
+
- .gitignore
|
49
|
+
- .ruby-gemset
|
50
|
+
- .ruby-version
|
50
51
|
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
51
54
|
- README.md
|
52
|
-
|
53
|
-
|
55
|
+
- Rakefile
|
56
|
+
- lib/spinner.rb
|
57
|
+
- spinner.gemspec
|
58
|
+
- test/spinner_test.rb
|
59
|
+
homepage: http://drawingablank.me/blog/spinner-a-ruby-gem-for-tasks-with-unknown-duration.html
|
60
|
+
licenses:
|
61
|
+
- MIT
|
54
62
|
metadata: {}
|
55
63
|
post_install_message:
|
56
64
|
rdoc_options: []
|
@@ -68,8 +76,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
76
|
version: '0'
|
69
77
|
requirements: []
|
70
78
|
rubyforge_project:
|
71
|
-
rubygems_version: 2.
|
79
|
+
rubygems_version: 2.1.10
|
72
80
|
signing_key:
|
73
81
|
specification_version: 4
|
74
82
|
summary: Ruby progress spinner for tasks with an unknown duration
|
75
|
-
test_files:
|
83
|
+
test_files:
|
84
|
+
- test/spinner_test.rb
|