Class: Buildr::Console
Overview
A utility class that helps with colorizing output for interactive shells where appropriate
Class Method Summary collapse
- .agree?(message) ⇒ Boolean
- .ask_password(prompt) ⇒ Object
-
.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.
-
.console_dimensions ⇒ Object
Return the [rows, columns] of a console or nil if unknown.
-
.output_cols ⇒ Object
Return the number of columns in console or nil if unknown.
- .present_menu(header, options) ⇒ Object
- .use_color ⇒ Object
- .use_color=(use_color) ⇒ Object
Class Method Details
.agree?(message) ⇒ Boolean
75 76 77 78 |
# File 'lib/buildr/core/console.rb', line 75 def agree?() puts "#{} (Y or N)" :agree == ask('Y' => :agree, 'N' => :disagree) end |
.ask_password(prompt) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# 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) ⇒ Object
Emit message with color at the start of the message and the clear color command at the end of the sequence.
31 32 33 34 35 36 37 |
# File 'lib/buildr/core/console.rb', line 31 def color(, color) raise "Unknown color #{color.inspect}" unless [:green, :red, :blue].include?(color) return unless use_color constants = {:green => "\e[32m", :red => "\e[31m", :blue => "\e[34m"} @java_console.putString("#{constants[color]}#{}\e[0m") if @java_console "#{constants[color]}#{}\e[0m" end |
.console_dimensions ⇒ Object
Return the [rows, columns] of a console or nil if unknown
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# 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 `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_cols ⇒ Object
Return the number of columns in console or nil if unknown
70 71 72 73 |
# File 'lib/buildr/core/console.rb', line 70 def output_cols d = console_dimensions d ? d[0] : nil end |
.present_menu(header, options) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/buildr/core/console.rb', line 91 def (header, ) puts header = {} count = 1 .each_pair do |, result| puts "#{count}. #{}" [count.to_s] = result count += 1 end ask() end |
.use_color ⇒ Object
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 28 |
# File 'lib/buildr/core/console.rb', line 25 def use_color=(use_color) return if use_color && !setup_support @use_color = use_color end |