1 # git-gui console support
2 # Copyright (C) 2006, 2007 Shawn Pearce
6 proc new_console {short_title long_title} {
7 global next_console_id console_data
8 set w .console[incr next_console_id]
9 set console_data($w) [list $short_title $long_title]
10 return [console_init $w]
13 proc console_init {w} {
14 global console_cr console_data M1B
16 set console_cr($w) 1.0
19 label $w.m.l1 -text "[lindex $console_data($w) 1]:" \
24 -background white -borderwidth 1 \
26 -width 80 -height 10 \
29 -yscrollcommand [list $w.m.sby set]
30 label $w.m.s -text {Working... please wait...} \
34 scrollbar $w.m.sby -command [list $w.m.t yview]
35 pack $w.m.l1 -side top -fill x
36 pack $w.m.s -side bottom -fill x
37 pack $w.m.sby -side right -fill y
38 pack $w.m.t -side left -fill both -expand 1
39 pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
41 menu $w.ctxm -tearoff 0
42 $w.ctxm add command -label "Copy" \
43 -command "tk_textCopy $w.m.t"
44 $w.ctxm add command -label "Select All" \
45 -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
46 $w.ctxm add command -label "Copy All" \
48 $w.m.t tag add sel 0.0 end
50 $w.m.t tag remove sel 0.0 end
53 button $w.ok -text {Close} \
56 pack $w.ok -side bottom -anchor e -pady 10 -padx 10
58 bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
59 bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
60 bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
61 bind $w <Visibility> "focus $w"
62 wm title $w "[appname] ([reponame]): [lindex $console_data($w) 0]"
66 proc console_exec {w cmd after} {
67 # -- Cygwin's Tcl tosses the enviroment when we exec our child.
68 # But most users need that so we have to relogin. :-(
71 set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
74 # -- Tcl won't let us redirect both stdout and stderr to
75 # the same pipe. So pass it through cat...
77 set cmd [concat | $cmd |& cat]
79 set fd_f [open $cmd r]
80 fconfigure $fd_f -blocking 0 -translation binary
81 fileevent $fd_f readable [list console_read $w $fd_f $after]
84 proc console_read {w fd after} {
89 if {![winfo exists $w]} {console_init $w}
90 $w.m.t conf -state normal
92 set n [string length $buf]
94 set cr [string first "\r" $buf $c]
95 set lf [string first "\n" $buf $c]
96 if {$cr < 0} {set cr [expr {$n + 1}]}
97 if {$lf < 0} {set lf [expr {$n + 1}]}
100 $w.m.t insert end [string range $buf $c $lf]
101 set console_cr($w) [$w.m.t index {end -1c}]
105 $w.m.t delete $console_cr($w) end
106 $w.m.t insert end "\n"
107 $w.m.t insert end [string range $buf $c $cr]
112 $w.m.t conf -state disabled
116 fconfigure $fd -blocking 1
118 if {[catch {close $fd}]} {
123 uplevel #0 $after $w $ok
126 fconfigure $fd -blocking 0
129 proc console_chain {cmdlist w {ok 1}} {
131 if {[llength $cmdlist] == 0} {
136 set cmd [lindex $cmdlist 0]
137 set cmdlist [lrange $cmdlist 1 end]
139 if {[lindex $cmd 0] eq {console_exec}} {
142 [list console_chain $cmdlist]
144 uplevel #0 $cmd $cmdlist $w $ok
151 proc console_done {args} {
152 global console_cr console_data
154 switch -- [llength $args] {
156 set w [lindex $args 0]
157 set ok [lindex $args 1]
160 set w [lindex $args 1]
161 set ok [lindex $args 2]
164 error "wrong number of args: console_done ?ignored? w ok"
169 if {[winfo exists $w]} {
170 $w.m.s conf -background green -text {Success}
171 $w.ok conf -state normal
175 if {![winfo exists $w]} {
178 $w.m.s conf -background red -text {Error: Command Failed}
179 $w.ok conf -state normal
183 array unset console_cr $w
184 array unset console_data $w