Browse Source

Giving up, fathom-cursor is not POSIX... for now

main
deicidus 2 years ago
parent
commit
9671e59033
  1. 49
      spells/cantrips/fathom-cursor

49
spells/cantrips/fathom-cursor

@ -1,13 +1,13 @@
#!/usr/bin/env sh #!/bin/bash
# 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() {
position="" local position
x=false local x=false
y=false local y=false
verbose=false local verbose=false
while getopts 'vxy' flag; do while getopts 'vxy' flag; do
case "${flag}" in case "${flag}" in
--verbose|v) verbose=true ;; --verbose|v) verbose=true ;;
@ -19,27 +19,27 @@ fathom_cursor() {
# Get the position of the cursor # Get the position of the cursor
position=$(IFS=';' read -sdR -p $'\E[6n' ROW COL; printf "%s;%s" "${ROW#*[}" "$COL") position=$(IFS=';' read -sdR -p $'\E[6n' ROW COL; printf "%s;%s" "${ROW#*[}" "$COL")
if [ "$x" = true ] && [ "$y" = true ]; then if [ $x = true ] && [ $y = true ]; then
if [ "$verbose" = true ]; then if [ $verbose = true ]; 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 = true ]; then
if [ "$verbose" = true ]; then if [ $verbose = true ]; then
printf "X: " printf "X: "
fi fi
printf "%s\n" "${position##*;}" printf "%s\n" "${position##*;}"
elif [ "$y" = true ]; then elif [ $y = true ]; then
if [ "$verbose" = true ]; then if [ $verbose = true ]; then
printf "Y: " printf "Y: "
fi fi
printf "%s\n" "${position%%;*}" printf "%s\n" "${position%%;*}"
else else
if [ "$verbose" = true ]; then if [ $verbose = true ]; then
printf "Position: " printf "Position: "
fi fi
printf "%s\n" "$position" printf "%s\n" "$position"
@ -47,5 +47,18 @@ fathom_cursor() {
fi fi
} }
# Call the function test_fathom_cursor() {
fathom_cursor "$@" assert_equal "$(./fathom-cursor -x)" "$(./fathom-cursor -y)" "fathom_cursor -x and -y outputs differ"
assert_equal "$(./fathom-cursor -x -v)" "X: $(./fathom-cursor -x)" "fathom_cursor -x and -x -v outputs differ"
assert_equal "$(./fathom-cursor -y -v)" "Y: $(./fathom-cursor -y)" "fathom_cursor -y and -y -v outputs differ"
assert_equal "$(./fathom-cursor -x -y -v)" "X: $(./fathom-cursor -x)\nY: $(./fathom-cursor -y)" "fathom_cursor -x -y and -x -y -v outputs differ"
assert_equal "$(./fathom-cursor -v -x -y)" "Position: $(./fathom-cursor -x -y)" "fathom_cursor -v -x -y and -x -y outputs differ"
assert_equal "$(./fathom-cursor -v)" "$(./fathom-cursor -x -y -v)" "fathom_cursor -v and -x -y -v outputs differ"
assert_equal "$(./fathom-cursor)" "$(./fathom-cursor -x -y)" "fathom_cursor and -x -y outputs differ"
}
# Check if the script is being called from another script
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
# If not, call the function
fathom_cursor "$@"
fi

Loading…
Cancel
Save