-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_prompt
125 lines (111 loc) · 3.58 KB
/
.bash_prompt
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
#!/bin/bash
# Function to display Git repository status in prompt
prompt_git() {
local s='' branchName=''
# Check if the current directory is in a Git repository
if [ "$(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}")" == '0' ] && [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
# Update Git index if necessary
[[ -O "$(git rev-parse --show-toplevel)/.git/index" ]] && git update-index --really-refresh -q &> /dev/null
# Git status checks
s+=$(check_uncommitted_changes)
s+=$(check_unstaged_changes)
s+=$(check_untracked_files)
s+=$(check_stashed_files)
# Get the current branch name or commit hash
branchName=$(get_git_branch)
[ -n "${s}" ] && s=" [${s}]"
echo -e "${1}${branchName}${blue}${s}"
else
return
fi
}
# Additional git functions
check_uncommitted_changes() { ! git diff --quiet --ignore-submodules --cached && echo '+'; }
check_unstaged_changes() { ! git diff-files --quiet --ignore-submodules -- && echo '!'; }
check_untracked_files() { [ -n "$(git ls-files --others --exclude-standard)" ] && echo '?'; }
check_stashed_files() { git rev-parse --verify refs/stash &>/dev/null && echo '$'; }
get_git_branch() { git symbolic-ref --quiet --short HEAD 2> /dev/null || git rev-parse --short HEAD 2> /dev/null || echo '(unknown)'; }
# Detect if running in a cloud environment
detect_cloud_environment() {
if [[ -f /proc/cpuinfo ]] && grep -q "^flags.* hypervisor" /proc/cpuinfo && [[ ! -d "/mnt/c/Windows/" ]]; then
cloud="☁️ "
else
cloud=""
fi
}
# Terminal color setup
setup_terminal_colors() {
if tput setaf 1 &> /dev/null; then
tput sgr0; # reset colors
bold=$(tput bold);
reset=$(tput sgr0);
black=$(tput setaf 0);
blue=$(tput setaf 33);
cyan=$(tput setaf 37);
green=$(tput setaf 64);
orange=$(tput setaf 166);
purple=$(tput setaf 125);
red=$(tput setaf 124);
violet=$(tput setaf 61);
white=$(tput setaf 15);
yellow=$(tput setaf 136);
else
bold='';
reset="\\e[0m";
black="\\e[1;30m";
blue="\\e[1;34m";
cyan="\\e[1;36m";
green="\\e[1;32m";
orange="\\e[1;33m";
purple="\\e[1;35m";
red="\\e[1;31m";
violet="\\e[1;35m";
white="\\e[1;37m";
yellow="\\e[1;33m";
fi
}
# User and Host styling for prompt
set_prompt_styles() {
[[ "${USER}" == "root" ]] && userStyle="${red}" || userStyle="${blue}"
[[ "${SSH_TTY}" ]] && hostStyle="${bold}${cyan}" || hostStyle="${cyan}"
}
# PS1 and PS2: Main and secondary prompt string setup
setup_prompts() {
PS1="\\[\\033]0;\\w\\007\\]" # Set terminal title to current directory
PS1+="\\[${bold}\\]\\n" # Newline
PS1+="\\[${userStyle}\\]\\u" # Username
PS1+="\\[${white}\\] at "
PS1+="\\[${hostStyle}\\]${cloud}\\h" # Hostname
PS1+="\\[${white}\\] in "
PS1+="\\[${green}\\]\\w" # Working directory
PS1+="\$(prompt_git \"${white} on ${violet}\")" # Git details
PS1+="\\n"
PS1+="\\[${white}\\]\$ \\[${reset}\\]" # `$` prompt and reset color
export PS1
PS2="\\[${yellow}\\]→ \\[${reset}\\]"
export PS2
}
# Check terminal color support
check_terminal_color_support() {
case "$TERM" in
xterm-color|*-256color) color_prompt=yes ;;
*) color_prompt=no ;;
esac
}
# Configure terminal title for specific terminals
configure_terminal_title() {
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
}
# Invoke the functions
detect_cloud_environment
setup_terminal_colors
set_prompt_styles
setup_prompts
check_terminal_color_support
configure_terminal_title