-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdockolor.plugin.zsh
More file actions
112 lines (103 loc) · 2.4 KB
/
dockolor.plugin.zsh
File metadata and controls
112 lines (103 loc) · 2.4 KB
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
# utils to check if awk is installed
dockolor_has_awk() {
if command -v awk >/dev/null 2>&1; then
echo true
else
echo false
fi
}
# todo: merge both cmd
dockolor_colorize() {
# check if the user want json format or table
if [[ "$*" == *"--format json"* ]]; then
# we got json so we need to check for eg State":"exited" in each line and colorize in function
command docker "$@" | awk '
BEGIN {
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
RESET = "\033[0m"
}
# set the color in fc of the status
{
line = $0
lower = tolower(line)
if (lower ~ /"state":"up"/ || lower ~ /"state":"running"/) {
color = GREEN
} else if (lower ~ /"state":"paused"/) {
color = YELLOW
} else if (lower ~ /"state":"exited"/ || lower ~ /"state":"dead"/) {
color = RED
} else {
color = RESET
}
printf "%s%s%s\n", color, line, RESET
}'
else
command docker "$@" | awk '
BEGIN {
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
RESET = "\033[0m"
}
# skip the first line because it containt header
# reminder, NR > built-in var in awk
NR == 1 {
print $0
next
}
# set the color in fc of the status
{
line = $0
lower = tolower(line)
# paused has to be check first since the string is likely up since x (paused)
if (lower ~ /paused/) {
color = YELLOW
} else if (lower ~ /up/ || lower ~ /running/) {
color = GREEN
} else if (lower ~ /exited/ || lower ~ /dead/) {
color = RED
} else {
color = RESET
}
printf "%s%s%s\n", color, line, RESET
}'
fi
}
# main function
# check awk and colorize
dockolor() {
if [ "$(dockolor_has_awk)" = false ]; then
echo "You have installed dockolor but you don't have awk installed"
docker "$@"
else
dockolor_colorize "$@"
fi
}
# ### ###
# remap each alias or cmd
# ### ###
# if the user have the docker plugin loaded
# we remove the alias
if alias dps >/dev/null 2>&1; then
unalias dps
fi
if alias dpsa >/dev/null 2>&1; then
unalias dpsa
fi
dps() {
dockolor ps "$@"
}
dpsa() {
dockolor ps -a "$@"
}
# intercept docker command to colorize ps output
# or pass through
docker() {
if [[ "$1" == "ps" || "$1" == "container" && "$2" == "ps" ]]; then
dockolor "$@"
else
command docker "$@"
fi
}