termsupport.zsh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. [[ "$EMACS" == *term* ]] && return
  11. # if $2 is unset use $1 as default
  12. # if it is set and empty, leave it as is
  13. : ${2=$1}
  14. if [[ "$TERM" == screen* ]]; then
  15. print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars
  16. elif [[ "$TERM" == xterm* ]] || [[ "$TERM" == rxvt* ]] || [[ "$TERM" == ansi ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  17. print -Pn "\e]2;$2:q\a" #set window name
  18. print -Pn "\e]1;$1:q\a" #set icon (=tab) name
  19. fi
  20. }
  21. ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  22. ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
  23. # Runs before showing the prompt
  24. function omz_termsupport_precmd {
  25. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  26. return
  27. fi
  28. title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  29. }
  30. # Runs before executing the command
  31. function omz_termsupport_preexec {
  32. if [[ $DISABLE_AUTO_TITLE == true ]]; then
  33. return
  34. fi
  35. emulate -L zsh
  36. setopt extended_glob
  37. # cmd name only, or if this is sudo or ssh, the next cmd
  38. local CMD=${1[(wr)^(*=*|sudo|ssh|rake|-*)]:gs/%/%%}
  39. local LINE="${2:gs/%/%%}"
  40. title '$CMD' '%100>...>$LINE%<<'
  41. }
  42. precmd_functions+=(omz_termsupport_precmd)
  43. preexec_functions+=(omz_termsupport_preexec)
  44. # Keep Apple Terminal.app's current working directory updated
  45. # Based on this answer: http://superuser.com/a/315029
  46. # With extra fixes to handle multibyte chars and non-UTF-8 locales
  47. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
  48. # URL-encodes a string
  49. # Outputs the encoded string on stdout
  50. # Returns nonzero if encoding failed
  51. function _omz_urlencode() {
  52. local url_str=''
  53. {
  54. local str=$1
  55. # URLs must use UTF-8 encoding; convert if required
  56. local encoding=${LC_CTYPE/*./}
  57. if [[ $encoding != UTF-8 ]]; then
  58. str=$(iconv -f $encoding -t UTF-8)
  59. if [[ $? != 0 ]]; then
  60. echo "Error converting string from $encoding to UTF-8" >&2
  61. return 1
  62. fi
  63. fi
  64. # Use LC_CTYPE=C to process text byte-by-byte
  65. local i ch hexch LC_CTYPE=C
  66. for ((i = 1; i <= ${#str}; ++i)); do
  67. ch="$str[i]"
  68. if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
  69. url_str+="$ch"
  70. else
  71. hexch=$(printf "%02X" "'$ch")
  72. url_str+="%$hexch"
  73. fi
  74. done
  75. echo $url_str
  76. }
  77. }
  78. # Emits the control sequence to notify Terminal.app of the cwd
  79. function update_terminalapp_cwd() {
  80. # Identify the directory using a "file:" scheme URL, including
  81. # the host name to disambiguate local vs. remote paths.
  82. # Percent-encode the pathname.
  83. local URL_PATH=$(_omz_urlencode $PWD)
  84. [[ $? != 0 ]] && return 1
  85. local PWD_URL="file://$HOST$URL_PATH"
  86. # Undocumented Terminal.app-specific control sequence
  87. printf '\e]7;%s\a' $PWD_URL
  88. }
  89. # Use a precmd hook instead of a chpwd hook to avoid contaminating output
  90. precmd_functions+=(update_terminalapp_cwd)
  91. # Run once to get initial cwd set
  92. update_terminalapp_cwd
  93. fi