lib / console.tclon commit git-gui: Abstract the revision picker into a mega widget (b1fa2bf)
   1# git-gui console support
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4class console {
   5
   6field t_short
   7field t_long
   8field w
   9field console_cr
  10
  11constructor new {short_title long_title} {
  12        set t_short $short_title
  13        set t_long $long_title
  14        _init $this
  15        return $this
  16}
  17
  18method _init {} {
  19        global M1B
  20        make_toplevel top w -autodelete 0
  21        wm title $top "[appname] ([reponame]): $t_short"
  22        set console_cr 1.0
  23
  24        frame $w.m
  25        label $w.m.l1 \
  26                -textvariable @t_long  \
  27                -anchor w \
  28                -justify left \
  29                -font font_uibold
  30        text $w.m.t \
  31                -background white -borderwidth 1 \
  32                -relief sunken \
  33                -width 80 -height 10 \
  34                -font font_diff \
  35                -state disabled \
  36                -yscrollcommand [list $w.m.sby set]
  37        label $w.m.s -text {Working... please wait...} \
  38                -anchor w \
  39                -justify left \
  40                -font font_uibold
  41        scrollbar $w.m.sby -command [list $w.m.t yview]
  42        pack $w.m.l1 -side top -fill x
  43        pack $w.m.s -side bottom -fill x
  44        pack $w.m.sby -side right -fill y
  45        pack $w.m.t -side left -fill both -expand 1
  46        pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
  47
  48        menu $w.ctxm -tearoff 0
  49        $w.ctxm add command -label "Copy" \
  50                -command "tk_textCopy $w.m.t"
  51        $w.ctxm add command -label "Select All" \
  52                -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
  53        $w.ctxm add command -label "Copy All" \
  54                -command "
  55                        $w.m.t tag add sel 0.0 end
  56                        tk_textCopy $w.m.t
  57                        $w.m.t tag remove sel 0.0 end
  58                "
  59
  60        button $w.ok -text {Close} \
  61                -state disabled \
  62                -command "destroy $w"
  63        pack $w.ok -side bottom -anchor e -pady 10 -padx 10
  64
  65        bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
  66        bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
  67        bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
  68        bind $w <Visibility> "focus $w"
  69}
  70
  71method exec {cmd {after {}}} {
  72        # -- Cygwin's Tcl tosses the enviroment when we exec our child.
  73        #    But most users need that so we have to relogin. :-(
  74        #
  75        if {[is_Cygwin]} {
  76                set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
  77        }
  78
  79        # -- Tcl won't let us redirect both stdout and stderr to
  80        #    the same pipe.  So pass it through cat...
  81        #
  82        set cmd [concat | $cmd |& cat]
  83
  84        set fd_f [open $cmd r]
  85        fconfigure $fd_f -blocking 0 -translation binary
  86        fileevent $fd_f readable [cb _read $fd_f $after]
  87}
  88
  89method _read {fd after} {
  90        set buf [read $fd]
  91        if {$buf ne {}} {
  92                if {![winfo exists $w.m.t]} {_init $this}
  93                $w.m.t conf -state normal
  94                set c 0
  95                set n [string length $buf]
  96                while {$c < $n} {
  97                        set cr [string first "\r" $buf $c]
  98                        set lf [string first "\n" $buf $c]
  99                        if {$cr < 0} {set cr [expr {$n + 1}]}
 100                        if {$lf < 0} {set lf [expr {$n + 1}]}
 101
 102                        if {$lf < $cr} {
 103                                $w.m.t insert end [string range $buf $c $lf]
 104                                set console_cr [$w.m.t index {end -1c}]
 105                                set c $lf
 106                                incr c
 107                        } else {
 108                                $w.m.t delete $console_cr end
 109                                $w.m.t insert end "\n"
 110                                $w.m.t insert end [string range $buf $c $cr]
 111                                set c $cr
 112                                incr c
 113                        }
 114                }
 115                $w.m.t conf -state disabled
 116                $w.m.t see end
 117        }
 118
 119        fconfigure $fd -blocking 1
 120        if {[eof $fd]} {
 121                if {[catch {close $fd}]} {
 122                        set ok 0
 123                } else {
 124                        set ok 1
 125                }
 126                if {$after ne {}} {
 127                        uplevel #0 $after $ok
 128                } else {
 129                        done $this $ok
 130                }
 131                return
 132        }
 133        fconfigure $fd -blocking 0
 134}
 135
 136method chain {cmdlist {ok 1}} {
 137        if {$ok} {
 138                if {[llength $cmdlist] == 0} {
 139                        done $this $ok
 140                        return
 141                }
 142
 143                set cmd [lindex $cmdlist 0]
 144                set cmdlist [lrange $cmdlist 1 end]
 145
 146                if {[lindex $cmd 0] eq {exec}} {
 147                        exec $this \
 148                                [lrange $cmd 1 end] \
 149                                [cb chain $cmdlist]
 150                } else {
 151                        uplevel #0 $cmd [cb chain $cmdlist]
 152                }
 153        } else {
 154                done $this $ok
 155        }
 156}
 157
 158method done {ok} {
 159        if {$ok} {
 160                if {[winfo exists $w.m.s]} {
 161                        $w.m.s conf -background green -text {Success}
 162                        $w.ok conf -state normal
 163                        focus $w.ok
 164                }
 165        } else {
 166                if {![winfo exists $w.m.s]} {
 167                        _init $this
 168                }
 169                $w.m.s conf -background red -text {Error: Command Failed}
 170                $w.ok conf -state normal
 171                focus $w.ok
 172        }
 173        delete_this
 174}
 175
 176}