💄 style(hypr): hyprlock script indentation corrected to 2 spaces

This commit is contained in:
Sergio Laín 2024-07-25 18:56:17 +02:00
parent 019ccf9272
commit 341c78ff12
No known key found for this signature in database
GPG key ID: 8429B2EE312F8150

View file

@ -3,7 +3,7 @@
set -e set -e
function Help() { function Help() {
cat <<EOF cat <<EOF
Usage: hyprshot [options ..] [-m [mode] ..] -- [command] Usage: hyprshot [options ..] [-m [mode] ..] -- [command]
Hyprshot is an utility to easily take screenshot in Hyprland using your mouse. Hyprshot is an utility to easily take screenshot in Hyprland using your mouse.
@ -20,11 +20,12 @@ Options:
-m, --mode one of: output, window, region, active, OUTPUT_NAME -m, --mode one of: output, window, region, active, OUTPUT_NAME
-o, --output-folder directory in which to save screenshot -o, --output-folder directory in which to save screenshot
-f, --filename the file name of the resulting screenshot -f, --filename the file name of the resulting screenshot
-D, --delay how long to delay taking the screenshot after selection (seconds)
-z, --freeze freeze the screen on initialization
-d, --debug print debug information -d, --debug print debug information
-s, --silent don't send notification when screenshot is saved -s, --silent don't send notification when screenshot is saved
-r, --raw output raw image data to stdout -r, --raw output raw image data to stdout
-t, --notif-timeout notification timeout in milliseconds (default 5000) -t, --notif-timeout notification timeout in milliseconds (default 5000)
-z, --freeze freeze current output to take screenshot
--clipboard-only copy screenshot to clipboard and don't save image in disk --clipboard-only copy screenshot to clipboard and don't save image in disk
-- [command] open screenshot with a command of your choosing. e.g. hyprshot -m window -- mirage -- [command] open screenshot with a command of your choosing. e.g. hyprshot -m window -- mirage
@ -41,267 +42,279 @@ EOF
} }
function Print() { function Print() {
if [ $DEBUG -eq 0 ]; then if [ $DEBUG -eq 0 ]; then
return 0 return 0
fi fi
1>&2 printf "$@" 1>&2 printf "$@"
} }
function send_notification() { function send_notification() {
if [ $SILENT -eq 1 ]; then if [ $SILENT -eq 1 ]; then
return 0 return 0
fi fi
local message=$([ $CLIPBOARD -eq 1 ] && local message=$([ $CLIPBOARD -eq 1 ] &&
echo "Image copied to the clipboard" || echo "Image copied to the clipboard" ||
echo "Image saved in <i>${1}</i> and copied to the clipboard.") echo "Image saved in <i>${1}</i> and copied to the clipboard.")
action=$(notify-send -i "$1" --action "View Image" "Screenshot saved" \ action=$(notify-send -i "$1" --action "View Image" "Screenshot saved" \
"${message}" \ "${message}" \
-t "$NOTIF_TIMEOUT" -i "${1}" -a Hyprshot) -t "$NOTIF_TIMEOUT" -i "${1}" -a Hyprshot)
if [ -n "$action" ]; then
if command -v satty &>/dev/null; then if [ -n "$action" ]; then
satty -f "$1" if command -v satty &>/dev/null; then
else satty -f "$1"
xdg-open "$1" else
fi xdg-open "$1"
fi fi
fi
} }
function trim() { function trim() {
local geometry="${1}" Print "Geometry: %s\n" "${1}"
local xy_str=$(echo "${geometry}" | cut -d' ' -f1) local geometry="${1}"
local wh_str=$(echo "${geometry}" | cut -d' ' -f2) local xy_str=$(echo "${geometry}" | cut -d' ' -f1)
local x=$(echo "${xy_str}" | cut -d',' -f1) local wh_str=$(echo "${geometry}" | cut -d' ' -f2)
local y=$(echo "${xy_str}" | cut -d',' -f2) local x=$(echo "${xy_str}" | cut -d',' -f1)
local width=$(echo "${wh_str}" | cut -dx -f1) local y=$(echo "${xy_str}" | cut -d',' -f2)
local height=$(echo "${wh_str}" | cut -dx -f2) local width=$(echo "${wh_str}" | cut -dx -f1)
local height=$(echo "${wh_str}" | cut -dx -f2)
local max_width=$(hyprctl monitors -j | jq -r '[.[] | if (.transform % 2 == 0) then (.x + .width) else (.x + .height) end] | max') local max_width=$(hyprctl monitors -j | jq -r '[.[] | if (.transform % 2 == 0) then (.x + .width) else (.x + .height) end] | max')
local max_height=$(hyprctl monitors -j | jq -r '[.[] | if (.transform % 2 == 0) then (.y + .height) else (.y + .width) end] | max') local max_height=$(hyprctl monitors -j | jq -r '[.[] | if (.transform % 2 == 0) then (.y + .height) else (.y + .width) end] | max')
local min_x=$(hyprctl monitors -j | jq -r '[.[] | (.x)] | min') local min_x=$(hyprctl monitors -j | jq -r '[.[] | (.x)] | min')
local min_y=$(hyprctl monitors -j | jq -r '[.[] | (.y)] | min') local min_y=$(hyprctl monitors -j | jq -r '[.[] | (.y)] | min')
local cropped_x=$x local cropped_x=$x
local cropped_y=$y local cropped_y=$y
local cropped_width=$width local cropped_width=$width
local cropped_height=$height local cropped_height=$height
if ((x + width > max_width)); then if ((x + width > max_width)); then
cropped_width=$((max_width - x)) cropped_width=$((max_width - x))
fi fi
if ((y + height > max_height)); then if ((y + height > max_height)); then
cropped_height=$((max_height - y)) cropped_height=$((max_height - y))
fi fi
if ((x < min_x)); then if ((x < min_x)); then
cropped_x="$min_x" cropped_x="$min_x"
cropped_width=$((cropped_width + x - min_x)) cropped_width=$((cropped_width + x - min_x))
fi fi
if ((y < min_y)); then if ((y < min_y)); then
cropped_y="$min_y" cropped_y="$min_y"
cropped_height=$((cropped_height + y - min_y)) cropped_height=$((cropped_height + y - min_y))
fi fi
printf "%s,%s %sx%s\n" \ local cropped=$(printf "%s,%s %sx%s\n" \
"${cropped_x}" "${cropped_y}" \ "${cropped_x}" "${cropped_y}" \
"${cropped_width}" "${cropped_height}" "${cropped_width}" "${cropped_height}")
Print "Crop: %s\n" "${cropped}"
echo ${cropped}
} }
function save_geometry() { function save_geometry() {
Print "Geometry: %s\n" "${1}" local geometry="${1}"
local cropped_geometry=$(trim "${1}") local output=""
Print "Crop: %s\n" "${cropped_geometry}"
local output=""
if [ $RAW -eq 1 ]; then if [ $RAW -eq 1 ]; then
grim -g "${cropped_geometry}" - grim -g "${geometry}" -
return 0 return 0
fi fi
if [ $CLIPBOARD -eq 0 ]; then if [ $CLIPBOARD -eq 0 ]; then
mkdir -p "$SAVEDIR" mkdir -p "$SAVEDIR"
grim -g "${cropped_geometry}" "$SAVE_FULLPATH" grim -g "${geometry}" "$SAVE_FULLPATH"
output="$SAVE_FULLPATH" output="$SAVE_FULLPATH"
wl-copy <"$output" wl-copy --type image/png <"$output"
[ -z "$COMMAND" ] || { [ -z "$COMMAND" ] || {
"$COMMAND" "$output" "$COMMAND" "$output"
} }
else else
wl-copy < <(grim -g "${cropped_geometry}" -) wl-copy --type image/png < <(grim -g "${geometry}" -)
fi fi
send_notification $output send_notification $output
}
function checkRunning() {
sleep 1
while [[ 1 == 1 ]]; do
if [[ $(pgrep slurp | wc -m) == 0 ]]; then
pkill hyprpicker
exit
fi
done
} }
function begin_grab() { function begin_grab() {
local option=$1 if [ $FREEZE -eq 1 ] && [ "$(command -v "hyprpicker")" ] >/dev/null 2>&1; then
hyprpicker -r -z &
if [ $FREEZE -eq 1 ]; then sleep 0.2
sleep 0.5 HYPRPICKER_PID=$!
hyprpicker -r -z & fi
sleep 0.2 local option=$1
local picker_pid=$! case $option in
fi output)
if [ $CURRENT -eq 1 ]; then
case $option in local geometry=$(grab_active_output)
output) elif [ -z $SELECTED_MONITOR ]; then
if [ $CURRENT -eq 1 ]; then local geometry=$(grab_output)
local geometry=$(grab_active_output) else
elif [ -z $SELECTED_MONITOR ]; then local geometry=$(grab_selected_output $SELECTED_MONITOR)
local geometry=$(grab_output) fi
else ;;
local geometry=$(grab_selected_output $SELECTED_MONITOR) region)
fi local geometry=$(grab_region)
;; ;;
region) window)
local geometry=$(grab_region) if [ $CURRENT -eq 1 ]; then
;; local geometry=$(grab_active_window)
window) else
if [ $CURRENT -eq 1 ]; then local geometry=$(grab_window)
local geometry=$(grab_active_window) fi
else geometry=$(trim "${geometry}")
local geometry=$(grab_window) ;;
fi esac
;; if [ ${DELAY} -gt 0 ] 2>/dev/null; then
esac sleep ${DELAY}
fi
save_geometry "${geometry}" save_geometry "${geometry}"
if [ $FREEZE -eq 1 ]; then
kill $picker_pid
fi
} }
function grab_output() { function grab_output() {
slurp -or slurp -or
} }
function grab_active_output() { function grab_active_output() {
local active_workspace=$(hyprctl -j activeworkspace) local active_workspace=$(hyprctl -j activeworkspace)
local monitors=$(hyprctl -j monitors) local monitors=$(hyprctl -j monitors)
Print "Monitors: %s\n" "$monitors" Print "Monitors: %s\n" "$monitors"
Print "Active workspace: %s\n" "$active_workspace" Print "Active workspace: %s\n" "$active_workspace"
local current_monitor="$(echo $monitors | jq -r 'first(.[] | select(.activeWorkspace.id == '$(echo $active_workspace | jq -r '.id')'))')" local current_monitor="$(echo $monitors | jq -r 'first(.[] | select(.activeWorkspace.id == '$(echo $active_workspace | jq -r '.id')'))')"
Print "Current output: %s\n" "$current_monitor" Print "Current output: %s\n" "$current_monitor"
echo $current_monitor | jq -r '"\(.x),\(.y) \(.width/.scale|round)x\(.height/.scale|round)"' echo $current_monitor | jq -r '"\(.x),\(.y) \(.width/.scale|round)x\(.height/.scale|round)"'
} }
function grab_selected_output() { function grab_selected_output() {
local monitor=$(hyprctl -j monitors | jq -r '.[] | select(.name == "'$(echo $1)'")') local monitor=$(hyprctl -j monitors | jq -r '.[] | select(.name == "'$(echo $1)'")')
Print "Capturing: %s\n" "${1}" Print "Capturing: %s\n" "${1}"
echo $monitor | jq -r '"\(.x),\(.y) \(.width/.scale|round)x\(.height/.scale|round)"' echo $monitor | jq -r '"\(.x),\(.y) \(.width/.scale|round)x\(.height/.scale|round)"'
} }
function grab_region() { function grab_region() {
slurp -d slurp -d
} }
function grab_window() { function grab_window() {
local monitors=$(hyprctl -j monitors) local monitors=$(hyprctl -j monitors)
local clients=$(hyprctl -j clients | jq -r '[.[] | select(.workspace.id | contains('$(echo $monitors | jq -r 'map(.activeWorkspace.id) | join(",")')'))]') local clients=$(hyprctl -j clients | jq -r '[.[] | select(.workspace.id | contains('$(echo $monitors | jq -r 'map(.activeWorkspace.id) | join(",")')'))]')
Print "Monitors: %s\n" "$monitors" Print "Monitors: %s\n" "$monitors"
Print "Clients: %s\n" "$clients" Print "Clients: %s\n" "$clients"
# Generate boxes for each visible window and send that to slurp # Generate boxes for each visible window and send that to slurp
# through stdin # through stdin
local boxes="$(echo $clients | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1]) \(.title)"')" local boxes="$(echo $clients | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1]) \(.title)"' | cut -f1,2 -d' ')"
Print "Boxes:\n%s\n" "$boxes" Print "Boxes:\n%s\n" "$boxes"
slurp -r <<<"$boxes" slurp -r <<<"$boxes"
} }
function grab_active_window() { function grab_active_window() {
local active_window=$(hyprctl -j activewindow) local active_window=$(hyprctl -j activewindow)
local box=$(echo $active_window | jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"') local box=$(echo $active_window | jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' | cut -f1,2 -d' ')
Print "Box:\n%s\n" "$box" Print "Box:\n%s\n" "$box"
echo "$box" echo "$box"
} }
function parse_mode() { function parse_mode() {
local mode="${1}" local mode="${1}"
case $mode in case $mode in
window | region | output) window | region | output)
OPTION=$mode OPTION=$mode
;; ;;
active) active)
CURRENT=1 CURRENT=1
;; ;;
*) *)
hyprctl monitors -j | jq -re '.[] | select(.name == "'$(echo $mode)'")' &>/dev/null hyprctl monitors -j | jq -re '.[] | select(.name == "'$(echo $mode)'")' &>/dev/null
SELECTED_MONITOR=$mode SELECTED_MONITOR=$mode
;; ;;
esac esac
} }
function args() { function args() {
local options=$(getopt -o hf:o:m:dsrt:z --long help,filename:,output-folder:,mode:,clipboard-only,debug,silent,raw,notif-timeout:,freeze -- "$@") local options=$(getopt -o hf:o:m:D:dszr:t: --long help,filename:,output-folder:,mode:,delay:,clipboard-only,debug,silent,freeze,raw,notif-timeout: -- "$@")
eval set -- "$options" eval set -- "$options"
while true; do while true; do
case "$1" in case "$1" in
-h | --help) -h | --help)
Help Help
exit exit
;; ;;
-o | --output-folder) -o | --output-folder)
shift shift
SAVEDIR=$1 SAVEDIR=$1
;; ;;
-f | --filename) -f | --filename)
shift shift
FILENAME=$1 FILENAME=$1
;; ;;
-m | --mode) -D | --delay)
shift shift
parse_mode $1 DELAY=$1
;; ;;
--clipboard-only) -m | --mode)
CLIPBOARD=1 shift
;; parse_mode $1
-d | --debug) ;;
DEBUG=1 --clipboard-only)
;; CLIPBOARD=1
-s | --silent) ;;
SILENT=1 -d | --debug)
;; DEBUG=1
-r | --raw) ;;
RAW=1 -z | --freeze)
;; FREEZE=1
-t | --notif-timeout) ;;
shift -s | --silent)
NOTIF_TIMEOUT=$1 SILENT=1
;; ;;
-z | --freeze) -r | --raw)
FREEZE=1 RAW=1
;; ;;
--) -t | --notif-timeout)
shift # Skip -- argument shift
COMMAND=${@:2} NOTIF_TIMEOUT=$1
break ;;
;; --)
esac shift # Skip -- argument
shift COMMAND=${@:2}
done break
;;
esac
shift
done
if [ -z $OPTION ]; then if [ -z $OPTION ]; then
Print "A mode is required\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n" Print "A mode is required\n\nAvailable modes are:\n\toutput\n\tregion\n\twindow\n"
exit 2 exit 2
fi fi
} }
if [ -z $1 ]; then if [ -z $1 ]; then
Help Help
exit exit
fi fi
CLIPBOARD=0 CLIPBOARD=0
DEBUG=0 DEBUG=0
SILENT=0 SILENT=0
RAW=0 RAW=0
FREEZE=0
NOTIF_TIMEOUT=5000 NOTIF_TIMEOUT=5000
CURRENT=0 CURRENT=0
FREEZE=0
[ -z "$XDG_PICTURES_DIR" ] && type xdg-user-dir &>/dev/null && XDG_PICTURES_DIR=$(xdg-user-dir PICTURES) [ -z "$XDG_PICTURES_DIR" ] && type xdg-user-dir &>/dev/null && XDG_PICTURES_DIR=$(xdg-user-dir PICTURES)
FILENAME="$(date +'%Y-%m-%d-%H%M%S_hyprshot.png')" FILENAME="$(date +'%Y-%m-%d-%H%M%S_hyprshot.png')"
[ -z "$HYPRSHOT_DIR" ] && SAVEDIR=${XDG_PICTURES_DIR:=~} || SAVEDIR=${HYPRSHOT_DIR} [ -z "$HYPRSHOT_DIR" ] && SAVEDIR=${XDG_PICTURES_DIR:=~} || SAVEDIR=${HYPRSHOT_DIR}
@ -310,4 +323,5 @@ args $0 "$@"
SAVE_FULLPATH="$SAVEDIR/$FILENAME" SAVE_FULLPATH="$SAVEDIR/$FILENAME"
[ $CLIPBOARD -eq 0 ] && Print "Saving in: %s\n" "$SAVE_FULLPATH" [ $CLIPBOARD -eq 0 ] && Print "Saving in: %s\n" "$SAVE_FULLPATH"
begin_grab $OPTION begin_grab $OPTION &
checkRunning