Skip to content

Commit

Permalink
Merge pull request #48 from chef/mp/add-configurable-spinner-element
Browse files Browse the repository at this point in the history
Make plain-text spinner element avaialble via config

Merging this, but will add a card to move the logging up the next time we want to pick up minor infra improvement .
  • Loading branch information
marcparadise authored Apr 3, 2018
2 parents 1083eb5 + f7adc47 commit 07d2669
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test_results/
tags
3 changes: 3 additions & 0 deletions components/chef-workstation/lib/chef-workstation/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Config
config_context :cache do
default(:path, File.join(WS_BASE_PATH, "cache"))
end
config_context :dev do
default(:spinner, "TTY::Spinner")
end

class << self
@custom_location = nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module ChefWorkstation
module UI
class PlainTextElement
def initialize(format, opts)
@orig_format = format
@format = format
@output = opts[:output]
end

def run(&block)
yield
end

def update(params)
# SOme of this is particular to our usage -
# prefix does not cause a text update, but does
# change the prefix for future messages.
if params.has_key?(:prefix)
@format = @orig_format.gsub(":prefix", params[:prefix])
return
end

if @succ
ind = "OK"
@succ = false
log_method = :info
elsif @err
ind = "ERR"
@err = false
log_method = :error
else
log_method = :debug
ind = " - "
end

# Since this is a generic type, we can replace any component
# name in this regex - but for now :spinner is the only component
# we're standing in for.
msg = @format.gsub(/:spinner/, ind)
params.each_pair do |k, v|
msg.gsub!(/:#{k}/, v)
end
ChefWorkstation::Log.send(log_method, msg)
@output.puts(msg)
end

def error
@err = true
@succ = false
end

def success
@succ = true
@err = false
end
end
end
end
13 changes: 10 additions & 3 deletions components/chef-workstation/lib/chef-workstation/ui/terminal.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require "chef-workstation/status_reporter"
require "tty-spinner"
require "chef-workstation/status_reporter"
require "chef-workstation/config"
require "chef-workstation/log"
require "chef-workstation/ui/plain_text_element"

module ChefWorkstation
module UI
Expand All @@ -12,17 +15,21 @@ def init(location = STDOUT)
@location = location
end

def write(msg)
@location.write(msg)
end

def output(msg)
@location.puts msg
end

def spinner(msg, prefix: "", &block)
spinner = TTY::Spinner.new("[:spinner] :prefix :status", output: @location)
klass = Object.const_get("ChefWorkstation::UI::#{ChefWorkstation::Config.dev.spinner}")
spinner = klass.new("[:spinner] :prefix :status", output: @location)
reporter = StatusReporter.new(spinner, prefix: prefix, key: :status)
reporter.update(msg)
spinner.run { yield(reporter) }
end

end
end
end
Expand Down

0 comments on commit 07d2669

Please sign in to comment.