-
Notifications
You must be signed in to change notification settings - Fork 32
/
title
executable file
·117 lines (105 loc) · 2.82 KB
/
title
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# terminal titles
precmd()
{
local termtitle
termtitle=`print -P "%n@${HOST}[%l]"`
title zsh "$termtitle"
}
# Written by Chris Page
# Tell the terminal about the working directory whenever it changes.
if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL, including
# the host name to disambiguate local vs. remote paths.
# Percent-encode the pathname.
local URL_PATH=''
{
# Use LANG=C to process text byte-by-byte.
local i ch hexch LANG=C
for ((i = 1; i <= ${#PWD}; ++i)); do
ch="$PWD[i]"
if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
URL_PATH+="$ch"
else
hexch=$(printf "%02X" "'$ch")
URL_PATH+="%$hexch"
fi
done
}
local PWD_URL="file://$HOST$URL_PATH"
printf '\e]7;%s\a' "$PWD_URL"
}
# Register the function so it is called whenever the working
# directory changes.
autoload add-zsh-hook
add-zsh-hook precmd update_terminal_cwd
# Tell the terminal about the initial directory.
update_terminal_cwd
fi
preexec()
{
# With bits from http://zshwiki.org/home/examples/hardstatus
# emulate -L zsh
local -a cmd; cmd=(${(z)1}) # Re-parse the command line
local termtitle
# Prepend this string to the title.
termtitle=`print -P "%n@${HOST}[%l]:"`
case $cmd[1] in
fg)
if (( $#cmd == 1 )); then
# No arguments, must find the current job
# Old approach: cmd=(builtin jobs -l %+)
# weakness: shows a load of bs
title ${jobtexts[${(k)jobstates[(R)*+*]}]%% *} "$termtitle ${jobtexts[${(k)jobstates[(R)*+*]}]}"
elif (( $#cmd == 2 )); then
# Replace the command name, ignore extra args.
# Old approach: cmd=(builtin jobs -l ${(Q)cmd[2]})
# weakness: shows all matching jobs on the title, not just one
title "${jobtexts[${cmd[2]#%}]%% *}" "$termtitle $jobtexts[${cmd[2]#%}]"
else
title "${cmd[2,-1]#%}" "$termtitle ${cmd[2,-1]#%}"
fi
;;
%*)
title "${jobtexts[${cmd[1]#%}]% *}" "$termtitle $jobtexts[${cmd[1]#%}]"
;;
*=*)
shift cmd
;&
exec|sudo)
shift cmd
# If the command is 'exec', drop that, because
# we'd rather just see the command that is being
# exec'd. Note the ;& to fall through the next entry.
;&
*)
title $cmd[1]:t "$termtitle $cmd[*]" # Starting a new job.
;;
esac
}
function title
{
case $TERM in
screen*)
# Use this for GNU Screen:
print -nR $'\ek'$2$'\e'"\\"
;;
interix)
# Use this for interix as "2" makes it upset
shift
print -nR $'\e]0;'$@$'\a'
;;
xterm*|*rxvt*|cygwin|Eterm|mlterm|kterm|aterm|putty*)
if [ "${STY}" ] ; then
print -nR $'\ek'$2$'\e'"\\"
else
# Use this one instead for everybody else:
shift
if [ "$TERM_PROGRAM" != "Apple_Terminal" ]; then
print -nR $'\e]1;'$@$'\a'
fi
print -nR $'\e]2;'$@$'\a'
fi
;;
esac
}