From 0a9abef2af93407280b8e2ab5b1671bb069114a1 Mon Sep 17 00:00:00 2001 From: Myned Date: Sat, 21 Sep 2024 07:35:32 -0500 Subject: [PATCH] screenshot: rewrite with argc Signed-off-by: Myned --- options/custom/desktops/hyprland/binds.nix | 8 +-- options/custom/scripts/default.nix | 1 + options/custom/scripts/screenshot.sh | 77 ++++++++++++++++++---- 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/options/custom/desktops/hyprland/binds.nix b/options/custom/desktops/hyprland/binds.nix index 6c8c702..35e23e1 100644 --- a/options/custom/desktops/hyprland/binds.nix +++ b/options/custom/desktops/hyprland/binds.nix @@ -138,10 +138,10 @@ in { (key "Left" "Super" "movewindow" "l") (key "Left" "Super+Shift" "movewindoworgroup" "l") (key "Minus" "Super" "exec" "audio") - (key "Print" "Shift" "exec" "screenshot -d") - (key "Print" "Super" "exec" "screenshot -e") - (key "Print" "Super+Shift" "exec" "screenshot -ed") - (key "Print" null "exec" "screenshot") + (key "Print" "Shift" "exec" "screenshot display") + (key "Print" "Super" "exec" "screenshot selection --edit") + (key "Print" "Super+Shift" "exec" "screenshot display --edit") + (key "Print" null "exec" "screenshot selection") (key "Return" "Super" "fullscreen" "1") # Maximize (key "Return" "Super+Shift" "fullscreen" "0") # Fullscreen (key "Right" "Super" "movewindow" "r") diff --git a/options/custom/scripts/default.nix b/options/custom/scripts/default.nix index adf2321..f1f0130 100644 --- a/options/custom/scripts/default.nix +++ b/options/custom/scripts/default.nix @@ -125,6 +125,7 @@ in { power-profiles-daemon ]) (bash "screenshot" [ + argc coreutils grimblast imagemagick diff --git a/options/custom/scripts/screenshot.sh b/options/custom/scripts/screenshot.sh index 7370e8c..4804762 100644 --- a/options/custom/scripts/screenshot.sh +++ b/options/custom/scripts/screenshot.sh @@ -1,10 +1,21 @@ #! /usr/bin/env bash -# Screenshot region or display with rounded corners +# @describe Wrapper for screenshot tools +# +# https://github.com/sigoden/argc +# https://github.com/hyprwm/contrib/tree/main/grimblast +# https://github.com/jtheoof/swappy -r=20 # Radius +# @meta combine-shorts +# @meta inherit-flag-options +# @flag -e --edit Edit screenshot with swappy +# @flag -nc --no-copy Do not copy to clipboard after capturing +# @flag -nf --no-freeze Do not freeze display before capturing + +# TODO: May not be needed for rounded corners on Hyprland +_round() { + r=20 # Radius -function round() { magick - \ \( +clone -alpha extract -draw "fill black polygon 0,0 0,$r $r,0 fill white circle $r,$r $r,0" \ \( +clone -flip \) -compose Multiply -composite \ @@ -12,12 +23,54 @@ function round() { -alpha off -compose CopyOpacity -composite - } -# TODO: Use proper flags -# TODO: Add clipboard support -if [[ "${1-}" == '-e' ]]; then - grimblast --freeze save area - | swappy --file - -elif [[ "${1-}" == '-d' ]]; then - grimblast --freeze save output - > "$XDG_SCREENSHOTS_DIR/$(date +'%F %H-%M-%S').png" -else - grimblast --freeze save area - > "$XDG_SCREENSHOTS_DIR/$(date +'%F %H-%M-%S').png" -fi +_capture() { + # Build arguments in array + command=(grimblast) + + if [[ ! "${argc_no_freeze:-}" ]]; then + command+=(--freeze) + fi + + # Always save + if [[ ! "${argc_no_copy:-}" ]]; then + command+=(copysave "$1") + else + command+=(save "$1") + fi + + command+=("${argc_file:-}") + + "${command[@]}" + + # Edit after first capture + if [[ "${argc_edit:-}" ]]; then + swappy --file "${argc_file:-}" --output-file "${argc_file_edit:-}" + fi +} + +_default_file() { + echo "$XDG_SCREENSHOTS_DIR/$(date +'%F %H-%M-%S').png" # 1970-01-01 00-00-00.png +} + +_default_file_edit() { + echo "$XDG_SCREENSHOTS_DIR/$(date +'%F %H-%M-%S').edit.png" # 1970-01-01 00-00-00.edit.png +} + +# @cmd Screenshot entire display +# @alias d,di,dis,disp,displ,displa,o,ou,out,outp,outpu,output +# @arg file=`_default_file` File to save screenshot as, defaults to "$XDG_SCREENSHOTS_DIR/$(date +'%F %H-%M-%S').png" +# @arg file_edit=`_default_file_edit` File to save edited screenshot as with swappy, defaults to "$XDG_SCREENSHOTS_DIR/$(date +'%F %H-%M-%S').edit.png" +display() { + _capture output +} + +# @cmd Screenshot selected area +# @meta default-subcommand +# @alias s,se,sel,sele,selec,select,selecti,selectio,a,ar,are,area +# @arg file=`_default_file` File to save screenshot as, defaults to "$XDG_SCREENSHOTS_DIR/$(date +'%F %H-%M-%S').png" +# @arg file_edit=`_default_file_edit` File to save edited screenshot as with swappy, defaults to "$XDG_SCREENSHOTS_DIR/$(date +'%F %H-%M-%S').edit.png" +selection() { + _capture area +} + +eval "$(argc --argc-eval "$0" "$@")"