Created
January 24, 2013 14:58
-
-
Save ttscoff/4622635 to your computer and use it in GitHub Desktop.
Rake task to locate and display a download ID from downloads.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "Find a download ID" | |
task :find_download, :term do |t, args| | |
raise "### You haven't created a download csv yet." unless File.exists?('downloads.csv') | |
results = CSV.read("downloads.csv").delete_if {|row| | |
row[0].strip =~ /^\d+$/ && row[1] + " " + row[4] =~ /.*#{args.term}.*/i ? false : true | |
} | |
results.sort! { |a,b| | |
a[0].to_i <=> b[0].to_i | |
}.map! { |res| | |
res[0] + ": " + res[1] + " v" + res[3] | |
} | |
results_menu(results,"download") | |
print ("Select download") | |
while line = Readline.readline(": ", true) | |
if !line || line =~ /^[a-z]/i | |
puts "## Canceled" | |
Process.exit 0 | |
end | |
line = line.to_i | |
if (line > 0 && line <= results.length) | |
id = results[line.to_i - 1].match(/^\d+/)[0] | |
download_tag = "{% download #{id} %}" | |
%x{echo "#{download_tag}\\c"| pbcopy} | |
puts %Q{Download tag in clipboard: "#{download_tag}"} | |
Process.exit 0 | |
else | |
puts "## Selection out of range" | |
Process.exit 0 | |
end | |
end | |
end | |
# creates a user menu from a hash or array | |
def results_menu(res, type = "file") | |
counter = 1 | |
puts | |
res.reverse! | |
res.each do |match| | |
match = match.class == String ? match : match[:path] | |
if type == "file" | |
display = match.sub(/^.*?\/([^\/]+)$/,"\\1") | |
display.gsub!(/^[\d-]+/,'') | |
display.gsub!(/\.(md|markdown)$/,'') | |
display.gsub!(/-/,' ') | |
elsif type == "download" | |
display = match.sub(/^\d+:\s*/,'') | |
else | |
display = match.strip | |
end | |
printf("%2d ) %s\n", counter, display) | |
counter += 1 | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment