diff --git a/CHANGELOG.md b/CHANGELOG.md index 6188f50..b1e68c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ * Change gemspec to include rake and rspec as dev dependencies * Change all built-in formats to use consistent frames formatting +### Fixed +* Fix cursor hiding in threaded run + ## [v0.9.3] - 2020-01-28 ### Changed diff --git a/lib/tty/spinner.rb b/lib/tty/spinner.rb index 55f48c4..3dde7b5 100644 --- a/lib/tty/spinner.rb +++ b/lib/tty/spinner.rb @@ -242,23 +242,25 @@ def auto_spin start sleep_time = 1.0 / @interval - spin @thread = Thread.new do - sleep(sleep_time) - while @started_at - if Thread.current["pause"] - Thread.stop - Thread.current["pause"] = false - end + # rubocop:disable Style/RedundantBegin + begin + # rubocop:enable Style/RedundantBegin spin sleep(sleep_time) + while @started_at + if Thread.current["pause"] + Thread.stop + Thread.current["pause"] = false + end + spin + sleep(sleep_time) + end + ensure + write(TTY::Cursor.show, false) if @hide_cursor end end end - ensure - if @hide_cursor - write(TTY::Cursor.show, false) - end end # Checked if current spinner is paused diff --git a/spec/unit/auto_spin_spec.rb b/spec/unit/auto_spin_spec.rb index b4715f4..9a9ebe0 100644 --- a/spec/unit/auto_spin_spec.rb +++ b/spec/unit/auto_spin_spec.rb @@ -20,6 +20,6 @@ } output.rewind - expect(output.read).to start_with("\e[?25l").and end_with("\e[?25h") + expect(output.read.empty?).to eq(true) end end diff --git a/spec/unit/pause_spec.rb b/spec/unit/pause_spec.rb index 4dba3e1..b8eec44 100644 --- a/spec/unit/pause_spec.rb +++ b/spec/unit/pause_spec.rb @@ -34,10 +34,6 @@ spinner.auto_spin output.rewind - expect(output.read).to eq([ - "\e[1G[|]", - "\e[1G[?]", - "\e[1G[|]" - ].join) + expect(output.read).to eq("\e[1G[?]") end end diff --git a/spec/unit/run_spec.rb b/spec/unit/run_spec.rb index 70b9251..8d18aca 100644 --- a/spec/unit/run_spec.rb +++ b/spec/unit/run_spec.rb @@ -27,4 +27,20 @@ spinner.run("done", &job) }.to yield_with_args(spinner) end + + it "restores cursor when error is raised" do + spinner = TTY::Spinner.new(output: output, hide_cursor: true) + + Thread.report_on_exception = false + begin + spinner.run do + raise "boom" + end + rescue RuntimeError + end + Thread.report_on_exception = true + + output.rewind + expect(output.read).to start_with("\e[?25l").and end_with("\e[?25h") + end end