Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make plain-text spinner element avaialble via config #48

Merged
merged 1 commit into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I found this useful, but I'm thinking we should put it in StatusUpdater instead so that it's the default behavior regardless of the UI element in use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed - I think it should go in StatusUpdater if we have it anywhere.

My future vision is that if you passed the equivalent of --force-logger to the CLI, the spinner logic would be disabled completely and you would just get traditional log output on STDOUT. So in that case I think StatusUpdater handling it makes more sense. But we can definitely leave this here for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

@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