Skip to content

Instantly share code, notes, and snippets.

@ericbn
Last active October 20, 2020 20:05
Show Gist options
  • Save ericbn/1289d1ad6a1b235a6160f4dc0bdbcde9 to your computer and use it in GitHub Desktop.
Save ericbn/1289d1ad6a1b235a6160f4dc0bdbcde9 to your computer and use it in GitHub Desktop.
getseq () {
trap "stty ${$(stty -g 2>/dev/null):-echo -raw}" 0 1 2 15
stty raw -echo
local k='' seq='' i
for ((i=10; i>0; --i)); do
read -t -k 1 k && break
sleep 1
done
[[ -n $k ]] || return 1
[[ $k = $'\012' || $k = $'\015' || $k = ' ' ]] && return 0
seq=$k
while read -t -k 1 k; do
seq=$seq$k
done
print -Rn ${(V)seq}
}
() {
print "TERM=${TERM}"
print "Wait for prompt before pressing each key."
print "Press SPACE to skip to the next key."
print "Don't press any key within 10 seconds to abort."
read -q "?Ready [y/N]? " || return 1
print
local keys=(
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
Backspace
Delete
Insert
Home
End
PageUp
PageDown
Up
Left
Down
Right
AltLeft
AltRight
ControlLeft
ControlRight
ShiftTab
)
zmodload -F zsh/terminfo +b:echoti +p:terminfo
# Create a zkbd compatible hash
local -A key
key[F1]="${terminfo[kf1]}"
key[F2]="${terminfo[kf2]}"
key[F3]="${terminfo[kf3]}"
key[F4]="${terminfo[kf4]}"
key[F5]="${terminfo[kf5]}"
key[F6]="${terminfo[kf6]}"
key[F7]="${terminfo[kf7]}"
key[F8]="${terminfo[kf8]}"
key[F9]="${terminfo[kf9]}"
key[F10]="${terminfo[kf10]}"
key[F11]="${terminfo[kf11]}"
key[F12]="${terminfo[kf12]}"
key[Backspace]="${terminfo[kbs]}"
key[Delete]="${terminfo[kdch1]}"
key[Insert]="${terminfo[kich1]}"
key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[PageUp]="${terminfo[kpp]}"
key[PageDown]="${terminfo[knp]}"
key[Up]="${terminfo[kcuu1]}"
key[Left]="${terminfo[kcub1]}"
key[Down]="${terminfo[kcud1]}"
key[Right]="${terminfo[kcuf1]}"
key[AltLeft]="${terminfo[kLFT3]}"
key[AltRight]="${terminfo[kRIT3]}"
key[ControlLeft]="${terminfo[kLFT5]}"
key[ControlRight]="${terminfo[kRIT5]}"
key[ShiftTab]="${terminfo[kcbt]}"
if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
echoti smkx
print -PR "${clear_line}%F{green})%f Started application mode"
else
print -PR "${clear_line}%F{red}x Application mode is not supported%f"
fi
{
local clear_line=$'\E[2K\r'
local zkey zreply
for zkey in ${keys}; do
print -Rn "Press ${zkey}"
zreply=$(getseq) || return 1
if [[ -z ${zreply} ]]; then
if [[ -z ${(V)key[${zkey}]} ]]; then
print -PR "${clear_line}%F{yellow}! ${zkey}: not defined, and skipped%f"
else
print -PR "${clear_line}%F{yellow}! ${zkey}: skipped, but expected ${(V)key[${zkey}]}%f"
fi
else
if [[ -z ${(V)key[${zkey}]} ]]; then
print -PR "${clear_line}%F{red}x ${zkey}: not defined, but got ${zreply}%f"
elif [[ ${zreply} == ${(V)key[${zkey}]} ]]; then
print -PR "${clear_line}%F{green})%f ${zkey}: ${zreply}"
else
print -PR "${clear_line}%F{red}x ${zkey}: Got ${zreply}, but expected ${(V)key[${zkey}]}%f"
fi
fi
done
} always {
if (( ${+terminfo[rmkx]} )); then
echoti rmkx
print -PR "${clear_line}%F{green})%f Ended application mode"
fi
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment