-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bashprompt.rc
175 lines (150 loc) · 5.78 KB
/
.bashprompt.rc
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Adding last command execution time
# source: https://jakemccrary.com/blog/2015/05/03/put-the-last-commands-run-time-in-your-bash-prompt/
# https://stackoverflow.com/a/34812608
timer_now() {
date +%s%N
}
timer_start() {
timer_start=${timer_start:-$(timer_now)}
}
timer_stop() {
local delta_us=$((($(timer_now) - $timer_start) / 1000))
local us=$((delta_us % 1000))
local ms=$(((delta_us / 1000) % 1000))
local s=$(((delta_us / 1000000) % 60))
local m=$(((delta_us / 60000000) % 60))
local h=$((delta_us / 3600000000))
# Goal: always show around 3 digits of accuracy
if ((h > 0)); then timer_show=${h}h${m}m
elif ((m > 0)); then timer_show=${m}m${s}s
elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
elif ((ms >= 100)); then timer_show=${ms}ms
elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
else timer_show=${us}us
fi
unset timer_start
}
trap 'timer_start' DEBUG
PROMPT_COMMAND=__prompt_command
# Kubernetes prompt script I found. Commented it out for now.
# source $HOME/.dotfiles/lib/kube-ps1.sh
function __git_color {
local git_status="$(git status --porcelain 2> /dev/null | wc -l)"
if (( $git_status > 0)); then
echo -e "\e[38;5;220m"
else
echo -e "\e[38;5;76m"
fi
}
function __git_branch {
local git_status="$(git status 2> /dev/null)"
local on_branch="On branch ([^${IFS}]*)"
local on_commit="HEAD detached at ([^${IFS}]*)"
if [[ $git_status =~ $on_branch ]]; then
local branch=${BASH_REMATCH[1]}
echo "$GreenBG🌳$(__git_color)$branch "
elif [[ $git_status =~ $on_commit ]]; then
local commit=${BASH_REMATCH[1]}
echo "$GreenBG📜 $(__git_color)$commit "
fi
}
function __serverLocation {
# Work out where we are running
where="Some Linux system 🤷"
if [[ ! -z $WSL_DISTRO_NAME ]] && [[ ! "$(uname -r)" =~ "WSL2" ]]; then where="\e[38;5;26mWSLv1💙";
elif [[ ! -z $WSL_INTEROP ]] && [[ "$(uname -r)" =~ "WSL2" ]]; then where="\e[38;5;46mWSLv2💚";
elif [[ -f /.dockerenv ]]; then where="\e[38;5;45mDocker container📦";
elif [[ ! -z $REMOTE_CONTAINERS_IPC ]]; then where="Devcontainer💻";
elif [[ ! -z $ACC_TERM_ID ]]; then where="Azure Cloud Shell ($ACC_LOCATION)☁";
elif [[ $CODESPACES == "true" ]]; then where="GitHub Codespaces🐙";
elif [[ "$(lsb_release -i)" =~ "Raspbian" ]]; then where="Raspberry Pi🍇";
elif [[ "$(lsb_release -i)" =~ "Ubuntu" ]]; then where="\e[38;5;209mUbuntu🐧"; fi
echo $where
}
function __azure_sub {
# Don't use az account show as it's slow
# We look directly into the cached config in ~/.azure
# regex='=\s(.*?)'
# if [[ -f $(which jq) && -f "$HOME/.azure/clouds.config" && $(cat $HOME/.azure/clouds.config) =~ $regex ]]; then
# azsub=$(cat $HOME/.azure/azureProfile.json | jq -r ".subscriptions[] | select(.id==\"${BASH_REMATCH[1]}\").name")
# echo "$SEP\e[38;5;15m⛈🌦 \e[38;5;87m$DEV_ENVIRONMENT"
# fi
if [[ ! -z $DEV_ENVIRONMENT ]] && [[ ! "$DEV_ENVIRONMENT" =~ "Gov" ]]; then echo " $BlueBG\e[38;5;15m☁️\e[38;5;87m$DEV_ENVIRONMENT";
elif [[ ! -z $DEV_ENVIRONMENT ]] && [[ "$DEV_ENVIRONMENT" =~ "Gov" ]]; then echo " $RedBG\e[38;5;15m☁️\e[38;5;87m$DEV_ENVIRONMENT";
fi
}
function __docker_context {
if [[ ! -z $DOCKER_CONTEXT ]]; then echo " $LightBlueBG🐋\e[38;5;226m$DOCKER_CONTEXT"; fi
}
SEP="\e[92m┃"
# Show timestamp on the right hand side
# source: https://superuser.com/questions/187455/right-align-part-of-prompt/1203400#1203400
# Create a string like: "[ 16:06:06 ]" with time in RED.
# Strip ANSI commands before counting length
# From: https://www.commandlinefu.com/commands/view/12043/remove-color-special-escape-ansi-codes-from-text-with-sed
__prompt_command()
{
local e="$?"
Bold='\e[1m'
GreyBG='\e[48;5;237m'
PurpleBG='\e[48;5;61m'
GreenBG='\e[48;5;29m'
BlueBG='\e[48;5;4m'
LightBlueBG='\e[48;5;39m'
RedBG='\e[48;5;9m'
# Green='\[\e[00;22m\]'
Green='\e[38;5;46m'
# White='\[\e[01;37m\]'
White='\e[38;5;15m'
# Cyan='\[\e[00;36m\]'
Cyan='\e[38;5;36m'
# Yellow='\[\e[00;33m\]'
Yellow='\e[38;5;220m'
# LightBlue='\[\e[00;94m\]'
LightBlue='\e[38;5;45m'
Blue='\e[38;5;26m'
User='\e[38;5;75m'
Reset='\e[0m'
# printf -v PS1RHS "\e[0m[ \e[1;31m%(%H:%M:%S)T \e[0m]" -1 # -1 is current time
printf -v PS1RHS "\e[48;5;237m\e[38;5;255m[ \e[38;5;9m%(%H:%M:%S)T \e[38;5;255m]" -1 # -1 is current time
PS1RHS_stripped=$(sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" <<<"$PS1RHS")
# Reference: https://en.wikipedia.org/wiki/ANSI_escape_code
Save='\e[s' # Save cursor position
Rest='\e[u' # Restore cursor to save point
# Add the elapsed time and current date
timer_stop
# Save cursor position, jump to right hand edge, then go left N columns where
# N is the length of the printable RHS string. Print the RHS string, then
# return to the saved position and print the LHS prompt.
# Note: "\[" and "\]" are used so that bash can calculate the number of
# printed characters so that the prompt doesn't do strange things when
# editing the entered text.
PS1="\[${Save}\e[${COLUMNS:-$(tput cols)}C\e[${#PS1RHS_stripped}D${PS1RHS}${Rest}\]"
# show last command execution time
PS1+="$GreyBG$LightBlue[⌚️${timer_show}]"
# show user@host
PS1+="$Blue $(__serverLocation) $User\u$White@$Cyan\h"
# show current directory
PS1+=" $PurpleBG📂$Yellow\W"
# show docker context
PS1+="$(__docker_context)"
# If wide enough show Azure subscription
if (( $(tput cols) > 80 )); then
PS1+="$(__azure_sub) "
if [ -f "$HOME/.kube/config" ]; then
PS1+="$(kube_ps1)"
fi
fi
# Add git details
PS1+="$(__git_branch)\e[0m\e[38;5;22m"
# PS1+="$(__git_branch)\[\e[0m\]\[\e[38;5;22m\]"
# Status code
if [ $e != 0 ]; then
PS1+="\[\e[0m\]\[\e[38;5;197m\] ${e} 💩 "
else
PS1+="\[\e[0m\]\[\e[38;5;76m\]"
fi
#PS1+="\n⮞ $Reset"
PS1+="$Reset \n$ "
}