forked from MattiasPernhult/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpprompt
executable file
·85 lines (70 loc) · 1.83 KB
/
mpprompt
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
#!/usr/bin/env bash
_escape() {
echo -nE "${1//\\/\\\\}"
}
_mp_git_branch() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
local branch
if branch="$(\git symbolic-ref -q HEAD)"; then
_escape "${branch#refs/heads/}"
else
\git rev-parse --short -q HEAD
fi
}
_k8s_context() {
local kubeconfig_context
kubeconfig_context="$(kubectl config current-context | rev | cut -d '_' -f1 | rev | cut -d'/' -f2)"
local ak_context
ak_context="none"
if [[ -n "${AK_URL}" ]]; then
if [[ "${AK_URL}" == *"kind" ]]; then
ak_context="$(cut -d'/' -f3 <<<"${AK_URL}")"
elif [[ "${AK_URL}" == "file://"* ]]; then
ak_context="$(akutil context current | cut -d'/' -f5)"
else
ak_context="$(echo "${AK_URL}" | cut -d'/' -f5 | cut -d'?' -f1)"
fi
fi
echo "${kubeconfig_context}/${ak_context}"
}
_set_prompt() {
local -i err=$?
_blue_color="\[\e[34m\]"
_dark_green_color="\[\e[92m\]"
_green_color="\[\e[32m\]"
_red_color="\[\e[91m\]"
_magenta_color="\[\e[95m\]"
_bold_text="\[\e[1m\]"
_dim_text="\[\e[2m\]"
_reset="\[\e[0m\]"
if [[ "${err}" != "0" ]]; then
MP_ERR=" ${_bold_text}${_red_color}${err}${_reset}"
else
MP_ERR=""
fi
mp_git="$(_mp_git_branch)"
if [[ -n "${mp_git}" ]]; then
MP_GIT=" ${_green_color}(${mp_git})${_reset}"
else
MP_GIT=""
fi
local k8s_context
k8s_context="$(_k8s_context)"
if [[ -n "${k8s_context}" ]]; then
MP_K8S_CONTEXT=" ${_magenta_color}{${k8s_context}}${_reset}"
else
MP_K8S_CONTEXT=""
fi
MP_PREFIX="${_dark_green_color}➜${_reset}"
MP_PWD=" ${_blue_color}[\W]${_reset}"
local mp="${MP_PREFIX}${MP_ERR}${MP_K8S_CONTEXT}${MP_GIT}${MP_PWD} "
if [[ "${MP_PS1:-}" != "${mp}" ]]; then
MP_PS1="${mp}"
shopt -u promptvars
PS1="${MP_PS1}"
fi
}
_main() {
PROMPT_COMMAND=_set_prompt
}
_main