spinner.rb 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ae85912762746269f1904e657622d271850b2cb
4
- data.tar.gz: 4ab07fad82243fd0ea66517bf78b0e28d1e1cf24
3
+ metadata.gz: bda3e4e335db85c46e9609b2c5c5da9b34ecc62b
4
+ data.tar.gz: 82646380d9692331f3e3e1d1a38e2afa9b6b86dd
5
5
  SHA512:
6
- metadata.gz: fc3d11b44f065a65dfdc8f9fb8da5a3bfd8c99518345dd043d9a26a637cca7d1a2320b13f709aff69eec9347e0d44ff9ef4e2406511effa99191f371d9ffb4a1
7
- data.tar.gz: 1d18ccfb5db6ca8f503fac6ff8582d355b6a86cb998d54be5ff06bc28960e267503734bfcd8ec68ffbb8b3a582698adb94c2afb363626d3ab533f52c323dc664
6
+ metadata.gz: 35af4ca9508c56716f3a402f5734de401e22082d4fbccfd932a56b957baf408b3d7d45e746074be0dc77b10321e45317f7027ab5c0f1d9f75c810a3c197599c8
7
+ data.tar.gz: 180d71fd9959853b43821693b6c045a5c78339d4d6766d987fd62902dfee74294cad129aca78b2e323d7c910fd37e1c6ce2aec8f1463a451f2e7b491b5313dd0
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ TODO
@@ -0,0 +1 @@
1
+ spinner
@@ -0,0 +1 @@
1
+ ruby-2.0.0
@@ -1,4 +1,6 @@
1
- Copyright 2011 Mike Fulcher
1
+ Copyright (c) 2013 Mike Fulcher
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
@@ -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
@@ -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.2.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-07-08 00:00:00.000000000 Z
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
- - lib/spinner.rb
48
- - MIT-LICENSE
49
- - Rakefile
48
+ - .gitignore
49
+ - .ruby-gemset
50
+ - .ruby-version
50
51
  - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
51
54
  - README.md
52
- homepage:
53
- licenses: []
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.0.3
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