Parent directory

termsupport.zsh

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