class Buildr::Console

A utility class that helps with colorizing output for interactive shells where appropriate

Public Class Methods

agree?(message) click to toggle source
# File lib/buildr/core/console.rb, line 75
def agree?(message)
  puts "#{message} (Y or N)"
  :agree == ask('Y' => :agree, 'N' => :disagree)
end
ask_password(prompt) click to toggle source
# File lib/buildr/core/console.rb, line 80
def ask_password(prompt)
  puts prompt
  begin
    set_no_echo_mode
    password = $stdin.readline
    return password.chomp
  ensure
    reset_mode
  end
end
color(message, color) click to toggle source

Emit message with color at the start of the message and the clear color command at the end of the sequence.

# File lib/buildr/core/console.rb, line 31
def color(message, color)
  raise "Unknown color #{color.inspect}" unless [:green, :red, :blue].include?(color)
  return message unless use_color
  constants = {:green => "\e[32m", :red => "\e[31m", :blue => "\e[34m"}
  @java_console.putString("#{constants[color]}#{message}\e[0m") if @java_console
  "#{constants[color]}#{message}\e[0m"
end
console_dimensions() click to toggle source

Return the [rows, columns] of a console or nil if unknown

# File lib/buildr/core/console.rb, line 40
def console_dimensions
  return nil unless setup_support

  begin
    if Buildr::Util.win_os?
      if Buildr::Util.java_platform?
        if JRUBY_VERSION =~ /^1.7/
          [@java_terminal.get_width, @java_terminal.get_height]
        else
          [@java_terminal.getTerminalWidth, @java_terminal.getTerminalHeight]
        end
      else
        Win32::Console.new(Win32::Console::STD_OUTPUT_HANDLE).MaxWindow
      end
    elsif $stdout.isatty
      if /solaris/ =~ RUBY_PLATFORM and
        %x`stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
        [$2, $1].map { |c| x.to_i }
      else
        %x`stty size 2> /dev/null`.split.map { |x| x.to_i }.reverse
      end
    else
      nil
    end
  rescue => e
    nil
  end
end
output_cols() click to toggle source

Return the number of columns in console or nil if unknown

# File lib/buildr/core/console.rb, line 70
def output_cols
  d = console_dimensions
  d ? d[0] : nil
end
present_menu(header, options) click to toggle source
# File lib/buildr/core/console.rb, line 91
def present_menu(header, options)
  puts header
  question_options = {}
  count = 1
  options.each_pair do |message, result|
    puts "#{count}. #{message}"
    question_options[count.to_s] = result
    count += 1
  end
  ask(question_options)
end
use_color() click to toggle source
# File lib/buildr/core/console.rb, line 21
def use_color
  @use_color.nil? ? false : @use_color
end
use_color=(use_color) click to toggle source
# File lib/buildr/core/console.rb, line 25
def use_color=(use_color)
  return if use_color && !setup_support
  @use_color = use_color
end