Last active
April 1, 2023 07:46
-
-
Save psprint/b6503c0c37de483f68d055e9c47981b3 to your computer and use it in GitHub Desktop.
Zsh function to display a message under prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The Zshell function will display the given message under-prompt (as a kind of notification). | |
# It has an optional delay first argument: "@sleep:<secnods with fractions>". If given, then | |
# the message will wait in background before showing for the specified time. | |
# | |
# Usage: | |
# deploy-message @msg "Hello world" | |
# deploy-message "Hello world" | |
# deploy-message @msg 1234 # I.e. the "@msg" is needed for number-only messages, otherwise optional | |
# deploy-message @sleep:5.5 "Hello world" | |
deploy-message() { | |
[[ "$1" = <-> && ( ${#} = 1 || ( "$2" = (hup|nval|err) && ${#} = 2 ) ) ]] && { zle && { | |
local alltext text IFS=$'\n' nl=$'\n' | |
repeat 25; do read -r -u"$1" text; alltext+="${text:+$text$nl}"; done | |
[[ -n "$alltext" ]] && zle -M "$alltext" | |
} | |
zle -F "$1"; exec {1}<&- | |
return 0 | |
} | |
local THEFD hasw | |
# The expansion is: if there is @sleep: pfx, then use what's after | |
# it, otherwise substitute 0 | |
exec {THEFD} < <(LANG=C sleep $(( 0.01 + ${${${(M)1#@sleep:}:+${1#@sleep:}}:-0} )); print -r -- ${1:#(@msg|@sleep:*)} "${@[2,-1]}") | |
zle -F "$THEFD" deploy-message | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An important update on 18.09.2019 – it was possible for the the function to not work, especially on Linux (OS X seems resistant, I've never occurred the described situation under it), due to the arguments passed to the zle -F callback by Zsh to be possible to be of count 2, not only 1.