Class: Rails::CssUnused::Spinner
- Inherits:
-
Object
- Object
- Rails::CssUnused::Spinner
- Defined in:
- lib/rails/css_unused/spinner.rb
Overview
A lightweight TTY spinner shown while scanning is in progress. Automatically disabled when output is not a TTY (CI, pipes, file output).
Constant Summary collapse
- FRAMES =
%w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏].freeze
- BOLD =
"\e[1m"- CYAN =
"\e[36m"- GREEN =
"\e[32m"- YELLOW =
"\e[33m"- RESET =
"\e[0m"- CLEAR =
move to column 0, erase line
"\r\e[K"- INTERVAL =
seconds between frames
0.08
Class Method Summary collapse
-
.run(label, output: $stderr, &block) ⇒ Object
Run a block with a spinner, returning the block’s return value.
Instance Method Summary collapse
-
#initialize(output: $stderr) ⇒ Spinner
constructor
A new instance of Spinner.
- #run(label, &block) ⇒ Object
Constructor Details
#initialize(output: $stderr) ⇒ Spinner
Returns a new instance of Spinner.
20 21 22 23 24 25 26 |
# File 'lib/rails/css_unused/spinner.rb', line 20 def initialize(output: $stderr) @output = output @tty = output.respond_to?(:isatty) && output.isatty @thread = nil @frame = 0 @label = "" end |
Class Method Details
.run(label, output: $stderr, &block) ⇒ Object
Run a block with a spinner, returning the block’s return value. The spinner is suppressed when not on a TTY.
result = Spinner.run("Scanning stylesheets") { expensive_work }
33 34 35 |
# File 'lib/rails/css_unused/spinner.rb', line 33 def self.run(label, output: $stderr, &block) new(output: output).run(label, &block) end |
Instance Method Details
#run(label, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rails/css_unused/spinner.rb', line 37 def run(label, &block) @label = label start! result = block.call stop!(success: true) result rescue => e stop!(success: false) raise e end |