summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-06-12 15:38:56 -0600
committermo khan <mo.khan@gmail.com>2020-06-12 15:38:56 -0600
commit077998970180a9f21d287d2716ef9d2eaceb4cfa (patch)
tree5ab7bd250de38d1290cc112753e50adb173b4f8a /lib
parent50437b8b4bf9f4e17a892c5a02f086ec3ac9fd3e (diff)
Move spinner into Table printer
Diffstat (limited to 'lib')
-rw-r--r--lib/spandx/cli.rb3
-rw-r--r--lib/spandx/cli/commands/scan.rb3
-rw-r--r--lib/spandx/cli/printers/table.rb6
-rw-r--r--lib/spandx/core/spinner.rb51
4 files changed, 7 insertions, 56 deletions
diff --git a/lib/spandx/cli.rb b/lib/spandx/cli.rb
index eaab0d6..e5a8d76 100644
--- a/lib/spandx/cli.rb
+++ b/lib/spandx/cli.rb
@@ -1,8 +1,7 @@
# frozen_string_literal: true
-require 'nanospinner'
require 'thor'
-require 'tty-screen'
+require 'tty-spinner'
require 'terminal-table'
module Spandx
diff --git a/lib/spandx/cli/commands/scan.rb b/lib/spandx/cli/commands/scan.rb
index 72bf670..10cc41d 100644
--- a/lib/spandx/cli/commands/scan.rb
+++ b/lib/spandx/cli/commands/scan.rb
@@ -35,16 +35,13 @@ module Spandx
end
def each_dependency
- spinner = @options[:show_progress] ? Spinner.new : Spinner::NULL
with_thread_pool(size: thread_count) do |thread|
each_file do |file|
- spinner.spin(file.to_s)
Parser.parse(file).each do |dependency|
thread.run { yield dependency }
end
end
end
- spinner.stop
end
def format(output)
diff --git a/lib/spandx/cli/printers/table.rb b/lib/spandx/cli/printers/table.rb
index 0ae98c5..931b739 100644
--- a/lib/spandx/cli/printers/table.rb
+++ b/lib/spandx/cli/printers/table.rb
@@ -6,12 +6,17 @@ module Spandx
class Table < Printer
HEADINGS = ['Name', 'Version', 'Licenses', 'Location'].freeze
+ def initialize
+ @spinner = TTY::Spinner.new(output: $stderr)
+ end
+
def match?(format)
format.to_sym == :table
end
def print_header(_io)
@dependencies = SortedSet.new
+ @spinner.auto_spin
end
def print_line(dependency, _io)
@@ -19,6 +24,7 @@ module Spandx
end
def print_footer(io)
+ @spinner.stop
io.puts(to_table(@dependencies.map(&:to_a)))
end
diff --git a/lib/spandx/core/spinner.rb b/lib/spandx/core/spinner.rb
deleted file mode 100644
index af3c7a7..0000000
--- a/lib/spandx/core/spinner.rb
+++ /dev/null
@@ -1,51 +0,0 @@
-# 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