paolodona / scripts

a collection of useful ruby scripts

This URL has Read+Write access

scripts / colorize
100755 47 lines (42 sloc) 1.687 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env ruby
# colorize the output of rspec stories
# eg: stakeout "stories/all.rb | ./colorize.rb" stories/* stories/**/*
 
module Color
  COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 }
  def self.method_missing(color_name, *args)
    color(color_name) + args.first + color(:clear)
  end
  def self.color(color)
    "\e[#{COLORS[color.to_sym]}m"
  end
end
 
# Growl stuff ---------
AUTOTEST_IMAGE_ROOT = "~/.autotest_images"
USE_GROWL = system 'which growlnotify'
def self.growl(title, msg, img, pri=0, sticky="")
  system "growlnotify -n autotest --image #{img} -p #{pri} -m '#{title}: #{msg.strip}' #{sticky}" if USE_GROWL
  true
end
def growl_success(msg); growl("PASS", msg, "#{AUTOTEST_IMAGE_ROOT}/pass.png", 2); end
def growl_fail(msg); growl("FAIL", msg, "#{AUTOTEST_IMAGE_ROOT}/fail.png", 2); end
def growl_pending(msg); growl("PENDING", msg, "#{AUTOTEST_IMAGE_ROOT}/pending.png", 2); end
 
# Summary -------------
# 14 scenarios: 13 succeeded, 0 failed, 1 pending
RESULTS_LINE = /(\d+)\s+scenarios:\s*(\d+)\s+succeeded?,\s*(\d+)\s+failed?(,\s*(\d+)\s+pending)?.*/
# colorize the summary
def colorize_summary(summary)
  output = summary.slice(RESULTS_LINE)
  succeeded, failed, pending = $~[2].to_i, $~[3].to_i, $~[5].to_i
  growl_fail(summary) and return Color.red(summary) if failed > 0
  growl_pending(summary) and return Color.yellow(summary) if pending > 0
  growl_success(summary) and return Color.green(summary)
end
 
STDIN.each_line do |line|
  colored_line = case line
    when /FAILED/ then Color.red(line)
    when /PENDING/ then Color.yellow(line)
    when RESULTS_LINE then colorize_summary(line)
    else line
  end
  puts colored_line
end