Skip to content

Instantly share code, notes, and snippets.

@l15n
Forked from kakutani/screen.rb
Created September 3, 2008 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l15n/8524 to your computer and use it in GitHub Desktop.
Save l15n/8524 to your computer and use it in GitHub Desktop.
##
# Autotest::Screen is test result notify GNU Screen's statusline.
#
# === screenshots
# * <img src="http://f.hatena.ne.jp/images/fotolife/s/secondlife/20061109/20061109015543.png" />
# * <img src="http://f.hatena.ne.jp/images/fotolife/s/secondlife/20061109/20061109015522.png" />
#
# == SYNOPSIS
# require 'autotest/screen'
# # Autotest::Screen.statusline = '%H %`%-w%{=b bw}%n %t%{-}%+w (your statusline)'
#
class Autotest::Screen
DEFAULT_STATUSLINE = '%H %`%-w%{=b bw}%n %t%{-}%+w'
DEFAULT_SCREEN_CMD = 'screen'
SCREEN_COLOR = {
:black => 'dd',
:green => 'gw',
:yellow => 'yk',
:red => 'rw',
}
def self.message(msg, color = :black)
col = SCREEN_COLOR[color]
msg = %Q[ %{=b #{col}} #{msg} %{-}]
send_cmd(msg)
end
def self.clear
send_cmd('')
end
def self.run_screen_session?
str = `#{screen_cmd} -ls`
str.match(/(\d+) Socket/) && ($1.to_i > 0)
end
def self.execute?
!($TESTING || !run_screen_session?)
end
@statusline, @screen_cmd = nil
def self.statusline; @statusline || DEFAULT_STATUSLINE.dup; end
def self.statusline=(a); @statusline = a; end
def self.screen_cmd; @screen_cmd || DEFAULT_SCREEN_CMD.dup; end
def self.screen_cmd=(a); @screen_cmd = a; end
def self.send_cmd(msg)
cmd = %(#{screen_cmd} -X eval 'hardstatus alwayslastline "#{(statusline + msg).gsub('"', '\"')}"') #' stupid ruby-mode
system cmd
end
# All blocks return false for each of following blocks defined in user's own ".autotest".
Autotest.add_hook :initialize do |at|
message "Run with #{at.class}" if execute?
next false
end
Autotest.add_hook :quit do |at|
clear if execute?
next false
end
Autotest.add_hook :run_command do |at|
message 'Running' if execute?
next false
end
Autotest.add_hook :ran_command do |at|
next false unless execute?
output = at.results.join
unless defined? Spec::Example
results = output.scan(/(\d+)\s*failures?,\s*(\d+)\s*errors?/).first
num_failures, num_errors = results.map{|r| r.to_i}
if num_failures > 0 || num_errors > 0
message "Red F:#{num_failures} E:#{num_errors}", :red
else
message 'All Green', :green
end
else
results = output.scan(/(\d+)\s*examples?,\s*(\d+)\s*failures?(?:,\s*(\d+)\s*pendings?)?/).first
num_examples, num_failures, num_pendings = results.map{|r| r.to_i}
if num_failures > 0
message "Fail F:#{num_failures} P:#{num_pendings}", :red
elsif num_pendings > 0
message "Pend F:#{num_failures} P:#{num_pendings}", :yellow
else
message 'All Green', :green
end
end
next false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment