Skip to content

Commit c1cbdbf

Browse files
committed
factor all messages out of display loop into parsing loop
* this refactoring simplifies column width calculation * fix bug supressing * from current branch display with --date flag
1 parent 674c566 commit c1cbdbf

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

git-branch-status

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ function current_branch
7272

7373
function is_current_branch # (a_branch_name)
7474
{
75-
if [ "$1" == "$(current_branch)" ] ; then echo 1 ; else echo 0 ; fi ;
75+
current=$(current_branch)
76+
if (($SHOW_DATES))
77+
then this_branch=$(get_head_date $1)$1 ; current=$(get_head_date $current)$current ;
78+
else this_branch=$1
79+
fi
80+
81+
if [ "$this_branch" == "$current" ] ; then echo 1 ; else echo 0 ; fi ;
7682
}
7783

7884
function does_branch_exist # (a_branch_name)
@@ -118,7 +124,7 @@ fi
118124

119125
readonly SHOW_ALL_LOCAL=$(($SHOW_ALL + 0)) # also show branches that have no upstream
120126
readonly SHOW_ALL_REMOTE=$(($SHOW_ALL + 0)) # also show branches that are up to date
121-
readonly MAX_COL_W=27
127+
readonly MAX_COL_W=27 # should be => 12
122128
readonly CWHITE='\033[0;37m'
123129
readonly CGREEN='\033[0;32m'
124130
readonly CYELLOW='\033[1;33m'
@@ -160,35 +166,38 @@ do
160166

161167
# parse local<->remote sync status
162168
if [ $remote ] ; then
163-
status=$(get_status)
164-
[ $(($?)) -eq 0 ] || continue
169+
status=$(get_status) ; (($?)) && continue ;
165170

166171
n_behind=$(echo $status | tr " " "\n" | grep -c '^>')
167172
n_ahead=$( echo $status | tr " " "\n" | grep -c '^<')
168173
n_differences=$(($n_behind + $n_ahead))
169174
n_total_differences=$(($n_total_differences + $n_differences))
170175

171176
# filter branches by status
172-
[ "$SHOW_ALL_REMOTE" -eq 0 -a "$n_differences" -eq 0 ] && continue
177+
(($SHOW_ALL_REMOTE)) || (($n_differences)) || continue
173178

174-
# color output
179+
# set data for branches with upstream
175180
local_color=$CDEFAULT
176-
if [ "$n_behind" -ne 0 ] ; then behind_color=$CBEHIND ; else behind_color=$CEVEN ; fi ;
177-
if [ "$n_ahead" -ne 0 ] ; then ahead_color=$CAHEAD ; else ahead_color=$CEVEN ; fi ;
181+
if (($n_behind))
182+
then behind_msg="(behind $n_behind)" ; behind_color=$CBEHIND
183+
else behind_msg="(even)" ; behind_color=$CEVEN ;
184+
fi
185+
if (($n_ahead))
186+
then ahead_msg="(ahead $n_ahead)" ; ahead_color=$CAHEAD ;
187+
else ahead_msg="(even)" ; ahead_color=$CEVEN ;
188+
fi
178189
remote_color=$CDEFAULT
179-
elif [ "$SHOW_ALL_LOCAL" -eq 1 ] ; then
190+
elif (($SHOW_ALL_LOCAL)) ; then
180191
# dummy data for branches with no upstream
181192
local_color=$CDEFAULT
182-
n_behind="X" ; behind_color="$CDEFAULT" ;
183-
n_ahead="X" ; ahead_color="$CDEFAULT" ;
184-
remote="$NO_UPSTREAM" ; remote_color=$CNOUPSTREAM
193+
behind_msg="n/a" ; behind_color="$CDEFAULT" ;
194+
ahead_msg="n/a" ; ahead_color="$CDEFAULT" ;
195+
remote="$NO_UPSTREAM" ; remote_color=$CNOUPSTREAM ;
185196
else continue
186197
fi
187198

