ao-mud is a spellbook of well-commented atomic bash scripts that each do one thing. we are building semantic building blocks for an autonomously-evolving digital spellcasting language.
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.

58 lines
2.0 KiB

2 years ago
#!/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() {
2 years ago
x=false
y=false
verbose=false
for arg; do
case "$arg" in
--verbose|-v) verbose=true ;;
-x) x=true ;;
-y) y=true ;;
esac
done
2 years ago
# Get the position of the cursor
stty -echo
printf "\033[6n"
2 years ago
oldIFS="$IFS"; IFS=';' read -r ROW COL
2 years ago
stty echo
position=$(printf "%s;%s" "${ROW#*[}" "${COL%%R*}")
2 years ago
IFS="$oldIFS"
2 years ago
if $x && $y; then
if $verbose; then
printf "X: %s\n" "${position%%;*}"
printf "Y: %s\n" "${position##*;}"
else
2 years ago
printf "%s\n" "${position%%;*}"
printf "%s\n" "${position##*;}"
fi
else
2 years ago
if $x; then
[ "$verbose" = true ] && printf "X: "
printf "%s\n" "${position%%;*}"
2 years ago
elif $y; then
[ "$verbose" = true ] && printf "Y: "
printf "%s\n" "${position##*;}"
else
2 years ago
[ "$verbose" = true ] && printf "Position: "
printf "%s\n" "$position"
fi
fi
}
test_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"
}
2 years ago
fathom_cursor "$@"