diff --git a/.gitignore b/.gitignore index 3752bbf61..f50e56519 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ test_results/ +tags diff --git a/components/chef-workstation/lib/chef-workstation/config.rb b/components/chef-workstation/lib/chef-workstation/config.rb index 78a6e91d5..c81ae49e9 100644 --- a/components/chef-workstation/lib/chef-workstation/config.rb +++ b/components/chef-workstation/lib/chef-workstation/config.rb @@ -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 diff --git a/components/chef-workstation/lib/chef-workstation/ui/plain_text_element.rb b/components/chef-workstation/lib/chef-workstation/ui/plain_text_element.rb new file mode 100644 index 000000000..b174c1ae3 --- /dev/null +++ b/components/chef-workstation/lib/chef-workstation/ui/plain_text_element.rb @@ -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 diff --git a/components/chef-workstation/lib/chef-workstation/ui/terminal.rb b/components/chef-workstation/lib/chef-workstation/ui/terminal.rb index bd2ae89ed..58dbbca83 100644 --- a/components/chef-workstation/lib/chef-workstation/ui/terminal.rb +++ b/components/chef-workstation/lib/chef-workstation/ui/terminal.rb @@ -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 @@ -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