#!/bin/sh # This spell reveals the x and y coordinates of the cursor in the terminal window. # Define the function to calculate the position of the cursor fathom_cursor() { x=false y=false verbose=false for arg; do case "$arg" in --verbose|-v) verbose=true ;; -x) x=true ;; -y) y=true ;; esac done # Get the position of the cursor oldstty=$(stty -g) stty raw -echo min 0 printf "\033[6n" > /dev/tty result="" char="" while IFS= read -r -n 1 char; do result="$result$char" if [ "$char" = "R" ]; then break fi done < /dev/tty stty "$oldstty" ROW="${result#*[}" COL="${result%;R}" position=$(printf "%s;%s" "$ROW" "$COL") if $x && $y; then if $verbose; then printf "X: %s\n" "${position%%;*}" printf "Y: %s\n" "${position##*;}" else printf "%s\n" "${position%%;*}" printf "%s\n" "${position##*;}" fi else if $x; then [ "$verbose" = true ] && printf "X: " printf "%s\n" "${position%%;*}" elif $y; then [ "$verbose" = true ] && printf "Y: " printf "%s\n" "${position##*;}" else [ "$verbose" = true ] && printf "Position: " printf "%s\n" "$position" fi fi } fathom_cursor "$@"