git-gui / lib / status_bar.tclon commit Merge branch 'maint' of git://repo.or.cz/git-gui into maint (faf1dc7)
   1# git-gui status bar mega-widget
   2# Copyright (C) 2007 Shawn Pearce
   3
   4class status_bar {
   5
   6field w         ; # our own window path
   7field w_l       ; # text widget we draw messages into
   8field w_c       ; # canvas we draw a progress bar into
   9field c_pack    ; # script to pack the canvas with
  10field status  {}; # single line of text we show
  11field prefix  {}; # text we format into status
  12field units   {}; # unit of progress
  13field meter   {}; # current core git progress meter (if active)
  14
  15constructor new {path} {
  16        set w $path
  17        set w_l $w.l
  18        set w_c $w.c
  19
  20        frame $w \
  21                -borderwidth 1 \
  22                -relief sunken
  23        label $w_l \
  24                -textvariable @status \
  25                -anchor w \
  26                -justify left
  27        pack $w_l -side left
  28        set c_pack [cb _oneline_pack]
  29
  30        bind $w <Destroy> [cb _delete %W]
  31        return $this
  32}
  33
  34method _oneline_pack {} {
  35        $w_c conf -width 100
  36        pack $w_c -side right
  37}
  38
  39constructor two_line {path} {
  40        set w $path
  41        set w_l $w.l
  42        set w_c $w.c
  43
  44        frame $w
  45        label $w_l \
  46                -textvariable @status \
  47                -anchor w \
  48                -justify left
  49        pack $w_l -anchor w -fill x
  50        set c_pack [list pack $w_c -fill x]
  51
  52        bind $w <Destroy> [cb _delete %W]
  53        return $this
  54}
  55
  56method start {msg uds} {
  57        if {[winfo exists $w_c]} {
  58                $w_c coords bar 0 0 0 20
  59        } else {
  60                canvas $w_c \
  61                        -height [expr {int([winfo reqheight $w_l] * 0.6)}] \
  62                        -borderwidth 1 \
  63                        -relief groove \
  64                        -highlightt 0
  65                $w_c create rectangle 0 0 0 20 -tags bar -fill navy
  66                eval $c_pack
  67        }
  68
  69        set status $msg
  70        set prefix $msg
  71        set units  $uds
  72        set meter  {}
  73}
  74
  75method update {have total} {
  76        set pdone 0
  77        if {$total > 0} {
  78                set pdone [expr {100 * $have / $total}]
  79                set cdone [expr {[winfo width $w_c] * $have / $total}]
  80        }
  81
  82        set prec [string length [format %i $total]]
  83        set status [mc "%s ... %*i of %*i %s (%3i%%)" \
  84                $prefix \
  85                $prec $have \
  86                $prec $total \
  87                $units $pdone]
  88        $w_c coords bar 0 0 $cdone 20
  89}
  90
  91method update_meter {buf} {
  92        append meter $buf
  93        set r [string last "\r" $meter]
  94        if {$r == -1} {
  95                return
  96        }
  97
  98        set prior [string range $meter 0 $r]
  99        set meter [string range $meter [expr {$r + 1}] end]
 100        set p "\\((\\d+)/(\\d+)\\)"
 101        if {[regexp ":\\s*\\d+% $p\(?:, done.\\s*\n|\\s*\r)\$" $prior _j a b]} {
 102                update $this $a $b
 103        } elseif {[regexp "$p\\s+done\r\$" $prior _j a b]} {
 104                update $this $a $b
 105        }
 106}
 107
 108method stop {{msg {}}} {
 109        destroy $w_c
 110        if {$msg ne {}} {
 111                set status $msg
 112        }
 113}
 114
 115method show {msg {test {}}} {
 116        if {$test eq {} || $status eq $test} {
 117                set status $msg
 118        }
 119}
 120
 121method _delete {current} {
 122        if {$current eq $w} {
 123                delete_this
 124        }
 125}
 126
 127}