git-gui: Work around bad interaction between Tcl and cmd.exe on ^{tree}
[tgit.git] / lib / console.tcl
blob34de5d48593be91fed3b97e66f1a8d9dc06acfdf
1 # git-gui console support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 class console {
6 field t_short
7 field t_long
8 field w
9 field console_cr
11 constructor new {short_title long_title} {
12 set t_short $short_title
13 set t_long $long_title
14 _init $this
15 return $this
18 method _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
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
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
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
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"
75 method 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. :-(
79 if {[is_Cygwin]} {
80 set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
83 # -- Tcl won't let us redirect both stdout and stderr to
84 # the same pipe. So pass it through cat...
86 set cmd [concat | $cmd |& cat]
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]
93 method _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}]}
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
119 $w.m.t conf -state disabled
120 $w.m.t see end
123 fconfigure $fd -blocking 1
124 if {[eof $fd]} {
125 if {[catch {close $fd}]} {
126 set ok 0
127 } else {
128 set ok 1
130 if {$after ne {}} {
131 uplevel #0 $after $ok
132 } else {
133 done $this $ok
135 return
137 fconfigure $fd -blocking 0
140 method chain {cmdlist {ok 1}} {
141 if {$ok} {
142 if {[llength $cmdlist] == 0} {
143 done $this $ok
144 return
147 set cmd [lindex $cmdlist 0]
148 set cmdlist [lrange $cmdlist 1 end]
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]
157 } else {
158 done $this $ok
162 method 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
169 } else {
170 if {![winfo exists $w.m.s]} {
171 _init $this
173 $w.m.s conf -background red -text {Error: Command Failed}
174 $w.ok conf -state normal
175 focus $w.ok
177 delete_this