Last active
March 13, 2020 09:18
-
-
Save celediel/c25b241ca4ea8c20630f to your computer and use it in GitHub Desktop.
Take screenshots and display system info and scp and stuff.
This file contains 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 | |
# encoding=utf-8 | |
require 'slop' | |
require 'yaml' | |
require 'os' | |
require 'clipboard' | |
begin | |
# To play a sound when screenshot is taken | |
require 'sdl2' | |
@sdl = true | |
rescue LoadError | |
@sdl = false | |
end | |
# These are all very annoying. | |
# rubocop:disable Metrics/LineLength, Metrics/ClassLength, Metrics/CyclomaticComplexity | |
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize | |
def read_cfg | |
home = `echo $HOME`.chomp | |
begin | |
f = File.open("#{home}/.sctrc") | |
rescue Errno::ENOENT | |
return {} | |
end | |
cfg = YAML.load(f.read) | |
cfg | |
end | |
def init_sdl | |
begin | |
SDL2::Mixer::INIT_OGG | |
SDL2::Mixer.open | |
sound = File.expand_path(File.dirname(__FILE__)) + '/sct.ogg' | |
puts "sound file: #{sound}" if @opts.verbose? | |
@shutter = SDL2::Mixer::Chunk.load(sound) | |
@channel = SDL2::Mixer::Channels | |
@channel.set_volume(1, SDL2::Mixer::MAX_VOLUME) | |
puts "Chunk: #{@shutter}" if @opts.verbose? | |
puts "Channel: #{@channel}" if @opts.verbose? | |
rescue SDL2::Error => e | |
puts e if @opts.verbose? | |
return false | |
end | |
true | |
end | |
def main | |
# Options with the rad gem slop | |
@opts = Slop.parse do |o| | |
o.bool '-n', '--noscreen', 'Do not take screen shot' | |
o.bool '-s', '--scp', 'Copy file with scp' | |
o.bool '-a', '--archey', 'Display archey3' | |
o.bool '-c', '--sfc', 'Display screenfetch' | |
o.string '-f', '--file', 'Optional filename' | |
o.string '-H', '--host', 'Remote scp host' | |
o.string '-d', '--dir', 'Remote scp directory' | |
o.bool '-h', '--help', 'Show this help' | |
o.bool '-v', '--verbose', 'Yell about stuff' | |
o.bool '-S', '--select', 'Select an area of the screen to capture' | |
o.bool '-w', '--window', 'Select a single window to capture' | |
end | |
# Print help and exit | |
if @opts.help? | |
puts @opts | |
print 'Create $HOME/.sctrc file with YAML formatted SSH host, ' | |
puts 'dir for scp, and public URL to save typing' | |
puts 'host: itoshii' | |
puts 'dir: ~/burningimages' | |
puts 'url: https://burning.moe/i/' | |
exit | |
end | |
# Check for stuff tbh | |
if OS.windows? | |
puts "This probably doesn't work on windows either." | |
exit | |
end | |
s = `which maim`.chomp | |
unless File.exist?(s) | |
puts 'You need maim to use this probably.' | |
exit | |
end | |
puts 'Reading config file' if @opts.verbose? | |
cfg = read_cfg | |
# If SDL2 isn't installed, continue without it | |
# not a critical component | |
if @sdl | |
puts 'Initialising SDL with:' if @opts.verbose? | |
has_sound = init_sdl | |
puts "Has sound? #{has_sound}." if @opts.verbose? | |
else | |
has_sound = false | |
end | |
# Screenshot name | |
fname = "#{Time.now.strftime('%a_%m-%d-%Y_%H-%M-%S')}.png" | |
# Parse options | |
filename = @opts[:file].nil? ? fname : @opts[:file] | |
host = @opts[:host].nil? ? cfg['host'] : @opts[:host] | |
dir = @opts[:dir].nil? ? cfg['dir'] : @opts[:dir] | |
# System information or exiting if script doesn't exist | |
if @opts.archey? | |
ar3 = `which archey3`.chomp | |
if File.exist?(ar3) | |
puts `archey3` | |
else | |
puts 'archey3 not found' | |
end | |
end | |
if @opts.sfc? | |
sc = `which screenfetch`.chomp | |
if File.exist?(sc) | |
puts `screenfetch` | |
else | |
puts 'screenfetch not found' | |
end | |
end | |
# Take screenshot | |
unless @opts.noscreen? | |
puts 'Taking screenshot' if @opts.verbose? | |
if @opts.select? | |
s = `which slop`.chomp | |
unless File.exist?(s) | |
puts 'You need slop to select an area of the screen' | |
exit | |
end | |
`maim #{filename} -d1 -s` | |
elsif @opts.window? | |
s = `which xdotool`.chomp | |
unless File.exist?(s) | |
puts 'You need xdotool to select a specific window' | |
exit | |
end | |
`maim #{filename} -i $(xdotool selectwindow) -d1` | |
else | |
`maim #{filename} -d1` | |
end | |
puts 'Playing shutter sound' if @opts.verbose? | |
@channel.play(1, @shutter, 0) if has_sound | |
puts SDL2::Mixer::Channels.play?(1) if @opts.verbose? | |
puts 'Played shutter sound' if @opts.verbose? | |
end | |
if @opts.scp? && !@opts.noscreen? | |
if host.nil? | |
puts 'No remote host specified in options or config file.' | |
exit | |
end | |
if dir.nil? | |
puts 'No remote dir specified in options or config file.' | |
exit | |
end | |
puts "Copying #{filename} to #{host}:#{dir}" if @opts.verbose? | |
`scp #{filename} #{host}:#{dir}` | |
# Copy URL to clipboard | |
url = "#{cfg['url']}#{filename}" | |
Clipboard.copy url unless @opts.noscreen? || cfg['url'].nil? | |
puts url if @opts.verbose? | |
end | |
puts "#{cfg}\nHost: #{host}\nRemote dir: #{dir}" if @opts.verbose? | |
end | |
main if __FILE__ == $PROGRAM_NAME | |
# vim:tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab foldmethod=syntax: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment