]> 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:

c694eeb54842dc850b3ccd946c95d8f081c70c5f
[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   case $TERM in
17     screen)
18       print -nR $'\033k'$1$'\033'\\ 
19       print -nR $'\033]0;'$2$'\a'
20       ;;
21     xterm|rxvt*)
22       print -nR $'\033]0;'$*$'\a'
23       ;;
24   esac
25 }
26
27 __get_session_flags() {
28   emulate -L zsh
29   [ -n "$SSH_CONNECTION" ] || return
30   local flags
31   [ -n "$DISPLAY" ] && flags+=x
32   [ -n "$SSH_AUTH_SOCK" ] && flags+=a
33   [[ $SSH_CLIENT = *:* ]] && flags+=6
34   [ -n "$flags" ] && echo "[${flags}]"
35 }
36
37 __get_standard_prompt() {
38   emulate -L zsh
39   print -n "[${${(%):-%l}#pts/}]"
40   [[ -z $SSH_CLIENT ]] || print -nP "%m"
41   print -P "%#%25\<..\<%~ $(__get_session_flags)"
42 }
43
44 _set_plain_xterm_title() {
45   emulate -L zsh
46   __set_xterm_title "$(__get_standard_prompt)"
47 }
48 add-zsh-hook precmd _set_plain_xterm_title
49
50 _set_cmd_xterm_title () {
51   emulate -L zsh
52   local -a cmd; cmd=(${(z)1})             # Re-parse the command line
53
54   # Construct a command that will output the desired job number.
55   case $cmd[1] in
56     fg) if (( $#cmd == 1 )); then
57           # No arguments, must find the current job
58           cmd=(builtin jobs -l %+)
59         else
60           # Replace the command name, ignore extra args.
61           cmd=(builtin jobs -l ${(Q)cmd[2]})
62         fi;;
63     %*) cmd=(builtin jobs -l ${(Q)cmd[1]});; # Same as "else" above
64     *) # Not resuming a job,
65       __set_xterm_title $cmd[1]:t${cmd[2]:+ }$cmd[2,-1] "| $(__get_standard_prompt)"
66       return;;                        # so we're all done
67   esac
68
69   local -A jt; jt=(${(kv)jobtexts})       # Copy jobtexts for subshell
70
71   # Run the command, read its output, and look up the jobtext.
72   # Could parse $rest here, but $jobtexts (via $jt) is easier.
73   $cmd 2>/dev/null >>(read num rest; cmd=(${(z)${(e):-\$jt$num}};)
74   __set_xterm_title $leader$cmd[1]:t${cmd[2]:+ }$cmd[2,-1] "| $(__get_standard_prompt)"
75 )
76 }
77 add-zsh-hook preexec _set_cmd_xterm_title
78
79 # vim:ft=zsh