deicidus 2 years ago
parent
commit
668c433700
  1. 65
      spells/cantrips/fathom-cursor

65
spells/cantrips/fathom-cursor

@ -1,53 +1,44 @@
#!/usr/bin/env sh #!/bin/sh
# This spell reveals the x and y coordinates of the cursor in the terminal window. # 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 # Define the function to calculate the position of the cursor
fathom_cursor() { fathom_cursor() {
local position x=false
local x=false y=false
local y=false verbose=false
local verbose=false for arg; do
while getopts 'vxy' flag; do case "$arg" in
case "${flag}" in --verbose|-v) verbose=true ;;
--verbose|v) verbose=true ;; -x) x=true ;;
x) x=true ;; -y) y=true ;;
y) y=true ;;
esac esac
done done
# Get the position of the cursor # Get the position of the cursor
stty -echo stty -echo
printf "\033[6n" printf "\033[6n"
while IFS=';' read -r -d R ROW COL; do IFS=';' read -r -d R ROW COL
break stty echo
done position=$(printf "%s;%s" "${ROW#*[}" "${COL%%R*}")
stty echo
position=$(printf "%s;%s" "${COL%%R*}" "${ROW#*[}")
if [ $x = true ] && [ $y = true ]; then if $x && $y; then
if [ $verbose = true ]; then if $verbose; then
printf "X: %s\n" "${position##*;}" printf "X: %s\n" "${position%%;*}"
printf "Y: %s\n" "${position%%;*}" printf "Y: %s\n" "${position##*;}"
else else
printf "%s\n" "${position##*;}" printf "%s\n" "${position%%;*}"
printf "%s\n" "${position%%;*}" printf "%s\n" "${position##*;}"
fi fi
else else
if [ $x = true ]; then if $x; then
if [ $verbose = true ]; then [ "$verbose" = true ] && printf "X: "
printf "X: "
fi
printf "%s\n" "${position##*;}"
elif [ $y = true ]; then
if [ $verbose = true ]; then
printf "Y: "
fi
printf "%s\n" "${position%%;*}" printf "%s\n" "${position%%;*}"
elif $y; then
[ "$verbose" = true ] && printf "Y: "
printf "%s\n" "${position##*;}"
else else
if [ $verbose = true ]; then [ "$verbose" = true ] && printf "Position: "
printf "Position: "
fi
printf "%s\n" "$position" printf "%s\n" "$position"
fi fi
fi fi
@ -63,4 +54,4 @@ test_fathom_cursor() {
assert_equal "$(./fathom-cursor)" "$(./fathom-cursor -x -y)" "fathom_cursor and -x -y outputs differ" assert_equal "$(./fathom-cursor)" "$(./fathom-cursor -x -y)" "fathom_cursor and -x -y outputs differ"
} }
fathom_cursor "$@" fathom_cursor "$@"
Loading…
Cancel
Save