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
62 63 64 65 |
# File 'lib/buildr/core/console.rb', line 62 def agree?() puts "#{} (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(, 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
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_cols ⇒ Object
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 (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 |
# File 'lib/buildr/core/console.rb', line 25 def use_color=(use_color) @use_color = use_color end |