async_prompt.zsh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # The async code is taken from
  2. # https://github.com/zsh-users/zsh-autosuggestions/blob/master/src/async.zsh
  3. # https://github.com/woefe/git-prompt.zsh/blob/master/git-prompt.zsh
  4. zmodload zsh/system
  5. # For now, async prompt function handlers are set up like so:
  6. # First, define the async function handler and register the handler
  7. # with _omz_register_handler:
  8. #
  9. # function _git_prompt_status_async {
  10. # # Do some expensive operation that outputs to stdout
  11. # }
  12. # _omz_register_handler _git_prompt_status_async
  13. #
  14. # Then add a stub prompt function in `$PROMPT` or similar prompt variables,
  15. # which will show the output of "$_OMZ_ASYNC_OUTPUT[handler_name]":
  16. #
  17. # function git_prompt_status {
  18. # echo -n $_OMZ_ASYNC_OUTPUT[_git_prompt_status_async]
  19. # }
  20. #
  21. # RPROMPT='$(git_prompt_status)'
  22. #
  23. # This API is subject to change and optimization. Rely on it at your own risk.
  24. function _omz_register_handler {
  25. setopt localoptions noksharrays
  26. typeset -ga _omz_async_functions
  27. # we want to do nothing if there's no $1 function or we already set it up
  28. if [[ -z "$1" ]] || (( ! ${+functions[$1]} )) \
  29. || (( ${_omz_async_functions[(Ie)$1]} )); then
  30. return
  31. fi
  32. _omz_async_functions+=("$1")
  33. # let's add the hook to async_request if it's not there yet
  34. if (( ! ${precmd_functions[(Ie)_omz_async_request]} )) \
  35. && (( ${+functions[_omz_async_request]})); then
  36. autoload -Uz add-zsh-hook
  37. add-zsh-hook precmd _omz_async_request
  38. fi
  39. }
  40. # Set up async handlers and callbacks
  41. function _omz_async_request {
  42. local -i ret=$?
  43. typeset -gA _OMZ_ASYNC_FDS _OMZ_ASYNC_PIDS _OMZ_ASYNC_OUTPUT
  44. # executor runs a subshell for all async requests based on key
  45. local handler
  46. for handler in ${_omz_async_functions}; do
  47. (( ${+functions[$handler]} )) || continue
  48. local fd=${_OMZ_ASYNC_FDS[$handler]:--1}
  49. local pid=${_OMZ_ASYNC_PIDS[$handler]:--1}
  50. # If we've got a pending request, cancel it
  51. if (( fd != -1 && pid != -1 )) && { true <&$fd } 2>/dev/null; then
  52. # Close the file descriptor and remove the handler
  53. exec {fd}<&-
  54. zle -F $fd
  55. # Zsh will make a new process group for the child process only if job
  56. # control is enabled (MONITOR option)
  57. if [[ -o MONITOR ]]; then
  58. # Send the signal to the process group to kill any processes that may
  59. # have been forked by the async function handler
  60. kill -TERM -$pid 2>/dev/null
  61. else
  62. # Kill just the child process since it wasn't placed in a new process
  63. # group. If the async function handler forked any child processes they may
  64. # be orphaned and left behind.
  65. kill -TERM $pid 2>/dev/null
  66. fi
  67. fi
  68. # Define global variables to store the file descriptor, PID and output
  69. _OMZ_ASYNC_FDS[$handler]=-1
  70. _OMZ_ASYNC_PIDS[$handler]=-1
  71. # Fork a process to fetch the git status and open a pipe to read from it
  72. exec {fd}< <(
  73. # Tell parent process our PID
  74. builtin echo ${sysparams[pid]}
  75. # Set exit code for the handler if used
  76. () { return $ret }
  77. # Run the async function handler
  78. $handler
  79. )
  80. # Save FD for handler
  81. _OMZ_ASYNC_FDS[$handler]=$fd
  82. # There's a weird bug here where ^C stops working unless we force a fork
  83. # See https://github.com/zsh-users/zsh-autosuggestions/issues/364
  84. command true
  85. # Save the PID from the handler child process
  86. read -u $fd "_OMZ_ASYNC_PIDS[$handler]"
  87. # When the fd is readable, call the response handler
  88. zle -F "$fd" _omz_async_callback
  89. done
  90. }
  91. # Called when new data is ready to be read from the pipe
  92. function _omz_async_callback() {
  93. emulate -L zsh
  94. local fd=$1 # First arg will be fd ready for reading
  95. local err=$2 # Second arg will be passed in case of error
  96. if [[ -z "$err" || "$err" == "hup" ]]; then
  97. # Get handler name from fd
  98. local handler="${(k)_OMZ_ASYNC_FDS[(r)$fd]}"
  99. # Store old output which is supposed to be already printed
  100. local old_output="${_OMZ_ASYNC_OUTPUT[$handler]}"
  101. # Read output from fd
  102. IFS= read -r -u $fd -d '' "_OMZ_ASYNC_OUTPUT[$handler]"
  103. # Repaint prompt if output has changed
  104. if [[ "$old_output" != "${_OMZ_ASYNC_OUTPUT[$handler]}" ]]; then
  105. zle reset-prompt
  106. zle -R
  107. fi
  108. # Close the fd
  109. exec {fd}<&-
  110. fi
  111. # Always remove the handler
  112. zle -F "$fd"
  113. # Unset global FD variable to prevent closing user created FDs in the precmd hook
  114. _OMZ_ASYNC_FDS[$handler]=-1
  115. _OMZ_ASYNC_PIDS[$handler]=-1
  116. }
  117. autoload -Uz add-zsh-hook
  118. add-zsh-hook precmd _omz_async_request