Skip to content

Instantly share code, notes, and snippets.

@evoL
Created February 9, 2015 19:35
Show Gist options
  • Select an option

  • Save evoL/aab2c8fc2e954123d556 to your computer and use it in GitHub Desktop.

Select an option

Save evoL/aab2c8fc2e954123d556 to your computer and use it in GitHub Desktop.
scrap — a simple manager of code meant to be used once
#!/usr/bin/env ruby
require 'pathname'
### Configuration begins here
SCRAP_DIR = '/Users/evol/.scraps'
EXTENSIONS = {
'rb' => 'irb -r $FILE',
'cpp' => 'g++ -std=c++11 $FILE; ./a.out',
'hs' => 'ghci $FILE'
}
EDITOR = 'subl'
### Configuration ends here
def usage
puts "Usage: scrap [--edit|-e] LANGUAGE"
puts
puts "Supported languages: " << EXTENSIONS.keys.join(', ')
exit
end
def file(language)
path = Pathname.new(SCRAP_DIR).join("scrap.#{language}")
path.to_s
end
def command(language)
EXTENSIONS.fetch(language).gsub('$FILE', file(language))
end
language = ARGV[0]
if ARGV.empty?
usage
elsif ARGV[0].start_with? '-'
usage if ARGV.length < 2
if ['--edit', '-e'].include? ARGV[0]
exec EDITOR, file(ARGV[1])
exit
else
puts 'scrap: Unrecognized parameter: ' << ARGV[0]
language = ARGV[1]
end
end
unless EXTENSIONS.has_key? language
puts 'scrap: Unknown language: ' << language
exit 1
end
Dir.chdir SCRAP_DIR
exec command(language)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment