Skip to content

Instantly share code, notes, and snippets.

@xentec
Last active April 21, 2019 19:40
Show Gist options
  • Save xentec/cee32e4783c60d4afc4c11e67cfabf66 to your computer and use it in GitHub Desktop.
Save xentec/cee32e4783c60d4afc4c11e67cfabf66 to your computer and use it in GitHub Desktop.
#!/bin/env fish
set file_name_base (date "+%Y-%m-%d_%H-%M-%S")
#set url "http://example.com"
#set uploader "rsync"
#set uploader_target "server:/srv/http/"
if [ $WAYLAND_DISPLAY ];
set IS_WAYLAND 1
set SEL_CMD 'slurp' -d
set CAP_CMD 'grim' -g
else if [ $DISPLAY ];
set IS_X11 1
set SEL_CMD 'slop' -b 2 -c 0.5,0.5,0.5,0.8
set CAP_CMD 'maim' -u -g
else
end
function notify -a title text timeout category
[ $category ]; and set c "-c"
notify-send -a scr -t (math "20 * 1000") $c $category $title $text
end
function clipit -a text
if [ $IS_WAYLAND ]; and command -vq wl-copy;
wl-copy $text
else if [ $IS_X11 ]; and command -vq xclip;
echo -n $text | xclip -selection clipboard
else
echo "No clipboard tool found! You'll need to copy the URL yourself."
end
end
function upload -a file_path
set -l file_name (basename $file_path)
notify "$file_name" "Uploading..." 3 "transfer"
set -l WHY ($uploader $file_name $uploader_target 2>&1)
if [ $status -ne 0 ];
notify "$file_name" "Failed: $WHY" 5 "transfer.failed"
exit
end
clipit $url/$file_name
notify "$file_name" "Done!" 5 "transfer.done"
end
set mode "image"
[ $argv[1] ]; and set mode $argv[1];
switch $mode
case image
set area ($SEL_CMD)
set file_tmp "/tmp/scr.png"
$CAP_CMD $area $file_tmp
[ $status -ne 0 ]; and exit 1
set file_name $file_name_base-(string sub -l 8 (openssl blake2b512 -r "/tmp/scr.png")).webp
set file_path /tmp/$file_name
notify "$file_name" "Optimizing..." 3
cwebp -quiet -mt -lossless -q 100 -m 6 $file_tmp -o $file_path
if [ $status -ne 0 ]
notify "$file_name" "Optimizsation failed"
set file_name (basename $file_name .webp).png
set file_path /tmp/$file_name
mv $file_tmp $file_path
end
case video
notify "Soon..."
echo "Soon..."
exit 1
end
if [ $uploader ]; and [ $uploader_target ]
upload $file_path
echo "$url/$file_name"
else
echo $file_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment