Skip to content

Instantly share code, notes, and snippets.

@holysugar
Created February 6, 2013 10:27
Show Gist options
  • Select an option

  • Save holysugar/4721717 to your computer and use it in GitHub Desktop.

Select an option

Save holysugar/4721717 to your computer and use it in GitHub Desktop.
Ruby のライブラリ汎用 watchr 設定ファイル screen 通知用
# vim: ft=ruby fileencoding=utf-8
# --------------------------------------------------
# Notifier to screen
# --------------------------------------------------
def hardstatus
status = open("#{ENV['HOME']}/.screenrc"){|f| f.grep(/^hardstatus/)[-1] } || ""
status.chomp.rstrip
end
def screencolor(color)
c = {
:black => 'k',
:red => 'r',
:green => 'g',
:yellow => 'y',
:blue => 'b',
:cyan => 'c',
:magenta => 'm',
:white => 'w',
:none => '.',
}
c.default = 'd'
c[color]
end
def notify_screen(message, foreground = :none, background = :none)
str = "%{=b #{screencolor background}#{screencolor foreground}} #{message} %{-}"
command = %|hardstatus alwayslastline "#{str}"|
system %|screen -X eval '#{command}'|
end
def clear_screen
system %|screen -X eval '#{hardstatus}'|
end
def notify_screen_result(result, message = nil)
case result
when nil
notify_screen(message || "???".center(20), :default, :default)
when 0, true
notify_screen(message || "SUCCESSFUL".center(20), :white, :green)
else
notify_screen(message || "SOMETHING WRONG".center(20), :white, :red)
end
end
def run_test
output = `bundle exec rake test`
result = $?
puts output
line = output.each_line.grep(/^(Finished tests|\d+ tests, \d+ assertions)/).map(&:chomp).join(" ")
no_int_for_you
notify_screen_result(result, line)
end
def no_int_for_you
@sent_an_int = nil
end
Signal.trap 'INT' do
if @sent_an_int
puts " A second INT? Ok, I get the message. Shutting down now."
clear_screen
exit
else
puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again."
@sent_an_int = true
Kernel.sleep 1.5
run_test
end
end
watch('^test/(.*)_test\.rb') {|m| run_test }
watch('^lib/(.*)\.rb') {|m| run_test }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment