tty-spinner 0.5.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fcc097a53cf0eecf886ee4784e264033b035e5f
4
- data.tar.gz: 749c0703578a4f0fd260c930eee3f1adf7e6c502
3
+ metadata.gz: ec461cddc1750681eca2382e8dff48841aa7dd95
4
+ data.tar.gz: 0c83312cf2d05f2c359b9b871cf5d2316078d66e
5
5
  SHA512:
6
- metadata.gz: eb546b547b98b0630006660873fe0224ac839c82c67481d0b3711a453898bee0601f037fdbac596cbff54f1a33d62efb951f084c15a8100459a57ac08023aefb
7
- data.tar.gz: c8b6121dccdbfbe8d3f0e1f04f662df51940d1ef2b3d42e227835e2c6394f95399bd9ed30db759be67bd615be19c81bbbb812781619f3db26ff5ee4dd0ac45a7
6
+ metadata.gz: e26527de9ece30e50817e6c2f96caa165e93146aaadedf3c5be9327a218ddb1c1be7f00a9f802b944448946ae48876c46f3b3de39b98d75e9d8225f5395db387
7
+ data.tar.gz: 0c6530fb8490ef81e9c8ae787adbd0cc342775c4b5e1d09b2755029f21c2b884fc1c6360db3eaf44a0ec83dca7e5bc9e22865673d913729330067914f9128942
data/README.md CHANGED
@@ -127,9 +127,9 @@ sp2.success
127
127
  which when done will display:
128
128
 
129
129
  ```ruby
130
- ┌[✔]] top
131
- ├──[✔] one
132
- └──[✔] two
130
+ [✔] top
131
+ ├── [✔] one
132
+ └── [✔] two
133
133
  ```
134
134
 
135
135
  For more usage examples please see [examples directory](https://github.com/piotrmurach/tty-spinner/tree/master/examples)
data/lib/tty/spinner.rb CHANGED
@@ -24,7 +24,7 @@ module TTY
24
24
  TICK = '✔'.freeze
25
25
  CROSS = '✖'.freeze
26
26
 
27
- CURSOR_USAGE_LOCK = Monitor.new
27
+ CURSOR_LOCK = Monitor.new
28
28
 
29
29
  # The object that responds to print call defaulting to stderr
30
30
  #
@@ -109,20 +109,18 @@ module TTY
109
109
  @thread = nil
110
110
  @job = nil
111
111
  @multispinner= nil
112
- @index = nil
112
+ @row = nil
113
113
  @succeeded = false
114
- @first_run = true
114
+ @first_run = true
115
115
  end
116
116
 
117
117
  # Notifies the TTY::Spinner that it is running under a multispinner
118
118
  #
119
119
  # @param [TTY::Spinner::Multi] the multispinner that it is running under
120
- # @param [Integer] the index of this spinner in the multispinner
121
120
  #
122
121
  # @api private
123
- def add_multispinner(multispinner, index)
122
+ def add_multispinner(multispinner)
124
123
  @multispinner = multispinner
125
- @index = index
126
124
  end
127
125
 
128
126
  # Whether the spinner has completed spinning
@@ -165,6 +163,11 @@ module TTY
165
163
 
166
164
  # Register callback
167
165
  #
166
+ # @param [Symbol] name
167
+ # the name for the event to listen for, e.i. :complete
168
+ #
169
+ # @return [self]
170
+ #
168
171
  # @api public
169
172
  def on(name, &block)
170
173
  synchronize do
@@ -219,7 +222,7 @@ module TTY
219
222
  #
220
223
  # @api public
221
224
  def auto_spin
222
- CURSOR_USAGE_LOCK.synchronize do
225
+ CURSOR_LOCK.synchronize do
223
226
  start
224
227
  sleep_time = 1.0 / @interval
225
228
 
@@ -465,14 +468,14 @@ module TTY
465
468
  # @api private
466
469
  def execute_on_line
467
470
  if @multispinner
468
- CURSOR_USAGE_LOCK.synchronize do
469
- lines_up = @multispinner.count_line_offset(@index)
470
-
471
+ CURSOR_LOCK.synchronize do
471
472
  if @first_run
473
+ @row = @multispinner.next_row
472
474
  yield if block_given?
473
475
  output.print "\n"
474
476
  @first_run = false
475
477
  else
478
+ lines_up = (@multispinner.rows + 1) - @row
476
479
  output.print TTY::Cursor.save
477
480
  output.print TTY::Cursor.up(lines_up)
478
481
  yield if block_given?
@@ -492,11 +495,9 @@ module TTY
492
495
  def write(data, clear_first = false)
493
496
  execute_on_line do
494
497
  output.print(TTY::Cursor.column(1)) if clear_first
495
-
496
498
  # If there's a top level spinner, print with inset
497
- characters_in = @multispinner.nil? ? "" : @multispinner.line_inset(self)
498
-
499
- output.print(characters_in + data)
499
+ characters_in = @multispinner.line_inset(self) if @multispinner
500
+ output.print("#{characters_in}#{data}")
500
501
  output.flush
501
502
  end
502
503
  end
@@ -505,8 +506,8 @@ module TTY
505
506
  #
506
507
  # @api private
507
508
  def emit(name, *args)
508
- @callbacks[name].each do |block|
509
- block.call(*args)
509
+ @callbacks[name].each do |callback|
510
+ callback.call(*args)
510
511
  end
511
512
  end
512
513
 
@@ -19,10 +19,15 @@ module TTY
19
19
 
20
20
  DEFAULT_INSET = {
21
21
  top: Gem.win_platform? ? '+ ' : "\u250c ",
22
- middle: Gem.win_platform? ? '|-- ' : "\u251c\u2500\u2500",
23
- bottom: Gem.win_platform? ? '|__ ' : "\u2514\u2500\u2500"
22
+ middle: Gem.win_platform? ? '|-- ' : "\u251c\u2500\u2500 ",
23
+ bottom: Gem.win_platform? ? '|__ ' : "\u2514\u2500\u2500 "
24
24
  }.freeze
25
25
 
26
+ # The current count of all rendered rows
27
+ #
28
+ # @api public
29
+ attr_reader :rows
30
+
26
31
  # Initialize a multispinner
27
32
  #
28
33
  # @example
@@ -59,6 +64,7 @@ module TTY
59
64
  error: [],
60
65
  done: []
61
66
  }
67
+ @rows = 0
62
68
  end
63
69
 
64
70
  # Register a new spinner
@@ -71,9 +77,9 @@ module TTY
71
77
  spinner = TTY::Spinner.new(pattern, @options.merge(options))
72
78
 
73
79
  @create_spinner_lock.synchronize do
74
- spinner.add_multispinner(self, @spinners.length)
80
+ spinner.add_multispinner(self)
75
81
  spinner.job(&job) if block_given?
76
- observe_events(spinner) if @top_spinner
82
+ observe(spinner)
77
83
  @spinners << spinner
78
84
  if @top_spinner
79
85
  @spinners.each { |sp| sp.redraw_indent if sp.spinning? || sp.done? }
@@ -83,16 +89,13 @@ module TTY
83
89
  spinner
84
90
  end
85
91
 
86
- # Observe all child events to notify top spinner of current state
92
+ # Increase a row count
87
93
  #
88
- # @param [TTY::Spinner] spinner
89
- # the spinner to listen to for events
90
- #
91
- # @api private
92
- def observe_events(spinner)
93
- spinner.on(:success) { @top_spinner.success if success? }
94
- .on(:error) { @top_spinner.error if error? }
95
- .on(:done) { @top_spinner.stop if done? && !success? && !error? }
94
+ # @api public
95
+ def next_row
96
+ @create_spinner_lock.synchronize do
97
+ @rows += 1
98
+ end
96
99
  end
97
100
 
