module DTK::Client::OsUtil::PrintMixin
Public Instance Methods
colorize(message, color)
click to toggle source
Method will convert given string, to string with colorize output
message - String
to be colorized color - Symbol describing color to be used
Returns String
with colorize output
# File lib/client/util/os_util/print.rb, line 61 def colorize(message, color) # at the moment we do not support colors in windows ((is_windows? || message.nil?) ? message : message.colorize(color)) end
print(message, color = :white)
click to toggle source
Method will print to STDOUT with given color
message - String
to be colorize and printed color - Symbol describing the color to be used on STDOUT
# File lib/client/util/os_util/print.rb, line 29 def print(message, color = :white) puts colorize(message, color) end
print_error(message)
click to toggle source
# File lib/client/util/os_util/print.rb, line 41 def print_error(message) print_with_prefix(:error, message, :red) end
print_info(message)
click to toggle source
# File lib/client/util/os_util/print.rb, line 33 def print_info(message) print_with_prefix(:info, message, :yellow) end
print_warning(message)
click to toggle source
# File lib/client/util/os_util/print.rb, line 37 def print_warning(message) print_with_prefix(:warning, message, :yellow) end
put_warning(prefix, text, color)
click to toggle source
# File lib/client/util/os_util/print.rb, line 45 def put_warning(prefix, text, color) width = HighLine::SystemExtensions.terminal_size[0] - (prefix.length + 1) text_split = wrap(text, width) Kernel.print colorize(prefix, color), " " text_split.lines.each_with_index do |line, index| line = " "*(prefix.length + 1) + line unless index == 0 puts line end end
Private Instance Methods
actual_length(string_with_escapes)
click to toggle source
# File lib/client/util/os_util/print.rb, line 89 def actual_length(string_with_escapes) string_with_escapes.to_s.gsub(/\e\[\d{1,2}m/, "").length end
print_with_prefix(prefix, message, color)
click to toggle source
# File lib/client/util/os_util/print.rb, line 93 def print_with_prefix(prefix, message, color) PrintWithPrefix.print(prefix, message, color) end
wrap(text, wrap_at)
click to toggle source
# File lib/client/util/os_util/print.rb, line 68 def wrap(text, wrap_at) wrapped = [ ] text.each_line do |line| # take into account color escape sequences when wrapping wrap_at = wrap_at + (line.length - actual_length(line)) while line =~ /([^\n]{#{wrap_at + 1},})/ search = $1.dup replace = $1.dup if index = replace.rindex(" ", wrap_at) replace[index, 1] = "\n" replace.sub!(/\n[ \t]+/, "\n") line.sub!(search, replace) else line[$~.begin(1) + wrap_at, 0] = "\n" end end wrapped << line end wrapped.join end