# File lib/core/util/console.rb, line 306 def self.black(string) colorize(string, :black) end
# File lib/core/util/console.rb, line 330 def self.blue(string) colorize(string, :blue) end
# File lib/core/util/console.rb, line 284 def self.colorize(string, color) return '' unless string return string.to_s unless use_colors color = color.to_sym string = string.to_s color_string = string if @@colors[color] color = @@colors[color] clear_color = @@colors[:clear] escaped_clear = Regexp.escape(clear_color) color_string = "#{color}" color_string << string.gsub(/#{escaped_clear}(?!\e\[)/, "#{clear_color}#{color}") color_string << "#{clear_color}" end color_string end
# File lib/core/util/console.rb, line 342 def self.cyan(string) colorize(string, :cyan) end
# File lib/core/util/console.rb, line 318 def self.green(string) colorize(string, :green) end
# File lib/core/util/console.rb, line 348 def self.grey(string) colorize(string, :grey) end
# File lib/core/util/console.rb, line 39 def initialize(options = {}) if options.is_a?(String) options = { :resource => options } end config = Config.ensure(options) @resource = config.get(:resource, '') @color = config.get(:color, true) @printer = config.get(:printer, :puts) @input = config.get(:input, $stdin) @output = config.get(:output, $stdout) @error = config.get(:error, $stderr) @delegate = config.get(:console_delegate, nil) end
# File lib/core/util/console.rb, line 336 def self.purple(string) colorize(string, :purple) end
# File lib/core/util/console.rb, line 70 def self.quiet @@quiet end
# File lib/core/util/console.rb, line 74 def self.quiet=quiet @@quiet = quiet end
# File lib/core/util/console.rb, line 312 def self.red(string) colorize(string, :red) end
# File lib/core/util/console.rb, line 80 def self.use_colors @@use_colors && ! ENV['NUCLEON_NO_COLOR'] end
# File lib/core/util/console.rb, line 84 def self.use_colors=use_colors @@use_colors = use_colors end
# File lib/core/util/console.rb, line 324 def self.yellow(string) colorize(string, :yellow) end
# File lib/core/util/console.rb, line 118 def ask(message, options = {}) return @delegate.ask(message, options) if check_delegate('ask') options[:new_line] = false if ! options.has_key?(:new_line) options[:prefix] = false if ! options.has_key?(:prefix) options[:echo] = true if ! options.has_key?(:echo) options[:sync] = true unless options.has_key?(:sync) user_input = nil collect = lambda do say(:info, message, Config.ensure(options).import({ :sync => false, :quiet_override => true }).export) if options[:echo] user_input = @input.gets.chomp else require 'io/console' user_input = @input.noecho(&:gets).chomp end safe_puts("\n") user_input end if options[:sync] #@@console_lock.synchronize do return collect.call #end else return collect.call end end
# File lib/core/util/console.rb, line 277 def check_delegate(method) return ( @delegate && @delegate.respond_to?(method.to_s) ) end
# File lib/core/util/console.rb, line 218 def format_message(type, message, options = {}) return @delegate.format_message(type, message, options) if check_delegate('format_message') return '' if message.to_s.strip.empty? if @resource && ! @resource.empty? && options[:prefix] prefix = "[#{@resource}]" end lines = [] prev_color = nil escaped_clear = Regexp.escape(@@colors[:clear]) message.split("\n").each do |line| line = prev_color + line if prev_color lines << "#{prefix} #{line}".sub(/^ +/, '') # Set next previous color if line =~ /#{escaped_clear}$/ prev_color = nil else line_section = line.split(/#{escaped_clear}/).pop prev_colors = line_section.scan(/\e\[[0-9][0-9]?m/) prev_color = prev_colors.pop unless prev_colors.empty? end end message = lines.join("\n") if @@use_colors && @color if options.has_key?(:color) message = self.class.colorize(message, options[:color]) else message = self.class.colorize(message, @@color_map[type]) if @@color_map[type] end end return message end
# File lib/core/util/console.rb, line 189 def info(message, *args) return @delegate.info(message, *args) if check_delegate('info') say(:info, message, *args) end
# File lib/core/util/console.rb, line 59 def inspect "#<#{self.class}: #{@resource}>" end
# File lib/core/util/console.rb, line 153 def password(type, options = {}) return @delegate.password(type, options) if check_delegate('password') options[:sync] = true unless options.has_key?(:sync) collect = lambda do try_again = true password = nil while try_again # Get and check a password from the keyboard password = ask("Enter #{type} password: ", { :echo => false, :sync => false }) confirmation_password = ask("Confirm #{type} password: ", { :echo => false, :sync => false }) if password != confirmation_password choice = ask('Passwords do not match! Try again? (Y|N): ', { :sync => false }) try_again = choice.upcase == "Y" password = nil unless try_again else try_again = false end end password.strip end if options[:sync] #@@console_lock.synchronize do return collect.call #end else return collect.call end end
# File lib/core/util/console.rb, line 259 def safe_puts(message = nil, options = {}) return @delegate.safe_puts(message, options) if check_delegate('safe_puts') message ||= "" options = { :channel => @output, :printer => @printer, }.merge(options) begin options[:channel].send(options[:printer], message) rescue Errno::EPIPE return end end
# File lib/core/util/console.rb, line 91 def say(type, message, options = {}) return if @@quiet && ! options[:quiet_override] return @delegate.say(type, message, options) if check_delegate('say') defaults = { :new_line => true, :prefix => true } options = defaults.merge(options) printer = options[:new_line] ? :puts : :print channel = type == :error || options[:channel] == :error ? @error : @output options[:sync] = true unless options.has_key?(:sync) render = lambda do safe_puts(format_message(type, message, options), :channel => channel, :printer => printer) end if options[:sync] #@@console_lock.synchronize do render.call #end else render.call end end
# File lib/core/util/console.rb, line 210 def success(message, *args) return @delegate.success(message, *args) if check_delegate('success') say(:success, message, *args) end
# File lib/core/util/console.rb, line 196 def warn(message, *args) return @delegate.warn(message, *args) if check_delegate('warn') say(:warn, message, *args) end