]> git.madduck.net Git - etc/zsh.git/blob - .zsh/zshrc/10-xterm_title

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

declare LOGCHECK global
[etc/zsh.git] / .zsh / zshrc / 10-xterm_title
1 #
2 # Fancy setting of the xterm title
3 #
4 # Copyright © 1994–2017 martin f. krafft <madduck@madduck.net>
5 # Released under the terms of the Artistic Licence 2.0
6 #
7 # Source repository: http://git.madduck.net/v/etc/zsh.git
8 #
9 # Based on http://www.zshwiki.org/cgi-bin/wiki.pl?ZshHardStatus
10 #
11
12 autoload -U add-zsh-hook
13
14 __set_xterm_title() {
15   emulate -L zsh
16   local typ
17   case $TERM in
18     (tmux|tmux-*) typ=tmux;;
19     (screen|screen-*) if [ -n "$TMUX" ]; then typ=tmux else typ=screen fi;;
20     (*) typ=xterm
21   esac
22
23   case "$typ" in
24     (xterm) print -nR $'\033]0;'$*$'\a';;
25     (screen)
26       print -nR $'\033k'$1$'\033'\\
27       print -nR $'\033]0;'$2$'\a'
28       ;;
29     (tmux) print -nR $'\033]2;'$*$'\a';;
30   esac
31 }
32
33 __get_session_flags() {
34   emulate -L zsh
35   [ -n "$SSH_CONNECTION" ] || return
36   local flags
37   [ -n "$DISPLAY" ] && flags+=x
38   [ -n "$SSH_AUTH_SOCK" ] && flags+=a
39   [[ $SSH_CLIENT = *:* ]] && flags+=6
40   [ -n "$flags" ] && echo "[${flags}]"
41 }
42
43 __get_standard_prompt() {
44   emulate -L zsh
45   [[ -z $SSH_CLIENT ]] || print -nP "%m"
46   print -P "@${${(%):-%l}#pts/}%#%25\<..\<%~ $(__get_session_flags)"
47 }
48
49 _set_plain_xterm_title() {
50   emulate -L zsh
51   __set_xterm_title "$(__get_standard_prompt)"
52 }
53 add-zsh-hook precmd _set_plain_xterm_title
54
55 _set_cmd_xterm_title () {
56   emulate -L zsh
57   local -a cmd; cmd=(${(z)1})             # Re-parse the command line
58
59   # Construct a command that will output the desired job number.
60   case $cmd[1] in
61     fg) if (( $#cmd == 1 )); then
62           # No arguments, must find the current job
63           cmd=(builtin jobs -l %+)
64         else
65           # Replace the command name, ignore extra args.
66           cmd=(builtin jobs -l ${(Q)cmd[2]})
67         fi;;
68     %*) cmd=(builtin jobs -l ${(Q)cmd[1]});; # Same as "else" above
69     *) # Not resuming a job,
70       __set_xterm_title $cmd[1]:t${cmd[2]:+ }$cmd[2,-1] "| $(__get_standard_prompt)"
71       return;;                        # so we're all done
72   esac
73
74   local -A jt; jt=(${(kv)jobtexts})       # Copy jobtexts for subshell
75
76   # Run the command, read its output, and look up the jobtext.
77   # Could parse $rest here, but $jobtexts (via $jt) is easier.
78   $cmd 2>/dev/null >>(read num rest; cmd=(${(z)${(e):-\$jt$num}};)
79   __set_xterm_title $leader$cmd[1]:t${cmd[2]:+ }$cmd[2,-1] "| $(__get_standard_prompt)"
80 )
81 }
82 add-zsh-hook preexec _set_cmd_xterm_title
83
84 # vim:ft=zsh