public
Created

A script to auto-generate scratch files

  • Download Gist
gistfile1.rb
Ruby
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#!/usr/bin/env ruby
# Setup:
# 1) run: $EDITOR /some/bin/dir/scratch
# My path is /Users/nate/bin/scratch
# 2) Paste this into the file
# 3) run: chmod +x /some/bin/dir/scratch
# 4) run: scratch -h
 
class Scratch
 
def self.itch
new.itch
end
 
def itch
if ARGV.last == "-h"
puts
puts "Scratch - scratch paper for ruby code"
puts " scratch -h shows you this information"
puts " scratch -l returns the filename of your latest scratch file"
puts " (useful for: ruby `scratch -l`)"
puts " scratch -n starts a new scratch file"
puts " scratch -a opens entire scratch directory in your editor"
puts
elsif ARGV.last == "-n"
edit(new_filename)
elsif ARGV.last == "-a"
edit_scratch_home
elsif ARGV.last == "-l"
print "#{scratch_home}/#{last_scratch_file}"
elsif last_scratch_file
edit(last_scratch_file)
else
edit(new_filename)
end
end
 
def scratch_home
@scratch_home ||= "#{ENV['HOME']}/.scratch"
end
 
def now
@now ||= Time.now
end
 
def editor
@editor ||= ENV['EDITOR'] || "mate"
end
 
def new_filename
@new_filename ||= "#{now.strftime("%Y.%m.%d-%H:%M:%S.#{now.usec.to_s.rjust(6, "0")}")}.rb"
end
 
def last_scratch_file
if @last_scratch_file
@last_scratch_file
else
create_scratch_home
@last_scratch_file = `ls #{scratch_home}`.split("\n").last
end
end
 
def create_scratch_home
`mkdir -p #{scratch_home}`
end
 
def initialize_file(file)
create_scratch_home
 
if !File.exists?("#{scratch_home}/#{file}")
`touch #{scratch_home}/#{file}`
`echo "#!/usr/bin/env ruby\n# Created: #{now}" > #{scratch_home}/#{file}`
end
end
 
def edit_scratch_home
edit(nil)
end
 
def edit(file)
initialize_file(file)
 
fork { `#{editor} #{scratch_home}/#{file}` }
end
 
end
 
Scratch.itch

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.