foxshell/fn/-z4h-get-cursor-pos
2025-07-04 11:48:40 -05:00

24 lines
530 B
Bash

#!/usr/bin/env zsh
#
# If invoked with an argument, passes unrecognized TTY content to zle.
local fd=${_z4h_tty_fd-1}
[[ -t $fd ]] || return
# Note: `read -u $fd` doesn't work.
local resp
IFS= builtin read -srt 5 -d R resp$'?\e[6n' <&$fd || return
while [[ $resp != *$'\e['<->';'<-> ]]; do
IFS= builtin read -srt 5 -d R resp <&$fd || return
done
if (( ARGC )); then
local pref=${resp%$'\e['*}
[[ -n $pref ]] && print -rz -- $pref
fi
resp=${resp##*'['}
typeset -g cursor_y=${resp%';'*}
typeset -g cursor_x=${resp#*';'}