You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
59 lines
1.4 KiB
#!/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 "$@" |