paolodona / scripts

a collection of useful ruby scripts

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