1 # git-gui console support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 namespace eval console {
6 variable next_console_id 0
10 proc new {short_title long_title} {
11 variable next_console_id
14 set w .console[incr next_console_id]
15 set console_data($w) [list $short_title $long_title]
24 set console_cr($w) 1.0
27 label $w.m.l1 -text "[lindex $console_data($w) 1]:" \
32 -background white -borderwidth 1 \
34 -width 80 -height 10 \
37 -yscrollcommand [list $w.m.sby set]
38 label $w.m.s -text {Working... please wait...} \
42 scrollbar $w.m.sby -command [list $w.m.t yview]
43 pack $w.m.l1 -side top -fill x
44 pack $w.m.s -side bottom -fill x
45 pack $w.m.sby -side right -fill y
46 pack $w.m.t -side left -fill both -expand 1
47 pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
49 menu $w.ctxm -tearoff 0
50 $w.ctxm add command -label "Copy" \
51 -command "tk_textCopy $w.m.t"
52 $w.ctxm add command -label "Select All" \
53 -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
54 $w.ctxm add command -label "Copy All" \
56 $w.m.t tag add sel 0.0 end
58 $w.m.t tag remove sel 0.0 end
61 button $w.ok -text {Close} \
64 pack $w.ok -side bottom -anchor e -pady 10 -padx 10
66 bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
67 bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
68 bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
69 bind $w <Visibility> "focus $w"
70 wm title $w "[appname] ([reponame]): [lindex $console_data($w) 0]"
74 proc exec {w cmd {after {}}} {
75 # -- Cygwin's Tcl tosses the enviroment when we exec our child.
76 # But most users need that so we have to relogin. :-(
79 set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
82 # -- Tcl won't let us redirect both stdout and stderr to
83 # the same pipe. So pass it through cat...
85 set cmd [concat | $cmd |& cat]
87 set fd_f [open $cmd r]
88 fconfigure $fd_f -blocking 0 -translation binary
89 fileevent $fd_f readable \
90 [namespace code [list _read $w $fd_f $after]]
93 proc _read {w fd after} {
98 if {![winfo exists $w]} {_init $w}
99 $w.m.t conf -state normal
101 set n [string length $buf]
103 set cr [string first "\r" $buf $c]
104 set lf [string first "\n" $buf $c]
105 if {$cr < 0} {set cr [expr {$n + 1}]}
106 if {$lf < 0} {set lf [expr {$n + 1}]}
109 $w.m.t insert end [string range $buf $c $lf]
110 set console_cr($w) [$w.m.t index {end -1c}]
114 $w.m.t delete $console_cr($w) end
115 $w.m.t insert end "\n"
116 $w.m.t insert end [string range $buf $c $cr]
121 $w.m.t conf -state disabled
125 fconfigure $fd -blocking 1
127 if {[catch {close $fd}]} {
133 uplevel #0 $after $w $ok
139 fconfigure $fd -blocking 0
142 proc chain {cmdlist w {ok 1}} {
144 if {[llength $cmdlist] == 0} {
149 set cmd [lindex $cmdlist 0]
150 set cmdlist [lrange $cmdlist 1 end]
152 if {[lindex $cmd 0] eq {exec}} {
155 [namespace code [list chain $cmdlist]]
157 uplevel #0 $cmd $cmdlist $w $ok
166 variable console_data
168 switch -- [llength $args] {
170 set w [lindex $args 0]
171 set ok [lindex $args 1]
174 set w [lindex $args 1]
175 set ok [lindex $args 2]
178 error "wrong number of args: done ?ignored? w ok"
183 if {[winfo exists $w]} {
184 $w.m.s conf -background green -text {Success}
185 $w.ok conf -state normal
189 if {![winfo exists $w]} {
192 $w.m.s conf -background red -text {Error: Command Failed}
193 $w.ok conf -state normal
197 array unset console_cr $w
198 array unset console_data $w