-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPSone
executable file
·96 lines (88 loc) · 2.31 KB
/
PSone
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
#!/usr/bin/env bash
# Return the colorcode for the given color
color(){
declare -A fgcol=( ["Default"]="\[\e[39m\]" ["Black"]="\[\e[30m\]" ["Red"]="\[\e[31m\]" ["Green"]="\[\e[32m\]" ["Yellow"]="\[\e[33m\]" ["Blue"]="\[\e[34m\]" ["Magenta"]="\[\e[35m\]" ["Cyan"]="\[\e[36m\]" ["White"]="\[\e[37m\]")
echo "${fgcol[$1]}"
}
# Print git status if in a git repo
__git_status(){
if [[ $PWD == "$HOME" ]]; then
return
fi
if ! git branch --show-current &>/dev/null ; then
return
fi
branch=$(git branch --show-current 2>/dev/null)
mess=$(git status -z | head -c2)
clean_color=$(color Green)
dirty_color=$(color Yellow)
hash=":$(git rev-parse --short HEAD 2>/dev/null)"
cols=$(tput cols)
msg="${branch}${hash}"
if [[ "${#msg}" -gt $((cols/4)) || "$hash" = ":" ]]; then
msg="${branch}"
else
msg="${branch}${hash}"
fi
if [[ -z "$mess" ]]; then
git_color=$clean_color
else
git_color=$dirty_color
fi
echo "$git_color$msg$def"
}
_venv_status(){
if [[ -n ${VIRTUAL_ENV+x} ]] ; then
echo "$(color Green)${VIRTUAL_ENV_PROMPT}$def" # show (venv) if inside a python venv
fi
}
_path_print(){
if [[ "$PWD" == "$HOME" ]]; then
echo "~"
else
dir=${PWD%/*}
# shellcheck disable=2001
dir=$(sed "s|${HOME}|~|g" <<< "$dir")
cols=$(tput cols)
if [[ ${#dir} -gt $((cols/4)) ]]; then
dir="...${dir: -((cols/4-3))}"
fi
echo "$dir/$(color Blue)${PWD##*/}$def"
fi
}
_file_count(){
files=$(find . -mindepth 1 -maxdepth 1)
files_unhidden=$(grep -c "^\./[^\.]" <<< "$files")
files_total=$(grep -c "^\./" <<< "$files")
if [[ $files_unhidden -eq $files_total ]]; then
output="($files_total)"
else
output="($files_unhidden, $files_total)"
fi
echo "$output"
}
_exit_code(){
ec=${1:-0}
if [[ $ec -ne 0 ]]; then
echo "$(color Red)× $ec$def" #then show exit code in red in prompt
fi
}
# Command that prints the prompt
prompt(){
ec=$? # Get exit code of last run command
def=$(color Default) # Default text color of terminal
if [[ $UID != 0 ]]; then
user_color=$(color Green) #set arrow as green
else
user_color=$(color Red) #otherwise red
fi
p="$user_color╭ $def"
p+="$(_venv_status)"
p+="$(__git_status) "
p+="$(_path_print) "
p+="$(_file_count) "
p+="$(_exit_code $ec) "
p+="\n"
p+="$user_color╰➤$def "
export PS1="$p"
}