this repo has no description
dotfiles
at main 158 lines 2.8 kB view raw
1#!/usr/bin/env bash 2 3set -euo pipefail 4 5err() { 6 printf "%s\n" "$*" >&2 7} 8 9NAME='sharpshot' 10SELF="${0:-$NAME}" 11VERSION='0.0.1' 12OUTPUT_FILE=${SHARPSHOT_OUTPUT_FILE:-/tmp/sharpshot.png} 13COMMAND=${SHARPSHOT_COMMAND:-spectacle -b -o ${OUTPUT_FILE}} 14COPY_COMMAND=${SHARPSHOT_COPY_COMMAND:-wl-copy} 15FULL_SWITCH=${SHARPSHOT_FULL_SWITCH:--f} 16ACTIVE_SWITCH=${SHARPSHOT_ACTIVE_SWITCH:--a} 17REGION_SWITCH=${SHARPSHOT_REGION_SWITCH:--r} 18VERBOSE=false 19DRY=false 20 21verbose() { 22 if $VERBOSE; then 23 printf "%s\n" "$*" 24 fi 25} 26 27full() { 28 verbose 'Capturing full desktop' 29 if $DRY; then 30 echo "DRY-RUN, would execute: '$COMMAND $FULL_SWITCH'" 31 else 32 $COMMAND "$FULL_SWITCH" & 33 fi 34} 35 36active() { 37 verbose 'Capturing active window' 38 if $DRY; then 39 echo "DRY-RUN, would execute: '$COMMAND $ACTIVE_SWITCH'" 40 else 41 $COMMAND "$ACTIVE_SWITCH" & 42 fi 43} 44 45region() { 46 verbose 'Capturing region' 47 if $DRY; then 48 echo "DRY-RUN, would execute: '$COMMAND $REGION_SWITCH'" 49 else 50 $COMMAND "$REGION_SWITCH" & 51 fi 52} 53 54printversion() { 55 echo "$NAME v$VERSION" 56} 57 58usage() { 59 echo 'Usage:' 60 echo " $SELF [options] [action]" 61} 62 63help() { 64 cat <<EOF 65$(printversion) 66 67$(usage) 68 69Screenshot helper 70 71Options: 72 -o <PATH> Set the output path to write to 73 -c <COMMAND> Set the base command to use 74 -f <SWITCH> Set the switch to add to the base command for full screen capture 75 -a <SWITCH> Set the switch to add to the base command for active window capture 76 -r <SWITCH> Set the switch to add to the base command for region capture 77 -v Enable verbose logging 78 -d Enable dry-run 79 80 -h Display this help 81 -V Display version 82EOF 83} 84 85while getopts ':hvdVo:c:f:a:r:' option; do 86 case "$option" in 87 v) 88 VERBOSE=true 89 ;; 90 d) 91 DRY=true 92 ;; 93 V) 94 printversion 95 exit 96 ;; 97 h) 98 help 99 exit 100 ;; 101 o) 102 OUTPUT_FILE="$OPTARG" 103 ;; 104 c) 105 COMMAND="$OPTARG" 106 ;; 107 f) 108 FULL_SWITCH="$OPTARG" 109 ;; 110 a) 111 ACTIVE_SWITCH="$OPTARG" 112 ;; 113 r) 114 REGION_SWITCH="$OPTARG" 115 ;; 116 \?) 117 err "Invalid option '$OPTARG'" 118 exit 1 119 ;; 120 :) 121 err "Missing value for option '$OPTARG'" 122 exit 1 123 ;; 124 *) 125 usage 126 exit 1 127 ;; 128 esac 129done 130 131shift $((OPTIND - 1)) 132 133ACTION="${*:-full}" 134 135$DRY && echo "$NAME running in dry-run mode" 136 137verbose "Outputting to $OUTPUT_FILE" 138verbose "Base command: $COMMAND" 139verbose "Full switch: $FULL_SWITCH" 140verbose "Active switch: $ACTIVE_SWITCH" 141verbose "Region switch: $REGION_SWITCH" 142verbose "Action: $ACTION" 143 144case "$ACTION" in 145 a*) 146 active 147 ;; 148 r*) 149 region 150 ;; 151 f*) 152 full 153 ;; 154 *) 155 err "Unknown capture mode '$ACTION'" 156 exit 1 157 ;; 158esac