Skip to content

Instantly share code, notes, and snippets.

@anthonybishopric
Created March 19, 2014 15:57
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 anthonybishopric/9644828 to your computer and use it in GitHub Desktop.
Save anthonybishopric/9644828 to your computer and use it in GitHub Desktop.
gat: execute the given command every time the working dir changes
#! /usr/bin/env ruby
#
# Example usage:
#
# 1> $ cd github.com/anthonybishopric/gotcha
# 1> $ gat go test # begin listening to this directory
#
# 2> $ cd github.com/anthonybishopric/gotcha
# 2> $ touch README.md
# PASS
# ok github.com/anthonybishopric/gotcha 0.016s
require 'rb-fsevent'
require 'optparse'
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Run a command when contents in this directory change.\nUsage: gat [-n|--no-clear] <command>"
opts.on("-n", "--[no-]clear", "Don't clear the screen before execution") do |n|
options[:no_clear] = n
end
opts.on_tail("-h", "--help", "Show help") do
puts opts
exit
end
end
opts.parse!
command = ARGV.join(" ")
if command == ""
puts opts
exit 1
end
unless options[:no_clear]
command = "clear && #{command}"
end
fsevent = FSEvent.new
fsevent.watch Dir.pwd do |directories|
system command
end
fsevent.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment