deicidus
1 year ago
1 changed files with 26 additions and 34 deletions
@ -1,59 +1,51 @@
|
||||
#!/bin/sh |
||||
#!/usr/bin/env 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() { |
||||
position="" |
||||
x=false |
||||
y=false |
||||
verbose=false |
||||
for arg; do |
||||
case "$arg" in |
||||
--verbose|-v) verbose=true ;; |
||||
-x) x=true ;; |
||||
-y) y=true ;; |
||||
while getopts 'vxy' flag; do |
||||
case "${flag}" 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 |
||||
position=$(IFS=';' read -sdR -p $'\E[6n' ROW COL; printf "%s;%s" "${ROW#*[}" "$COL") |
||||
|
||||
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##*;}" |
||||
if [ "$x" = true ] && [ "$y" = true ]; then |
||||
if [ "$verbose" = true ]; then |
||||
printf "X: %s\n" "${position##*;}" |
||||
printf "Y: %s\n" "${position%%;*}" |
||||
else |
||||
printf "%s\n" "${position%%;*}" |
||||
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: " |
||||
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 |
||||
printf "%s\n" "${position%%;*}" |
||||
else |
||||
[ "$verbose" = true ] && printf "Position: " |
||||
if [ "$verbose" = true ]; then |
||||
printf "Position: " |
||||
fi |
||||
printf "%s\n" "$position" |
||||
fi |
||||
fi |
||||
} |
||||
|
||||
# Call the function |
||||
fathom_cursor "$@" |
Loading…
Reference in new issue