1 # git-gui status bar mega-widget
2 # Copyright (C) 2007 Shawn Pearce
4 # The status_bar class manages the entire status bar. It is possible for
5 # multiple overlapping asynchronous operations to want to display status
6 # simultaneously. Each one receives a status_bar_operation when it calls the
7 # start method, and the status bar combines all active operations into the
8 # line of text it displays. Most of the time, there will be at most one
11 # Note that the entire status bar can be either in single-line or two-line
12 # mode, depending on the constructor. Multiple active operations are only
13 # supported for single-line status bars.
17 field allow_multiple ; # configured at construction
19 field w ; # our own window path
20 field w_l ; # text widget we draw messages into
21 field w_c ; # canvas we draw a progress bar into
22 field c_pack ; # script to pack the canvas with
24 field baseline_text ; # text to show if there are no operations
25 field status_bar_text ; # combined text for all operations
27 field operations ; # list of current ongoing operations
29 # The status bar can display a progress bar, updated when consumers call the
30 # update method on their status_bar_operation. When there are multiple
31 # operations, the status bar shows the combined status of all operations.
33 # When an overlapping operation completes, the progress bar is going to
34 # abruptly have one fewer operation in the calculation, causing a discontinuity.
35 # Therefore, whenever an operation completes, if it is not the last operation,
36 # this counter is increased, and the progress bar is calculated as though there
37 # were still another operation at 100%. When the last operation completes, this
39 field completed_operation_count
41 constructor new {path} {
47 # Standard single-line status bar: Permit overlapping operations
52 set completed_operation_count 0
56 $w configure -borderwidth 1 -relief sunken
59 -textvariable @status_bar_text \
63 set c_pack [cb _oneline_pack]
65 bind $w <Destroy> [cb _delete %W]
69 method _oneline_pack {} {
74 constructor two_line {path} {
80 # Two-line status bar: Only one ongoing operation permitted.
85 set completed_operation_count 0
89 -textvariable @status_bar_text \
92 pack $w_l -anchor w -fill x
93 set c_pack [list pack $w_c -fill x]
95 bind $w <Destroy> [cb _delete %W]
99 method ensure_canvas {} {
100 if {[winfo exists $w_c]} {
101 $w_c coords bar 0 0 0 20
104 -height [expr {int([winfo reqheight $w_l] * 0.6)}] \
108 $w_c create rectangle 0 0 0 20 -tags bar -fill navy
115 set baseline_text $msg
119 method start {msg {uds {}}} {
122 if {!$allow_multiple && [llength $operations]} {
123 return [lindex $operations 0]
128 set operation [status_bar_operation::new $this $msg $uds]
130 lappend operations $operation
140 set total [expr $completed_operation_count * 100]
143 foreach operation $operations {
144 if {$new_text != ""} {
145 append new_text " / "
148 append new_text [$operation get_status]
150 set total [expr $total + 100]
151 set have [expr $have + [$operation get_progress]]
154 if {$new_text == ""} {
155 set new_text $baseline_text
158 set status_bar_text $new_text
160 if {[winfo exists $w_c]} {
163 set pixel_width [expr {[winfo width $w_c] * $have / $total}]
166 $w_c coords bar 0 0 $pixel_width 20
170 method stop {operation stop_msg} {
171 set idx [lsearch $operations $operation]
174 set operations [lreplace $operations $idx $idx]
175 set completed_operation_count [expr \
176 $completed_operation_count + 1]
178 if {[llength $operations] == 0} {
179 set completed_operation_count 0
182 if {$stop_msg ne {}} {
183 set baseline_text $stop_msg
191 method stop_all {{stop_msg {}}} {
192 # This makes the operation's call to stop a no-op.
193 set operations_copy $operations
194 set operations [list]
196 foreach operation $operations_copy {
200 if {$stop_msg ne {}} {
201 set baseline_text $stop_msg
207 method _delete {current} {
208 if {$current eq $w} {
215 # The status_bar_operation class tracks a single consumer's ongoing status bar
216 # activity, with the context that there are a few situations where multiple
217 # overlapping asynchronous operations might want to display status information
218 # simultaneously. Instances of status_bar_operation are created by calling
219 # start on the status_bar, and when the caller is done with its stauts bar
220 # operation, it calls stop on the operation.
222 class status_bar_operation {
224 field status_bar; # reference back to the status_bar that owns this object
228 field status {}; # single line of text we show
229 field progress {}; # current progress (0 to 100)
230 field prefix {}; # text we format into status
231 field units {}; # unit of progress
232 field meter {}; # current core git progress meter (if active)
234 constructor new {owner msg uds} {
235 set status_bar $owner
248 method get_is_active {} { return $is_active }
249 method get_status {} { return $status }
250 method get_progress {} { return $progress }
252 method update {have total} {
253 if {!$is_active} { return }
258 set progress [expr {100 * $have / $total}]
261 set prec [string length [format %i $total]]
263 set status [mc "%s ... %*i of %*i %s (%3i%%)" \
272 method update_meter {buf} {
273 if {!$is_active} { return }
276 set r [string last "\r" $meter]
281 set prior [string range $meter 0 $r]
282 set meter [string range $meter [expr {$r + 1}] end]
283 set p "\\((\\d+)/(\\d+)\\)"
284 if {[regexp ":\\s*\\d+% $p\(?:, done.\\s*\n|\\s*\r)\$" $prior _j a b]} {
286 } elseif {[regexp "$p\\s+done\r\$" $prior _j a b]} {
291 method stop {{stop_msg {}}} {
294 $status_bar stop $this $stop_msg
298 method restart {msg} {
299 if {!$is_active} { return }