Last active
October 12, 2020 16:24
-
-
Save mnowotnik/7c08f1aaef97a9f6ae64da4ad5b64125 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# This snippet of code is released into the public domain. | |
# However, an attribution link is welcome. | |
# Run a job in the foreground after TIMEOUT seconds | |
# to make prompt rendering in a new session faster and more responsive. | |
# for example: | |
# pyenv_loader() { | |
# unset -f pyenv_load | |
# [[ -z "$PYENV_HOME" ]] && export PYENV_HOME="$HOME/.pyenv" | |
# export PATH="$PYENV_HOME/bin:$PATH" | |
# eval "$(pyenv init -)" | |
# } | |
# run_after_seconds pyenv_loader 0.5 | |
run_after_seconds() { | |
if [[ $# != 2 ]]; then | |
echo run_after_seconds FUNC TIMEOUT | |
return | |
fi | |
local func=$1 | |
local timeout=$2 | |
local int FD | |
wrapper() { | |
local fd=$1 | |
local func | |
# read -r -u $fd func | |
# or | |
func="$(<&$fd)" | |
$func | |
zle -F $fd | |
exec {fd}<&- | |
} | |
exec {FD}< <(sleep $timeout; printf "%s" "$func";) | |
zle -F $FD wrapper | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment