Class: Buildr::Console

Inherits:
Object show all
Defined in:
lib/buildr/core/console.rb

Overview

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

Class Method Summary collapse

Class Method Details

.agree?(message) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/buildr/core/console.rb', line 62

def agree?(message)
  puts "#{message} (Y or N)"
  :agree == ask('Y' => :agree, 'N' => :disagree)
end

.ask_password(prompt) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/buildr/core/console.rb', line 67

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) ⇒ Object

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



30
31
32
33
34
35
36
# File 'lib/buildr/core/console.rb', line 30

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_dimensionsObject

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/buildr/core/console.rb', line 39

def console_dimensions
  begin
    if $stdout.isatty
      if /solaris/ =~ RUBY_PLATFORM and
        `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
        [$2, $1].map { |c| x.to_i }
      else
        `stty size 2> /dev/null`.split.map { |x| x.to_i }.reverse
      end
    else
      nil
    end
  rescue => e
    nil
  end
end

.output_colsObject

Return the number of columns in console or nil if unknown



57
58
59
60
# File 'lib/buildr/core/console.rb', line 57

def output_cols
  d = console_dimensions
  d ? d[0] : nil
end

.present_menu(header, options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/buildr/core/console.rb', line 78

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_colorObject



21
22
23
# File 'lib/buildr/core/console.rb', line 21

def use_color
  @use_color.nil? ? false : @use_color
end

.use_color=(use_color) ⇒ Object



25
26
27
# File 'lib/buildr/core/console.rb', line 25

def use_color=(use_color)
  @use_color = use_color
end