git-gui / lib / console.tclon commit documentation: use the word "index" in the git-add manual page (5f42ac9)
   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                -wrap none \
  35                -font font_diff \
  36                -state disabled \
  37                -xscrollcommand [list $w.m.sbx set] \
  38                -yscrollcommand [list $w.m.sby set]
  39        label $w.m.s -text {Working... please wait...} \
  40                -anchor w \
  41                -justify left \
  42                -font font_uibold
  43        scrollbar $w.m.sbx -command [list $w.m.t xview] -orient h
  44        scrollbar $w.m.sby -command [list $w.m.t yview]
  45        pack $w.m.l1 -side top -fill x
  46        pack $w.m.s -side bottom -fill x
  47        pack $w.m.sbx -side bottom -fill x
  48        pack $w.m.sby -side right -fill y
  49        pack $w.m.t -side left -fill both -expand 1
  50        pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
  51
  52        menu $w.ctxm -tearoff 0
  53        $w.ctxm add command -label "Copy" \
  54                -command "tk_textCopy $w.m.t"
  55        $w.ctxm add command -label "Select All" \
  56                -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
  57        $w.ctxm add command -label "Copy All" \
  58                -command "
  59                        $w.m.t tag add sel 0.0 end
  60                        tk_textCopy $w.m.t
  61                        $w.m.t tag remove sel 0.0 end
  62                "
  63
  64        button $w.ok -text {Close} \
  65                -state disabled \
  66                -command "destroy $w"
  67        pack $w.ok -side bottom -anchor e -pady 10 -padx 10
  68
  69        bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
  70        bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
  71        bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
  72        bind $w <Visibility> "focus $w"
  73}
  74
  75method exec {cmd {after {}}} {
  76        # -- Cygwin's Tcl tosses the enviroment when we exec our child.
  77        #    But most users need that so we have to relogin. :-(
  78        #
  79        if {[is_Cygwin]} {
  80                set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
  81        }
  82
  83        # -- Tcl won't let us redirect both stdout and stderr to
  84        #    the same pipe.  So pass it through cat...
  85        #
  86        set cmd [concat | $cmd |& cat]
  87
  88        set fd_f [open $cmd r]
  89        fconfigure $fd_f -blocking 0 -translation binary
  90        fileevent $fd_f readable [cb _read $fd_f $after]
  91}
  92
  93method _read {fd after} {
  94        set buf [read $fd]
  95        if {$buf ne {}} {
  96                if {![winfo exists $w.m.t]} {_init $this}
  97                $w.m.t conf -state normal
  98                set c 0
  99                set n [string length $buf]
 100                while {$c < $n} {
 101                        set cr [string first "\r" $buf $c]
 102                        set lf [string first "\n" $buf $c]
 103                        if {$cr < 0} {set cr [expr {$n + 1}]}
 104                        if {$lf < 0} {set lf [expr {$n + 1}]}
 105
 106                        if {$lf < $cr} {
 107                                $w.m.t insert end [string range $buf $c $lf]
 108                                set console_cr [$w.m.t index {end -1c}]
 109                                set c $lf
 110                                incr c
 111                        } else {
 112                                $w.m.t delete $console_cr end
 113                                $w.m.t insert end "\n"
 114                                $w.m.t insert end [string range $buf $c $cr]
 115                                set c $cr
 116                                incr c
 117                        }
 118                }
 119                $w.m.t conf -state disabled
 120                $w.m.t see end
 121        }
 122
 123        fconfigure $fd -blocking 1
 124        if {[eof $fd]} {
 125                if {[catch {close $fd}]} {
 126                        set ok 0
 127                } else {
 128                        set ok 1
 129                }
 130                if {$after ne {}} {
 131                        uplevel #0 $after $ok
 132                } else {
 133                        done $this $ok
 134                }
 135                return
 136        }
 137        fconfigure $fd -blocking 0
 138}
 139
 140method chain {cmdlist {ok 1}} {
 141        if {$ok} {
 142                if {[llength $cmdlist] == 0} {
 143                        done $this $ok
 144                        return
 145                }
 146
 147                set cmd [lindex $cmdlist 0]
 148                set cmdlist [lrange $cmdlist 1 end]
 149
 150                if {[lindex $cmd 0] eq {exec}} {
 151                        exec $this \
 152                                [lrange $cmd 1 end] \
 153                                [cb chain $cmdlist]
 154                } else {
 155                        uplevel #0 $cmd [cb chain $cmdlist]
 156                }
 157        } else {
 158                done $this $ok
 159        }
 160}
 161
 162method done {ok} {
 163        if {$ok} {
 164                if {[winfo exists $w.m.s]} {
 165                        $w.m.s conf -background green -text {Success}
 166                        $w.ok conf -state normal
 167                        focus $w.ok
 168                }
 169        } else {
 170                if {![winfo exists $w.m.s]} {
 171                        _init $this
 172                }
 173                $w.m.s conf -background red -text {Error: Command Failed}
 174                $w.ok conf -state normal
 175                focus $w.ok
 176        }
 177        delete_this
 178}
 179
 180}