this repo has no description
dotfiles
1#!/usr/bin/env ruby
2
3# This script can use some improving...
4
5require 'optparse'
6require 'open3'
7
8TIME = Time.now
9DATE_PATH = TIME.strftime '%Y/%m/%d'
10LOCAL = '/tmp'
11FILENAME = "#{TIME.strftime '%Y-%m-%d_%H-%M-%S'}.png"
12REMOTE = 'scrotify' # In ~/.ssh/config
13TARGET = "/var/www/files.sharparam.com/public_html/#{DATE_PATH}"
14URL = "http://files.sharparam.com/#{DATE_PATH}/#{FILENAME}"
15
16options = {
17 mode: :all
18}
19
20# Apparently the keyring variables cannot be accessed from scripts that run
21# from i3's exec tool, so we have to manually set the socket here.
22sock = `gpgconf --list-dirs agent-ssh-socket`.strip
23ENV['SSH_AUTH_SOCK'] = sock
24OptionParser.new do |opts|
25 opts.banner = "Usage: #{$0} [options]"
26
27 opts.on('-s', '--select', 'Screenshot region') do
28 options[:mode] = :select
29 end
30
31 opts.on('-a', '--active', 'Screenshot active window') do
32 options[:mode] = :active
33 end
34end.parse!
35
36def error(msg)
37 `notify-send -u critical "Scrotify" "#{msg}"`
38 abort msg
39end
40
41def maim(args = '')
42 `maim -u #{args} #{LOCAL}/#{FILENAME}`
43 error 'maim failed' unless $?.exitstatus == 0
44
45 `notify-send -u low "Scrotify" "Uploading screenshot..."`
46
47 `ssh -q #{REMOTE} mkdir -p #{TARGET}/`
48 error 'ssh mkdir failed' unless $?.exitstatus == 0
49
50 `rsync -q #{LOCAL}/#{FILENAME} #{REMOTE}:#{TARGET}/`
51 error 'rsync failed' unless $?.exitstatus == 0
52
53 `rm #{LOCAL}/#{FILENAME}`
54 Open3.popen2('xclip -sel c') { |i, _, _| i.print(URL); i.close; }
55
56 `notify-send -u normal "Scrotify" "#{URL}"`
57end
58
59def capture(mode = :all)
60 case mode
61 when :select
62 region = `slop -f %g`.strip
63 maim "-g #{region}"
64 when :active
65 `xdotool getactivewindow getwindowgeometry` =~ /(\d+),(\d+).+?(\d+)x(\d+)/m
66 maim "-g #{$3}x#{$4}+#{$1}+#{$2}"
67 else
68 maim
69 end
70end
71
72capture options[:mode]