-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathiterm-tab-color.plugin.zsh
67 lines (57 loc) · 1.74 KB
/
iterm-tab-color.plugin.zsh
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
tcConfigFilePath="$(dirname "$0")/.tc-config"
declare -A tcConfigColors
declare -a orderedConfig
while IFS="=" read -r configKey hexValue || [ -n "$hexValue" ]; do
if ! ( [[ $configKey == \#* ]]); then
orderedConfig+=( $configKey )
tcConfigColors[$configKey]+=$hexValue
fi
done < $tcConfigFilePath
function directory_tab_color() {
try_set_tab_color "$PWD"
}
function command_tab_color() {
try_set_tab_color "$1"
}
function try_set_tab_color() {
for k in $orderedConfig; do
if ( [[ "$1" =~ "$k" ]] ); then
iterm_tab_color "$tcConfigColors[$k]"
return 0
fi
iterm_tab_color
done
}
function iterm_tab_color() {
if [ $# -eq 0 ]; then
# Reset tab color if called with no arguments
echo -ne "\033]6;1;bg;*;default\a"
return 0
elif [ $# -eq 1 ]; then
if ( [[ $1 == \#* ]] ); then
# If single argument starts with '#', skip first character to find hex value
RED_HEX=${1:1:2}
GREEN_HEX=${1:3:2}
BLUE_HEX=${1:5:2}
else
# If single argument doesn't start with '#', assume it's hex value
RED_HEX=${1:0:2}
GREEN_HEX=${1:2:2}
BLUE_HEX=${1:4:2}
fi
RED=$(( 16#${RED_HEX} ))
GREEN=$(( 16#${GREEN_HEX} ))
BLUE=$(( 16#${BLUE_HEX} ))
echo -ne "\033]6;1;bg;red;brightness;$RED\a"
echo -ne "\033]6;1;bg;green;brightness;$GREEN\a"
echo -ne "\033]6;1;bg;blue;brightness;$BLUE\a"
return 0
fi
# If more than 1 argument, assume 3 arguments were passed
echo -ne "\033]6;1;bg;red;brightness;$1\a"
echo -ne "\033]6;1;bg;green;brightness;$2\a"
echo -ne "\033]6;1;bg;blue;brightness;$3\a"
}
alias tc='iterm_tab_color'
preexec_functions=(${preexec_functions[@]} "command_tab_color")
precmd_functions=(${precmd_functions[@]} "directory_tab_color")