From 668c43370014f5f0ca9051eed656b38cbec84e14 Mon Sep 17 00:00:00 2001 From: deicidus Date: Fri, 12 May 2023 12:33:03 -0700 Subject: [PATCH] POSIX --- spells/cantrips/fathom-cursor | 65 +++++++++++++++-------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/spells/cantrips/fathom-cursor b/spells/cantrips/fathom-cursor index 805310a..0ca984c 100755 --- a/spells/cantrips/fathom-cursor +++ b/spells/cantrips/fathom-cursor @@ -1,53 +1,44 @@ -#!/usr/bin/env sh +#!/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() { - local position - local x=false - local y=false - local verbose=false - while getopts 'vxy' flag; do - case "${flag}" in - --verbose|v) verbose=true ;; - x) x=true ;; - y) y=true ;; + 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 - stty -echo - printf "\033[6n" - while IFS=';' read -r -d R ROW COL; do - break - done - stty echo - position=$(printf "%s;%s" "${COL%%R*}" "${ROW#*[}") + # Get the position of the cursor + stty -echo + printf "\033[6n" + IFS=';' read -r -d R ROW COL + stty echo + position=$(printf "%s;%s" "${ROW#*[}" "${COL%%R*}") - if [ $x = true ] && [ $y = true ]; then - if [ $verbose = true ]; then - printf "X: %s\n" "${position##*;}" - printf "Y: %s\n" "${position%%;*}" + 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%%;*}" + printf "%s\n" "${position%%;*}" + printf "%s\n" "${position##*;}" fi else - 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 + if $x; then + [ "$verbose" = true ] && printf "X: " printf "%s\n" "${position%%;*}" + elif $y; then + [ "$verbose" = true ] && printf "Y: " + printf "%s\n" "${position##*;}" else - if [ $verbose = true ]; then - printf "Position: " - fi + [ "$verbose" = true ] && printf "Position: " printf "%s\n" "$position" fi fi @@ -63,4 +54,4 @@ test_fathom_cursor() { assert_equal "$(./fathom-cursor)" "$(./fathom-cursor -x -y)" "fathom_cursor and -x -y outputs differ" } -fathom_cursor "$@" +fathom_cursor "$@" \ No newline at end of file