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