Class: Rpatch::Tty

Inherits:
Object
  • Object
show all
Defined in:
lib/rpatch/utils.rb

Class Method Summary collapse

Class Method Details

.blueObject



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(*messages)
  tty_puts(*messages << {:type => :debug, :color => :blue})
end

.die(*messages) ⇒ Object



27
28
29
30
# File 'lib/rpatch/utils.rb', line 27

def die(*messages)
  error messages
  exit 1
end

.emObject



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(*messages)
  tty_puts(*messages << {:type => :error, :color => :red})
end

.grayObject



17
# File 'lib/rpatch/utils.rb', line 17

def gray; bold 30 end

.greenObject



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(*messages)
  tty_puts(*messages << {:type => :info, :color => :green})
end

.notice(*messages) ⇒ Object



81
82
83
# File 'lib/rpatch/utils.rb', line 81

def notice(*messages)
  tty_puts(*messages << {:type => nil})
end

.optionsObject



7
8
9
# File 'lib/rpatch/utils.rb', line 7

def options
  @options ||= {:verbose => 0}
end

.redObject



12
# File 'lib/rpatch/utils.rb', line 12

def red; underline 31; end

.resetObject



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(*messages)
  tty_puts(*messages << {:type => :warning, :color => :yellow})
end

.whiteObject



11
# File 'lib/rpatch/utils.rb', line 11

def white; bold 39; end

.widthObject



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

.yellowObject



13
# File 'lib/rpatch/utils.rb', line 13

def yellow; underline 33 ; end