summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-05-20 20:28:31 -0600
committermo khan <mo.khan@gmail.com>2020-05-20 20:28:31 -0600
commit77489f0239fca9bcabd0cd556ab1619bbb723521 (patch)
treee8a9233419ac9b308dbab2dfbe67d11793e045bd /lib
parent5ce1a4bb47592d33f725b7586f8e767bedd54129 (diff)
switch to a nanospinner with less dependencies
Diffstat (limited to 'lib')
-rw-r--r--lib/spandx/cli.rb3
-rw-r--r--lib/spandx/cli/commands/scan.rb32
-rw-r--r--lib/spandx/core/spinner.rb51
3 files changed, 63 insertions, 23 deletions
diff --git a/lib/spandx/cli.rb b/lib/spandx/cli.rb
index 9e3ad4d..817ff89 100644
--- a/lib/spandx/cli.rb
+++ b/lib/spandx/cli.rb
@@ -1,7 +1,8 @@
# frozen_string_literal: true
+require 'nanospinner'
require 'thor'
-require 'tty-progressbar'
+require 'tty-screen'
module Spandx
module Cli
diff --git a/lib/spandx/cli/commands/scan.rb b/lib/spandx/cli/commands/scan.rb
index aede23b..329063e 100644
--- a/lib/spandx/cli/commands/scan.rb
+++ b/lib/spandx/cli/commands/scan.rb
@@ -4,25 +4,25 @@ module Spandx
module Cli
module Commands
class Scan
- NULL_BAR = Class.new do
- def advance(*args); end
- end.new
-
- attr_reader :scan_path
+ attr_reader :scan_path, :spinner
def initialize(scan_path, options)
@scan_path = ::Pathname.new(scan_path)
@options = options
+ @spinner = options[:show_progress] ? ::Spandx::Core::Spinner.new : ::Spandx::Core::Spinner::NULL
require(options[:require]) if options[:require]
end
def execute(output: $stdout)
report = ::Spandx::Core::Report.new
each_file do |file|
+ spinner.spin(file)
each_dependency_from(file) do |dependency|
+ spinner.spin(file)
report.add(dependency)
end
end
+ spinner.stop
output.puts(format(report.to(@options[:format])))
end
@@ -35,15 +35,11 @@ module Spandx
end
def each_dependency_from(file)
- dependencies = ::Spandx::Core::Parser.for(file).parse(file)
- with_progress(title_for(file), dependencies.size) do |bar|
- dependencies
- .map { |x| enhance(x) }
- .each do |dependency|
- bar.advance(1)
- yield dependency
- end
- end
+ ::Spandx::Core::Parser
+ .for(file)
+ .parse(file)
+ .map { |x| enhance(x) }
+ .each { |dependency| yield dependency }
end
def format(output)
@@ -55,14 +51,6 @@ module Spandx
.all
.inject(dependency) { |memo, plugin| plugin.enhance(memo) }
end
-
- def title_for(file)
- "#{file} [:bar, :elapsed] :percent"
- end
-
- def with_progress(title, total)
- yield @options[:show_progress] ? TTY::ProgressBar.new(title, total: total) : NULL_BAR
- end
end
end
end
diff --git a/lib/spandx/core/spinner.rb b/lib/spandx/core/spinner.rb
new file mode 100644
index 0000000..af3c7a7
--- /dev/null
+++ b/lib/spandx/core/spinner.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: true
+
+module Spandx
+ module Core
+ class Spinner
+ NULL = Class.new do
+ def self.spin(*args); end
+
+ def self.stop(*args); end
+ end
+
+ attr_reader :columns, :spinner
+
+ def initialize(columns: TTY::Screen.columns, output: $stderr)
+ @columns = columns
+ @spinner = Nanospinner.new(output)
+ @queue = Queue.new
+ @thread = Thread.new { work }
+ end
+
+ def spin(message)
+ @queue.enq(justify(message))
+ yield if block_given?
+ end
+
+ def stop
+ @queue.clear
+ @queue.enq(:stop)
+ @thread.join
+ end
+
+ private
+
+ def justify(message)
+ message.to_s.ljust(columns - 3)
+ end
+
+ def work
+ last_message = justify('')
+ loop do
+ message = @queue.empty? ? last_message : @queue.deq
+ break if message == :stop
+
+ spinner.spin(message)
+ last_message = message
+ sleep 0.1
+ end
+ end
+ end
+ end
+end