98
101
  # Get the top level spinner if it exists
@@ -124,6 +127,15 @@ module TTY
124
127
  jobs.each(&:join)
125
128
  end
126
129
 
130
+ # Perform a single spin animation
131
+ #
132
+ # @api public
133
+ def spin
134
+ raise "No top level spinner" if @top_spinner.nil?
135
+
136
+ @top_spinner.spin
137
+ end
138
+
127
139
  # Pause all spinners
128
140
  #
129
141
  # @api public
@@ -138,27 +150,6 @@ module TTY
138
150
  @spinners.dup.each(&:resume)
139
151
  end
140
152
 
141
- # Find relative offset position to which to move the current cursor
142
- #
143
- # The position is found among the registered spinners given the current
144
- # position the spinner is at provided its index
145
- #
146
- # @param [Integer] index
147
- # the position to search from
148
- #
149
- # @return [Integer]
150
- # the current position
151
- #
152
- # @api public
153
- def count_line_offset(index)
154
- Array(@spinners[index..-1]).reduce(0) do |acc, spinner|
155
- if spinner.spinning? || spinner.done?
156
- acc += 1
157
- end
158
- acc
159
- end
160
- end
161
-
162
153
  # Find the number of characters to move into the line
163
154
  # before printing the spinner
164
155
  #
@@ -213,8 +204,8 @@ module TTY
213
204
  #
214
205
  # @api public
215
206
  def stop
207
+ @top_spinner.stop if @top_spinner
216
208
  @spinners.dup.each(&:stop)
217
- emit :done
218
209
  end
219
210
 
220
211
  # Stop all spinners with success status
@@ -223,7 +214,6 @@ module TTY
223
214
  def success
224
215
  @top_spinner.success if @top_spinner
225
216
  @spinners.dup.each(&:success)
226
- emit :success
227
217
  end
228
218
 
229
219
  # Stop all spinners with error status
@@ -232,7 +222,6 @@ module TTY
232
222
  def error
233
223
  @top_spinner.error if @top_spinner
234
224
  @spinners.dup.each(&:error)
235
- emit :error
236
225
  end
237
226
 
238
227
  # Listen on event
@@ -254,6 +243,54 @@ module TTY
254
243
  block.call(*args)
255
244
  end
256
245
  end
246
+
247
+ # Observe spinner for events to notify top spinner of current state
248
+ #
249
+ # @param [TTY::Spinner] spinner
250
+ # the spinner to listen to for events
251
+ #
252
+ # @api private
253
+ def observe(spinner)
254
+ spinner.on(:success, &success_handler)
255
+ .on(:error, &error_handler)
256
+ .on(:done, &done_handler)
257
+ end
258
+
259
+ # Handle the success state
260
+ #
261
+ # @api private
262
+ def success_handler
263
+ proc do
264
+ if success?
265
+ @top_spinner.success if @top_spinner
266
+ emit(:success)
267
+ end
268
+ end
269
+ end
270
+
271
+ # Handle the error state
272
+ #
273
+ # @api private
274
+ def error_handler
275
+ proc do
276
+ if error?
277
+ @top_spinner.error if @top_spinner
278
+ @fired ||= emit(:error) # fire once
279
+ end
280
+ end
281
+ end
282
+
283
+ # Handle the done state
284
+ #
285
+ # @api private
286
+ def done_handler
287
+ proc do
288
+ if done?
289
+ @top_spinner.done if @top_spinner && !error? && !success?
290
+ emit(:done)
291
+ end
292
+ end
293
+ end
257
294
  end # MultiSpinner
258
295
  end # Spinner
259
296
  end # TTY
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Spinner
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.0"
6
6
  end # Spinner
7
7
  end # TTY
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-spinner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-09 00:00:00.000000000 Z
11
+ date: 2017-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-cursor
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.1'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.1'
61
75
  description: A terminal spinner for tasks that have non-deterministic time frame.
62
76
  email:
63
77
  - pmurach@gmail.com