deicidus
2 years 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. |
# 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="" |
||||||
x=false |
x=false |
||||||
y=false |
y=false |
||||||
verbose=false |
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 |
||||||
oldstty=$(stty -g) |
position=$(IFS=';' read -sdR -p $'\E[6n' ROW COL; printf "%s;%s" "${ROW#*[}" "$COL") |
||||||
stty raw -echo min 0 |
|
||||||
printf "\033[6n" > /dev/tty |
|
||||||
|
|
||||||
result="" |
if [ "$x" = true ] && [ "$y" = true ]; then |
||||||
char="" |
if [ "$verbose" = true ]; then |
||||||
while IFS= read -r -n 1 char; do |
printf "X: %s\n" "${position##*;}" |
||||||
result="$result$char" |
printf "Y: %s\n" "${position%%;*}" |
||||||
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 |
else |
||||||
printf "%s\n" "${position%%;*}" |
|
||||||
printf "%s\n" "${position##*;}" |
printf "%s\n" "${position##*;}" |
||||||
|
printf "%s\n" "${position%%;*}" |
||||||
fi |
fi |
||||||
else |
else |
||||||
if $x; then |
if [ "$x" = true ]; then |
||||||
[ "$verbose" = true ] && printf "X: " |
if [ "$verbose" = true ]; then |
||||||
printf "%s\n" "${position%%;*}" |
printf "X: " |
||||||
elif $y; then |
fi |
||||||
[ "$verbose" = true ] && printf "Y: " |
|
||||||
printf "%s\n" "${position##*;}" |
printf "%s\n" "${position##*;}" |
||||||
|
elif [ "$y" = true ]; then |
||||||
|
if [ "$verbose" = true ]; then |
||||||
|
printf "Y: " |
||||||
|
fi |
||||||
|
printf "%s\n" "${position%%;*}" |
||||||
else |
else |
||||||
[ "$verbose" = true ] && printf "Position: " |
if [ "$verbose" = true ]; then |
||||||
|
printf "Position: " |
||||||
|
fi |
||||||
printf "%s\n" "$position" |
printf "%s\n" "$position" |
||||||
fi |
fi |
||||||
fi |
fi |
||||||
} |
} |
||||||
|
|
||||||
|
# Call the function |
||||||
fathom_cursor "$@" |
fathom_cursor "$@" |
Loading…
Reference in new issue