]>
Commit | Line | Data |
---|---|---|
9d0d2184 ER |
1 | # NOTE: |
2 | # This code works known to work for bash | |
3 | ||
4 | # To use it, source this file and set $PROMPT_COMMAND env var: | |
5 | # PROMPT_COMMAND=__bash_prompt_command | |
6 | ||
7 | # | |
8 | # A colorized bash prompt | |
9 | # - shows curret branch | |
10 | # - shows if branch is up to date/ahead/behind | |
11 | # - shows if last command exited with error (red) | |
12 | # | |
13 | __bash_prompt_command() { | |
14 | local previous_return_value=$? | |
15 | ||
16 | local RED="\[\033[0;31m\]" | |
17 | local YELLOW="\[\033[0;33m\]" | |
18 | local GREEN="\[\033[0;32m\]" | |
19 | local BLUE="\[\033[0;34m\]" | |
20 | local LIGHT_RED="\[\033[1;31m\]" | |
21 | local LIGHT_GREEN="\[\033[1;32m\]" | |
22 | local WHITE="\[\033[1;37m\]" | |
23 | local LIGHT_GRAY="\[\033[0;37m\]" | |
24 | local COLOR_NONE="\[\e[0m\]" | |
25 | ||
26 | # if we are in rpm subdir and have exactly one .spec in the dir, include package version | |
27 | __package_update_rpmversion | |
28 | local rpmver=$(__package_rpmversion) | |
29 | ||
30 | local prompt="${BLUE}[${RED}\w${GREEN}${rpmver:+($rpmver)}$(__bash_parse_git_branch)${BLUE}]${COLOR_NONE} " | |
31 | if [ $previous_return_value -eq 0 ]; then | |
32 | PS1="${prompt}➔ " | |
33 | else | |
34 | PS1="${prompt}${RED}➔${COLOR_NONE} " | |
35 | fi | |
36 | } | |
37 | ||
38 | # helper for __bash_prompt_command | |
39 | # command line (git) coloring | |
40 | # note we use "\" here to avoid any "git" previous alias/func | |
41 | __bash_parse_git_branch() { | |
42 | # not in git dir. return early | |
43 | \git rev-parse --git-dir &> /dev/null || return | |
44 | ||
45 | local git_status branch_pattern remote_pattern diverge_pattern | |
46 | local state remote branch | |
47 | ||
48 | git_status=$(\git -c color.ui=no status 2> /dev/null) | |
49 | branch_pattern="^On branch ([^${IFS}]*)" | |
50 | remote_pattern="Your branch is (behind|ahead) " | |
51 | diverge_pattern="Your branch and (.*) have diverged" | |
52 | ||
53 | if [[ ! ${git_status} =~ "working directory clean" ]]; then | |
54 | state="${RED}⚡" | |
55 | fi | |
56 | ||
57 | # add an else if or two here if you want to get more specific | |
58 | if [[ ${git_status} =~ ${remote_pattern} ]]; then | |
59 | if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then | |
60 | remote="${YELLOW}↑" | |
61 | else | |
62 | remote="${YELLOW}↓" | |
63 | fi | |
64 | fi | |
65 | ||
66 | if [[ ${git_status} =~ ${diverge_pattern} ]]; then | |
67 | remote="${YELLOW}↕" | |
68 | fi | |
69 | ||
70 | if [[ ${git_status} =~ ${branch_pattern} ]]; then | |
71 | branch=${BASH_REMATCH[1]} | |
72 | echo " (${branch})${remote}${state}" | |
73 | fi | |
74 | } | |
75 | ||
76 | # cache requires bash 4.x | |
77 | declare -A __package_update_rpmversion_cache | |
78 | __package_update_rpmversion() { | |
79 | # extract vars from cache | |
80 | set -- ${__package_update_rpmversion_cache[$PWD]} | |
81 | local specfile=$1 version=$2 mtime=$3 | |
82 | ||
83 | # invalidate cache | |
84 | if [ -f "$specfile" ]; then | |
85 | local stat | |
86 | stat=$(stat -c %Y $specfile) | |
87 | if [ $mtime ] && [ $stat -gt $mtime ]; then | |
88 | unset version | |
89 | fi | |
90 | mtime=$stat | |
91 | else | |
92 | # reset cache, .spec may be renamed | |
93 | unset version specfile | |
94 | fi | |
95 | ||
96 | # we have cached version | |
97 | test -n "$version" && return | |
98 | ||
99 | # needs to be one file | |
100 | specfile=${specfile:-$(\ls *.spec 2>/dev/null)} | |
101 | if [ ! -f "$specfile" ]; then | |
102 | unset __package_update_rpmversion_cache[$PWD] | |
103 | return | |
104 | fi | |
105 | ||
106 | mtime=${mtime:-$(stat -c %Y $specfile)} | |
107 | ||
108 | # give only first version (ignore subpackages) | |
109 | version=$(rpm --define "_specdir $PWD" --specfile $specfile -q --qf '%{VERSION}\n' | head -n1) | |
110 | __package_update_rpmversion_cache[$PWD]="$specfile ${version:-?} $mtime" | |
111 | } | |
112 | ||
113 | __package_rpmversion() { | |
114 | # extract vars from cache | |
115 | set -- ${__package_update_rpmversion_cache[$PWD]} | |
116 | # print version | |
117 | echo $2 | |
118 | } |