async_prompt.zsh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. # Store handler name for callback
  76. builtin echo $handler
  77. # Set exit code for the handler if used
  78. (exit $ret)
  79. # Run the async function handler
  80. $handler
  81. )
  82. # Save FD for handler
  83. _OMZ_ASYNC_FDS[$handler]=$fd
  84. # There's a weird bug here where ^C stops working unless we force a fork
  85. # See https://github.com/zsh-users/zsh-autosuggestions/issues/364
  86. command true
  87. # Save the PID from the handler child process
  88. read pid <&$fd
  89. _OMZ_ASYNC_PIDS[$handler]=$pid
  90. # When the fd is readable, call the response handler
  91. zle -F "$fd" _omz_async_callback
  92. done
  93. }
  94. # Called when new data is ready to be read from the pipe
  95. function _omz_async_callback() {
  96. emulate -L zsh
  97. local fd=$1 # First arg will be fd ready for reading
  98. local err=$2 # Second arg will be passed in case of error
  99. if [[ -z "$err" || "$err" == "hup" ]]; then
  100. # Get handler name from first line
  101. local handler
  102. read handler <&$fd
  103. # Store old output which is supposed to be already printed
  104. local old_output="${_OMZ_ASYNC_OUTPUT[$handler]}"
  105. # Read output from fd
  106. _OMZ_ASYNC_OUTPUT[$handler]="$(cat <&$fd)"
  107. # Repaint prompt if output has changed
  108. if [[ "$old_output" != "${_OMZ_ASYNC_OUTPUT[$handler]}" ]]; then
  109. zle reset-prompt
  110. zle -R
  111. fi
  112. # Close the fd
  113. exec {fd}<&-
  114. fi
  115. # Always remove the handler
  116. zle -F "$fd"
  117. # Unset global FD variable to prevent closing user created FDs in the precmd hook
  118. _OMZ_ASYNC_FDS[$handler]=-1
  119. _OMZ_ASYNC_PIDS[$handler]=-1
  120. }
  121. autoload -Uz add-zsh-hook
  122. add-zsh-hook precmd _omz_async_request