termsupport.zsh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Set terminal window and tab/icon title
  2. #
  3. # usage: title short_tab_title [long_window_title]
  4. #
  5. # See: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#ss3.1
  6. # Fully supports screen, iterm, and probably most modern xterm and rxvt
  7. # (In screen, only short_tab_title is used)
  8. # Limited support for Apple Terminal (Terminal can't set window and tab separately)
  9. function title {
  10. setopt localoptions nopromptsubst
  11. # Don't set the title if inside emacs, unless using vterm
  12. [[ -n "${INSIDE_EMACS:-}" && "$INSIDE_EMACS" != vterm ]] && return
  13. # if $2 is unset use $1 as default
  14. # if it is set and empty, leave it as is
  15. : ${2=$1}
  16. case "$TERM" in
  17. cygwin|xterm*|putty*|rxvt*|konsole*|ansi|mlterm*|alacritty|st*|foot|contour*)
  18. print -Pn "\e]2;${2:q}\a" # set window name
  19. print -Pn "\e]1;${1:q}\a" # set tab name
  20. ;;
  21. screen*|tmux*)
  22. print -Pn "\ek${1:q}\e\\" # set screen hardstatus
  23. ;;
  24. *)
  25. if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  26. print -Pn "\e]2;${2:q}\a" # set window name
  27. print -Pn "\e]1;${1:q}\a" # set tab name
  28. else
  29. # Try to use terminfo to set the title if the feature is available
  30. if (( ${+terminfo[fsl]} && ${+terminfo[tsl]} )); then
  31. print -Pn "${terminfo[tsl]}$1${terminfo[fsl]}"
  32. fi
  33. fi
  34. ;;
  35. esac
  36. }
  37. ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  38. ZSH_THEME_TERM_TITLE_IDLE="%n@%m:%~"
  39. # Avoid duplication of directory in terminals with independent dir display
  40. if [[ "$TERM_PROGRAM" == Apple_Terminal ]]; then
  41. ZSH_THEME_TERM_TITLE_IDLE="%n@%m"
  42. fi
  43. # Runs before showing the prompt
  44. function omz_termsupport_precmd {
  45. [[ "${DISABLE_AUTO_TITLE:-}" != true ]] || return
  46. title "$ZSH_THEME_TERM_TAB_TITLE_IDLE" "$ZSH_THEME_TERM_TITLE_IDLE"
  47. }
  48. # Runs before executing the command
  49. function omz_termsupport_preexec {
  50. [[ "${DISABLE_AUTO_TITLE:-}" != true ]] || return
  51. emulate -L zsh
  52. setopt extended_glob
  53. # split command into array of arguments
  54. local -a cmdargs
  55. cmdargs=("${(z)2}")
  56. # if running fg, extract the command from the job description
  57. if [[ "${cmdargs[1]}" = fg ]]; then
  58. # get the job id from the first argument passed to the fg command
  59. local job_id jobspec="${cmdargs[2]#%}"
  60. # logic based on jobs arguments:
  61. # http://zsh.sourceforge.net/Doc/Release/Jobs-_0026-Signals.html#Jobs
  62. # https://www.zsh.org/mla/users/2007/msg00704.html
  63. case "$jobspec" in
  64. <->) # %number argument:
  65. # use the same <number> passed as an argument
  66. job_id=${jobspec} ;;
  67. ""|%|+) # empty, %% or %+ argument:
  68. # use the current job, which appears with a + in $jobstates:
  69. # suspended:+:5071=suspended (tty output)
  70. job_id=${(k)jobstates[(r)*:+:*]} ;;
  71. -) # %- argument:
  72. # use the previous job, which appears with a - in $jobstates:
  73. # suspended:-:6493=suspended (signal)
  74. job_id=${(k)jobstates[(r)*:-:*]} ;;
  75. [?]*) # %?string argument:
  76. # use $jobtexts to match for a job whose command *contains* <string>
  77. job_id=${(k)jobtexts[(r)*${(Q)jobspec}*]} ;;
  78. *) # %string argument:
  79. # use $jobtexts to match for a job whose command *starts with* <string>
  80. job_id=${(k)jobtexts[(r)${(Q)jobspec}*]} ;;
  81. esac
  82. # override preexec function arguments with job command
  83. if [[ -n "${jobtexts[$job_id]}" ]]; then
  84. 1="${jobtexts[$job_id]}"
  85. 2="${jobtexts[$job_id]}"
  86. fi
  87. fi
  88. # cmd name only, or if this is sudo or ssh, the next cmd
  89. local CMD="${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}"
  90. local LINE="${2:gs/%/%%}"
  91. title "$CMD" "%100>...>${LINE}%<<"
  92. }
  93. autoload -Uz add-zsh-hook
  94. if [[ -z "$INSIDE_EMACS" || "$INSIDE_EMACS" = vterm ]]; then
  95. add-zsh-hook precmd omz_termsupport_precmd
  96. add-zsh-hook preexec omz_termsupport_preexec
  97. fi
  98. # Keep terminal emulator's current working directory correct,
  99. # even if the current working directory path contains symbolic links
  100. #
  101. # References:
  102. # - Apple's Terminal.app: https://superuser.com/a/315029
  103. # - iTerm2: https://iterm2.com/documentation-escape-codes.html (iTerm2 Extension / CurrentDir+RemoteHost)
  104. # - Konsole: https://bugs.kde.org/show_bug.cgi?id=327720#c1
  105. # - libvte (gnome-terminal, mate-terminal, …): https://bugzilla.gnome.org/show_bug.cgi?id=675987#c14
  106. # Apparently it had a bug before ~2012 were it would display the unknown OSC 7 code
  107. #
  108. # As of May 2021 mlterm, PuTTY, rxvt, screen, termux & xterm simply ignore the unknown OSC.
  109. # Don't define the function if we're inside Emacs or in an SSH session (#11696)
  110. if [[ -n "$INSIDE_EMACS" || -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then
  111. return
  112. fi
  113. # Don't define the function if we're in an unsupported terminal
  114. case "$TERM" in
  115. # all of these either process OSC 7 correctly or ignore entirely
  116. xterm*|putty*|rxvt*|konsole*|mlterm*|alacritty|screen*|tmux*) ;;
  117. contour*|foot*) ;;
  118. *)
  119. # Terminal.app and iTerm2 process OSC 7 correctly
  120. case "$TERM_PROGRAM" in
  121. Apple_Terminal|iTerm.app) ;;
  122. *) return ;;
  123. esac ;;
  124. esac
  125. # Emits the control sequence to notify many terminal emulators
  126. # of the cwd
  127. #
  128. # Identifies the directory using a file: URI scheme, including
  129. # the host name to disambiguate local vs. remote paths.
  130. function omz_termsupport_cwd {
  131. # Percent-encode the host and path names.
  132. local URL_HOST URL_PATH
  133. URL_HOST="$(omz_urlencode -P $HOST)" || return 1
  134. URL_PATH="$(omz_urlencode -P $PWD)" || return 1
  135. # Konsole errors if the HOST is provided
  136. [[ -z "$KONSOLE_VERSION" ]] || URL_HOST=""
  137. # common control sequence (OSC 7) to set current host and path
  138. printf "\e]7;file://%s%s\e\\" "${URL_HOST}" "${URL_PATH}"
  139. }
  140. # Use a precmd hook instead of a chpwd hook to avoid contaminating output
  141. # i.e. when a script or function changes directory without `cd -q`, chpwd
  142. # will be called the output may be swallowed by the script or function.
  143. add-zsh-hook precmd omz_termsupport_cwd