188199
# populate lists
189200
local_msg="$(get_head_date $local)$local" ; local_msg="${local_msg:0:$MAX_COL_W}" ;
190-
behind_msg="$n_behind"
191-
ahead_msg="$n_ahead"
192201
remote_msg="$(get_head_date $remote)$remote" ; remote_msg="${remote_msg:0:$MAX_COL_W}" ;
193202
local_msgs=( ${local_msgs[@]} "$local_msg" )
194203
behind_msgs=( ${behind_msgs[@]} "$behind_msg" )
@@ -204,40 +213,21 @@ do
204213
if [ ${#behind_msg} -gt $behind_w ] ; then behind_w=${#behind_msg} ; fi ;
205214
if [ ${#ahead_msg} -gt $ahead_w ] ; then ahead_w=${#ahead_msg} ; fi ;
206215
if [ ${#remote_msg} -gt $remote_w ] ; then remote_w=${#remote_msg} ; fi ;
207-
208216
done < <(get_refs)
209217

210-
# compensate width for dates and "(ahead )" and "(behind )" to be appended
211-
# local_w=$(( $local_w + 11 ))
212-
ahead_w=$(( $ahead_w + 8 ))
213-
behind_w=$(( $behind_w + 9 ))
214-
# remote_w=$(( $remote_w + 11 ))
215-
216218
# pretty print results
217219
for (( result_n = 0 ; result_n < ${#local_msgs[@]} ; result_n++ ))
218220
do
219221
# fetch data
220-
this_branch=${local_msgs[$result_n]}
221-
n_behind=${behind_msgs[$result_n]}
222-
n_ahead=${ahead_msgs[$result_n]}
223-
remote_branch=${remote_msgs[$result_n]}
222+
local_msg=${local_msgs[$result_n]}
223+
behind_msg=${behind_msgs[$result_n]}
224+
ahead_msg=${ahead_msgs[$result_n]}
225+
remote_msg=${remote_msgs[$result_n]}
224226
local_color="${local_colors[$result_n]}"
225227
behind_color="${behind_colors[$result_n]}"
226228
ahead_color="${ahead_colors[$result_n]}"
227229
remote_color="${remote_colors[$result_n]}"
228230

229-
# format data representation
230-
# echo "this_branch=$this_branch current_branch=$(current_branch)" not_current_branch
231-
if (($(is_current_branch $this_branch))) ; then star=$STAR ; else star=" " ; fi ;
232-
local_msg="$this_branch"
233-
behind_msg="(behind $n_behind)"
234-
ahead_msg="(ahead $n_ahead)"
235-
remote_msg="$remote_branch"
236-
if [ "$behind_msg" == "(behind 0)" ] ; then behind_msg="(even)" ; fi ;
237-
if [ "$ahead_msg" == "(ahead 0)" ] ; then ahead_msg="(even)" ; fi ;
238-
if [ "$remote_msg" == "$NO_UPSTREAM" ] ; then behind_msg="n/a" ; fi ;
239-
if [ "$remote_msg" == "$NO_UPSTREAM" ] ; then ahead_msg="n/a" ; fi ;
240-
241231
# calculate column offsets
242232
local_offset=1
243233
behind_offset=$(( $local_w - ${#local_msg} ))
@@ -246,6 +236,7 @@ do
246236
end_offset=$(( $remote_w - ${#remote_msg} ))
247237

248238
# build output messages and display
239+
if (($(is_current_branch $local_msg))) ; then star=$STAR ; else star=" " ; fi ;
249240
local_msg="%$(( $local_offset ))s$star$(echo -e $DELIM $local_color$local_msg$CEND)"
250241
behind_msg="%$(( $behind_offset ))s $( echo -e $DELIM $behind_color$behind_msg$CEND)"
251242
ahead_msg="%$(( $ahead_offset ))s $( echo -e $DELIM $ahead_color$ahead_msg$CEND)"

0 commit comments

Comments
 (0)