Created
June 24, 2015 02:28
-
-
Save nviennot/cec00dd728c10fbac8ff to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'fileutils' | |
#################### | |
# Global vars | |
#################### | |
$skip_upload=false | |
$skip_screenshot=false | |
$whole_screen=false | |
$is_mac = !!(`uname` =~ /Darwin/) | |
#################### | |
# Primitives | |
#################### | |
# Display notification. Arguments: | |
# 1) title | |
# 2) message | |
# 3) url to open on click (optional, ignored on Linux) | |
# 4) icon url (optional) | |
def display_notification(title, message, url=nil, icon_url=nil) | |
if $is_mac; then | |
`terminal-notifier -message "#{message}" -title "#{title}" -open "#{url}" -contentImage "#{icon_url}" -sound Glass` | |
else | |
`notify-send "#{title}" "#{message}" -t 3000 --icon="#{icon_url}"` | |
# notify-send "Cumulus Error" "$1" -t 5000 --icon=dialog-error | |
# notify-send "$IMG" "$URL copied to clipboard" -t 3000 --icon=$IMG | |
end | |
end | |
# Display error message an exit | |
def error(msg) | |
display_notification "Cumulus Error", msg | |
exit 1 | |
end | |
# Take an interactive screenshot and put it in ~/.cumulus | |
def take_screenshot() | |
filename="#{Dir.home}/.cumulus/screen-#{`date +"%m-%d-%Y-%H:%M:%S"`.strip}.png" | |
case [$is_mac, $whole_screen] | |
when [true, true] then `screencapture #{filename}` | |
when [true, false] then `screencapture -i #{filename}` | |
when [false,true] then `scrot #{filename}` | |
when [false,false] then `scrot -s #{filename}` | |
end | |
end | |
# Pipe text to this function to copy it to the clipboard | |
def clipboard(data) | |
if $is_mac; then | |
`echo #{data} | pbcopy` | |
else | |
`echo #{data} | xsel -i -b` | |
end | |
end | |
# Upload the specified image path to imgur and print out the path of the image. | |
def upload_image(path) | |
id = File.read("#{Dir.home}/.cumulusrc").strip | |
if ! $skip_upload; then | |
json=`curl -s -XPOST -H "Authorization: Client-ID #{id}" -F "image=@#{path}" https://api.imgur.com/3/upload` | |
$?.success? || error("Cannot upload image") | |
File.write("#{Dir.home}/.cumulus/.imgur-response", json) # Store for debugging | |
end | |
end | |
def get_last_url() | |
`cat ~/.cumulus/.imgur-response | grep -o -P '"http.*?\"' | tr -d '\\"'`.strip | |
end | |
def last_screenshot() | |
`ls -t ~/.cumulus/*.png | head -n 1`.strip | |
end | |
#################### | |
# Main functions | |
#################### | |
def open_last_screenshot() | |
if $is_mac; then | |
`open #{last_screenshot}` | |
else | |
`xdg-open #{last_screenshot}` | |
end | |
end | |
def cumulus_main() | |
if ! $skip_screenshot; then | |
take_screenshot; $?.success? || error('Failed to take screenshot') | |
end | |
# Grab the most recent screenshot in ~/.cumulus | |
img=last_screenshot | |
upload_image(img) | |
url=get_last_url | |
clipboard(url) | |
display_notification img, "#{url} copied to clipboard", url, img | |
end | |
#################### | |
# Main execution | |
#################### | |
File.exists?("#{Dir.home}/.cumulusrc") || error("Missing ~/.cumulusrc. Please create one with your imgur client id.") | |
FileUtils.mkdir_p("#{Dir.home}/.cumulus") | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{$0} [options]" | |
opts.on("--open-last", "Open the last image.") do | |
puts open_last_screenshot | |
exit 0 | |
end | |
opts.on("--get-last-url", "Echo the last imgur url and copy it to the clipboard.") do | |
puts get_last_url | |
exit 0 | |
end | |
opts.on("--whole-screen", "Take a screenshot of the whole screen. Don't prompt for selection.") do | |
$whole_screen = true | |
end | |
opts.on("--skip-screenshot", "Don't take a screenshot, just use the last image. Useful for debugging.") do | |
$skip_screenshot = true | |
end | |
opts.on("--skip-upload", "Don't upload an image, but reuse the last url. Useful for debugging.") do | |
$skip_upload = true | |
end | |
end.parse! | |
cumulus_main | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment