Class: Rpatch::Tty
- Inherits:
-
Object
- Object
- Rpatch::Tty
- Defined in:
- lib/rpatch/utils.rb
Class Method Summary collapse
- .blue ⇒ Object
- .debug(*messages) ⇒ Object
- .die(*messages) ⇒ Object
- .em ⇒ Object
- .error(*messages) ⇒ Object
- .gray ⇒ Object
- .green ⇒ Object
- .info(*messages) ⇒ Object
- .notice(*messages) ⇒ Object
- .options ⇒ Object
- .red ⇒ Object
- .reset ⇒ Object
- .tty_puts(*args) ⇒ Object
- .warning(*messages) ⇒ Object
- .white ⇒ Object
- .width ⇒ Object
- .yellow ⇒ Object
Class Method Details
.blue ⇒ Object
10 |
# File 'lib/rpatch/utils.rb', line 10 def blue; bold 34; end |
.debug(*messages) ⇒ Object
89 90 91 |
# File 'lib/rpatch/utils.rb', line 89 def debug(*) tty_puts(* << {:type => :debug, :color => :blue}) end |
.die(*messages) ⇒ Object
27 28 29 30 |
# File 'lib/rpatch/utils.rb', line 27 def die(*) error exit 1 end |
.em ⇒ Object
15 |
# File 'lib/rpatch/utils.rb', line 15 def em; underline 39; end |
.error(*messages) ⇒ Object
73 74 75 |
# File 'lib/rpatch/utils.rb', line 73 def error(*) tty_puts(* << {:type => :error, :color => :red}) end |
.gray ⇒ Object
17 |
# File 'lib/rpatch/utils.rb', line 17 def gray; bold 30 end |
.green ⇒ Object
16 |
# File 'lib/rpatch/utils.rb', line 16 def green; color 92 end |
.info(*messages) ⇒ Object
85 86 87 |
# File 'lib/rpatch/utils.rb', line 85 def info(*) tty_puts(* << {:type => :info, :color => :green}) end |
.notice(*messages) ⇒ Object
81 82 83 |
# File 'lib/rpatch/utils.rb', line 81 def notice(*) tty_puts(* << {:type => nil}) end |
.options ⇒ Object
7 8 9 |
# File 'lib/rpatch/utils.rb', line 7 def ||= {:verbose => 0} end |
.red ⇒ Object
12 |
# File 'lib/rpatch/utils.rb', line 12 def red; underline 31; end |
.reset ⇒ Object
14 |
# File 'lib/rpatch/utils.rb', line 14 def reset; escape 0; end |
.tty_puts(*args) ⇒ Object
32 33 34 35 36 37 38 39 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 68 69 70 71 |
# File 'lib/rpatch/utils.rb', line 32 def tty_puts(*args) opts = args.last.is_a?(Hash) ? args.pop : {} case (opts[:type] || :notice).to_s.downcase.to_sym when :error # always show errors when :warning return if verbose < -1 when :notice return if verbose < 0 when :info return if verbose < 1 when :debug return if verbose < 2 end lines = args.map{|m| m.to_s.split($/)}.flatten prompt = opts[:type] ? "#{opts[:type].to_s.upcase}: " : "" if opts[:type] if STDERR.tty? STDERR.write "#{Tty.red}#{prompt}#{Tty.reset}" else STDERR.write "#{prompt}" end end if STDERR.tty? and opts[:color] and Tty.respond_to? opts[:color] STDERR.puts "#{Tty.send(opts[:color])}#{lines.shift}#{Tty.reset}" else STDERR.puts lines.shift end spaces = " " * prompt.size lines.each do |line| STDERR.write spaces if STDERR.tty? and opts[:color] and Tty.respond_to? opts[:color] STDERR.puts "#{Tty.send(opts[:color])}#{line}#{Tty.reset}" else STDERR.puts line end end end |
.warning(*messages) ⇒ Object
77 78 79 |
# File 'lib/rpatch/utils.rb', line 77 def warning(*) tty_puts(* << {:type => :warning, :color => :yellow}) end |
.white ⇒ Object
11 |
# File 'lib/rpatch/utils.rb', line 11 def white; bold 39; end |
.width ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/rpatch/utils.rb', line 19 def width @width = begin w = %x{stty size 2>/dev/null}.chomp.split.last.to_i.nonzero? w ||= %x{tput cols 2>/dev/null}.to_i w < 1 ? 80 : w end end |
.yellow ⇒ Object
13 |
# File 'lib/rpatch/utils.rb', line 13 def yellow; underline 33 ; end |