termsupport.zsh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. emulate -L zsh
  11. setopt prompt_subst
  12. [[ "$EMACS" == *term* ]] && 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*)
  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
  30. # If the feature is available set title
  31. if [[ -n "$terminfo[fsl]" ]] && [[ -n "$terminfo[tsl]" ]]; then
  32. echoti tsl
  33. print -Pn "$1"
  34. echoti fsl
  35. fi
  36. fi
  37. ;;
  38. esac
  39. }
  40. ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  41. ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
  42. # Avoid duplication of directory in terminals with independent dir display
  43. if [[ "$TERM_PROGRAM" == Apple_Terminal ]]; then
  44. ZSH_THEME_TERM_TITLE_IDLE="%n@%m"
  45. fi
  46. # Runs before showing the prompt
  47. function omz_termsupport_precmd {
  48. [[ "${DISABLE_AUTO_TITLE:-}" == true ]] && return
  49. title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  50. }
  51. # Runs before executing the command
  52. function omz_termsupport_preexec {
  53. [[ "${DISABLE_AUTO_TITLE:-}" == true ]] && return
  54. emulate -L zsh
  55. setopt extended_glob
  56. # split command into array of arguments
  57. local -a cmdargs
  58. cmdargs=("${(z)2}")
  59. # if running fg, extract the command from the job description
  60. if [[ "${cmdargs[1]}" = fg ]]; then
  61. # get the job id from the first argument passed to the fg command
  62. local job_id jobspec="${cmdargs[2]#%}"
  63. # logic based on jobs arguments:
  64. # http://zsh.sourceforge.net/Doc/Release/Jobs-_0026-Signals.html#Jobs
  65. # https://www.zsh.org/mla/users/2007/msg00704.html
  66. case "$jobspec" in
  67. <->) # %number argument:
  68. # use the same <number> passed as an argument
  69. job_id=${jobspec} ;;
  70. ""|%|+) # empty, %% or %+ argument:
  71. # use the current job, which appears with a + in $jobstates:
  72. # suspended:+:5071=suspended (tty output)
  73. job_id=${(k)jobstates[(r)*:+:*]} ;;
  74. -) # %- argument:
  75. # use the previous job, which appears with a - in $jobstates:
  76. # suspended:-:6493=suspended (signal)
  77. job_id=${(k)jobstates[(r)*:-:*]} ;;
  78. [?]*) # %?string argument:
  79. # use $jobtexts to match for a job whose command *contains* <string>
  80. job_id=${(k)jobtexts[(r)*${(Q)jobspec}*]} ;;
  81. *) # %string argument:
  82. # use $jobtexts to match for a job whose command *starts with* <string>
  83. job_id=${(k)jobtexts[(r)${(Q)jobspec}*]} ;;
  84. esac
  85. # override preexec function arguments with job command
  86. if [[ -n "${jobtexts[$job_id]}" ]]; then
  87. 1="${jobtexts[$job_id]}"
  88. 2="${jobtexts[$job_id]}"
  89. fi
  90. fi
  91. # cmd name only, or if this is sudo or ssh, the next cmd
  92. local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}
  93. local LINE="${2:gs/%/%%}"
  94. title '$CMD' '%100>...>$LINE%<<'
  95. }
  96. autoload -U add-zsh-hook
  97. add-zsh-hook precmd omz_termsupport_precmd
  98. add-zsh-hook preexec omz_termsupport_preexec
  99. # Keep Apple Terminal.app's current working directory updated
  100. # Based on this answer: https://superuser.com/a/315029
  101. # With extra fixes to handle multibyte chars and non-UTF-8 locales
  102. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
  103. # Emits the control sequence to notify Terminal.app of the cwd
  104. # Identifies the directory using a file: URI scheme, including
  105. # the host name to disambiguate local vs. remote paths.
  106. function update_terminalapp_cwd() {
  107. emulate -L zsh
  108. # Percent-encode the host and path names.
  109. local URL_HOST URL_PATH
  110. URL_HOST="$(omz_urlencode -P $HOST)" || return 1
  111. URL_PATH="$(omz_urlencode -P $PWD)" || return 1
  112. # Undocumented Terminal.app-specific control sequence
  113. printf '\e]7;%s\a' "file://$URL_HOST$URL_PATH"
  114. }
  115. # Use a precmd hook instead of a chpwd hook to avoid contaminating output
  116. add-zsh-hook precmd update_terminalapp_cwd
  117. # Run once to get initial cwd set
  118. update_terminalapp_cwd
  119. fi