Code

GIT 1.5.4.7
[git.git] / git-gui / lib / console.tcl
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 w_t
10 field console_cr
11 field is_toplevel    1; # are we our own window?
13 constructor new {short_title long_title} {
14         set t_short $short_title
15         set t_long $long_title
16         _init $this
17         return $this
18 }
20 constructor embed {path title} {
21         set t_short {}
22         set t_long $title
23         set w $path
24         set is_toplevel 0
25         _init $this
26         return $this
27 }
29 method _init {} {
30         global M1B
32         if {$is_toplevel} {
33                 make_toplevel top w -autodelete 0
34                 wm title $top "[appname] ([reponame]): $t_short"
35         } else {
36                 frame $w
37         }
39         set console_cr 1.0
40         set w_t $w.m.t
42         frame $w.m
43         label $w.m.l1 \
44                 -textvariable @t_long  \
45                 -anchor w \
46                 -justify left \
47                 -font font_uibold
48         text $w_t \
49                 -background white -borderwidth 1 \
50                 -relief sunken \
51                 -width 80 -height 10 \
52                 -wrap none \
53                 -font font_diff \
54                 -state disabled \
55                 -xscrollcommand [cb _sb_set $w.m.sbx h] \
56                 -yscrollcommand [cb _sb_set $w.m.sby v]
57         label $w.m.s -text [mc "Working... please wait..."] \
58                 -anchor w \
59                 -justify left \
60                 -font font_uibold
61         pack $w.m.l1 -side top -fill x
62         pack $w.m.s -side bottom -fill x
63         pack $w_t -side left -fill both -expand 1
64         pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
66         menu $w.ctxm -tearoff 0
67         $w.ctxm add command -label [mc "Copy"] \
68                 -command "tk_textCopy $w_t"
69         $w.ctxm add command -label [mc "Select All"] \
70                 -command "focus $w_t;$w_t tag add sel 0.0 end"
71         $w.ctxm add command -label [mc "Copy All"] \
72                 -command "
73                         $w_t tag add sel 0.0 end
74                         tk_textCopy $w_t
75                         $w_t tag remove sel 0.0 end
76                 "
78         if {$is_toplevel} {
79                 button $w.ok -text [mc "Close"] \
80                         -state disabled \
81                         -command [list destroy $w]
82                 pack $w.ok -side bottom -anchor e -pady 10 -padx 10
83                 bind $w <Visibility> [list focus $w]
84         }
86         bind_button3 $w_t "tk_popup $w.ctxm %X %Y"
87         bind $w_t <$M1B-Key-a> "$w_t tag add sel 0.0 end;break"
88         bind $w_t <$M1B-Key-A> "$w_t tag add sel 0.0 end;break"
89 }
91 method exec {cmd {after {}}} {
92         if {[lindex $cmd 0] eq {git}} {
93                 set fd_f [eval git_read --stderr [lrange $cmd 1 end]]
94         } else {
95                 lappend cmd 2>@1
96                 set fd_f [_open_stdout_stderr $cmd]
97         }
98         fconfigure $fd_f -blocking 0 -translation binary
99         fileevent $fd_f readable [cb _read $fd_f $after]
102 method _read {fd after} {
103         set buf [read $fd]
104         if {$buf ne {}} {
105                 if {![winfo exists $w_t]} {_init $this}
106                 $w_t conf -state normal
107                 set c 0
108                 set n [string length $buf]
109                 while {$c < $n} {
110                         set cr [string first "\r" $buf $c]
111                         set lf [string first "\n" $buf $c]
112                         if {$cr < 0} {set cr [expr {$n + 1}]}
113                         if {$lf < 0} {set lf [expr {$n + 1}]}
115                         if {$lf < $cr} {
116                                 $w_t insert end [string range $buf $c $lf]
117                                 set console_cr [$w_t index {end -1c}]
118                                 set c $lf
119                                 incr c
120                         } else {
121                                 $w_t delete $console_cr end
122                                 $w_t insert end "\n"
123                                 $w_t insert end [string range $buf $c [expr {$cr - 1}]]
124                                 set c $cr
125                                 incr c
126                         }
127                 }
128                 $w_t conf -state disabled
129                 $w_t see end
130         }
132         fconfigure $fd -blocking 1
133         if {[eof $fd]} {
134                 if {[catch {close $fd}]} {
135                         set ok 0
136                 } else {
137                         set ok 1
138                 }
139                 if {$after ne {}} {
140                         uplevel #0 $after $ok
141                 } else {
142                         done $this $ok
143                 }
144                 return
145         }
146         fconfigure $fd -blocking 0
149 method chain {cmdlist {ok 1}} {
150         if {$ok} {
151                 if {[llength $cmdlist] == 0} {
152                         done $this $ok
153                         return
154                 }
156                 set cmd [lindex $cmdlist 0]
157                 set cmdlist [lrange $cmdlist 1 end]
159                 if {[lindex $cmd 0] eq {exec}} {
160                         exec $this \
161                                 [lrange $cmd 1 end] \
162                                 [cb chain $cmdlist]
163                 } else {
164                         uplevel #0 $cmd [cb chain $cmdlist]
165                 }
166         } else {
167                 done $this $ok
168         }
171 method insert {txt} {
172         if {![winfo exists $w_t]} {_init $this}
173         $w_t conf -state normal
174         $w_t insert end "$txt\n"
175         set console_cr [$w_t index {end -1c}]
176         $w_t conf -state disabled
179 method done {ok} {
180         if {$ok} {
181                 if {[winfo exists $w.m.s]} {
182                         bind $w.m.s <Destroy> [list delete_this $this]
183                         $w.m.s conf -background green -text [mc "Success"]
184                         if {$is_toplevel} {
185                                 $w.ok conf -state normal
186                                 focus $w.ok
187                         }
188                 } else {
189                         delete_this
190                 }
191         } else {
192                 if {![winfo exists $w.m.s]} {
193                         _init $this
194                 }
195                 bind $w.m.s <Destroy> [list delete_this $this]
196                 $w.m.s conf -background red -text [mc "Error: Command Failed"]
197                 if {$is_toplevel} {
198                         $w.ok conf -state normal
199                         focus $w.ok
200                 }
201         }
204 method _sb_set {sb orient first last} {
205         if {![winfo exists $sb]} {
206                 if {$first == $last || ($first == 0 && $last == 1)} return
207                 if {$orient eq {h}} {
208                         scrollbar $sb -orient h -command [list $w_t xview]
209                         pack $sb -fill x -side bottom -before $w_t
210                 } else {
211                         scrollbar $sb -orient v -command [list $w_t yview]
212                         pack $sb -fill y -side right -before $w_t
213                 }
214         }
215         $sb set $first $last