async_prompt.zsh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 add the function name
  7. # to the _omz_async_functions array:
  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]
  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. typeset -gA _OMZ_ASYNC_FDS _OMZ_ASYNC_PIDS _OMZ_ASYNC_OUTPUT
  43. # executor runs a subshell for all async requests based on key
  44. local handler
  45. for handler in ${_omz_async_functions}; do
  46. (( ${+functions[$handler]} )) || continue
  47. local fd=${_OMZ_ASYNC_FDS[$handler]:--1}
  48. local pid=${_OMZ_ASYNC_PIDS[$handler]:--1}
  49. # If we've got a pending request, cancel it
  50. if (( fd != -1 && pid != -1 )) && { true <&$fd } 2>/dev/null; then
  51. # Close the file descriptor and remove the handler
  52. exec {fd}<&-
  53. zle -F $fd
  54. # Zsh will make a new process group for the child process only if job
  55. # control is enabled (MONITOR option)
  56. if [[ -o MONITOR ]]; then
  57. # Send the signal to the process group to kill any processes that may
  58. # have been forked by the async function handler
  59. kill -TERM -$pid 2>/dev/null
  60. else
  61. # Kill just the child process since it wasn't placed in a new process
  62. # group. If the async function handler forked any child processes they may
  63. # be orphaned and left behind.
  64. kill -TERM $pid 2>/dev/null
  65. fi
  66. fi
  67. # Define global variables to store the file descriptor, PID and output
  68. _OMZ_ASYNC_FDS[$handler]=-1
  69. _OMZ_ASYNC_PIDS[$handler]=-1
  70. # Fork a process to fetch the git status and open a pipe to read from it
  71. exec {fd}< <(
  72. # Tell parent process our PID
  73. builtin echo ${sysparams[pid]}
  74. # Store handler name for callback
  75. builtin echo $handler
  76. # Run the async function handler
  77. $handler
  78. )
  79. # Save FD for handler
  80. _OMZ_ASYNC_FDS[$handler]=$fd
  81. # There's a weird bug here where ^C stops working unless we force a fork
  82. # See https://github.com/zsh-users/zsh-autosuggestions/issues/364
  83. command true
  84. # Save the PID from the handler child process
  85. read pid <&$fd
  86. _OMZ_ASYNC_PIDS[$handler]=$pid
  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 first line
  98. local handler
  99. read handler <&$fd
  100. # Store old output which is supposed to be already printed
  101. local old_output="${_OMZ_ASYNC_OUTPUT[$handler]}"
  102. # Read output from fd
  103. _OMZ_ASYNC_OUTPUT[$handler]="$(cat <&$fd)"
  104. # Repaint prompt if output has changed
  105. if [[ "$old_output" != "${_OMZ_ASYNC_OUTPUT[$handler]}" ]]; then
  106. zle reset-prompt
  107. zle -R
  108. fi
  109. # Close the fd
  110. exec {fd}<&-
  111. fi
  112. # Always remove the handler
  113. zle -F "$fd"
  114. # Unset global FD variable to prevent closing user created FDs in the precmd hook
  115. _OMZ_ASYNC_FDS[$handler]=-1
  116. _OMZ_ASYNC_PIDS[$handler]=-1
  117. }