git-gui: Teach console widget to use git_read
[git-gui/me-and.git] / lib / console.tcl
blob03d0354d5ee4934b93784e949e07c694bd325236
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
10 field is_toplevel 1; # are we our own window?
12 constructor new {short_title long_title} {
13 set t_short $short_title
14 set t_long $long_title
15 _init $this
16 return $this
19 constructor embed {path title} {
20 set t_short {}
21 set t_long $title
22 set w $path
23 set is_toplevel 0
24 _init $this
25 return $this
28 method _init {} {
29 global M1B
31 if {$is_toplevel} {
32 make_toplevel top w -autodelete 0
33 wm title $top "[appname] ([reponame]): $t_short"
34 } else {
35 frame $w
38 set console_cr 1.0
40 frame $w.m
41 label $w.m.l1 \
42 -textvariable @t_long \
43 -anchor w \
44 -justify left \
45 -font font_uibold
46 text $w.m.t \
47 -background white -borderwidth 1 \
48 -relief sunken \
49 -width 80 -height 10 \
50 -font font_diff \
51 -state disabled \
52 -yscrollcommand [list $w.m.sby set]
53 label $w.m.s -text {Working... please wait...} \
54 -anchor w \
55 -justify left \
56 -font font_uibold
57 scrollbar $w.m.sby -command [list $w.m.t yview]
58 pack $w.m.l1 -side top -fill x
59 pack $w.m.s -side bottom -fill x
60 pack $w.m.sby -side right -fill y
61 pack $w.m.t -side left -fill both -expand 1
62 pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
64 menu $w.ctxm -tearoff 0
65 $w.ctxm add command -label "Copy" \
66 -command "tk_textCopy $w.m.t"
67 $w.ctxm add command -label "Select All" \
68 -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
69 $w.ctxm add command -label "Copy All" \
70 -command "
71 $w.m.t tag add sel 0.0 end
72 tk_textCopy $w.m.t
73 $w.m.t tag remove sel 0.0 end
76 if {$is_toplevel} {
77 button $w.ok -text {Close} \
78 -state disabled \
79 -command [list destroy $w]
80 pack $w.ok -side bottom -anchor e -pady 10 -padx 10
81 bind $w <Visibility> [list focus $w]
84 bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
85 bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
86 bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
89 method exec {cmd {after {}}} {
90 if {[lindex $cmd 0] eq {git}} {
91 set fd_f [eval git_read --stderr [lrange $cmd 1 end]]
92 } else {
93 lappend cmd 2>@1
94 set fd_f [_open_stdout_stderr $cmd]
96 fconfigure $fd_f -blocking 0 -translation binary
97 fileevent $fd_f readable [cb _read $fd_f $after]
100 method _read {fd after} {
101 set buf [read $fd]
102 if {$buf ne {}} {
103 if {![winfo exists $w.m.t]} {_init $this}
104 $w.m.t conf -state normal
105 set c 0
106 set n [string length $buf]
107 while {$c < $n} {
108 set cr [string first "\r" $buf $c]
109 set lf [string first "\n" $buf $c]
110 if {$cr < 0} {set cr [expr {$n + 1}]}
111 if {$lf < 0} {set lf [expr {$n + 1}]}
113 if {$lf < $cr} {
114 $w.m.t insert end [string range $buf $c $lf]
115 set console_cr [$w.m.t index {end -1c}]
116 set c $lf
117 incr c
118 } else {
119 $w.m.t delete $console_cr end
120 $w.m.t insert end "\n"
121 $w.m.t insert end [string range $buf $c $cr]
122 set c $cr
123 incr c
126 $w.m.t conf -state disabled
127 $w.m.t see end
130 fconfigure $fd -blocking 1
131 if {[eof $fd]} {
132 if {[catch {close $fd}]} {
133 set ok 0
134 } else {
135 set ok 1
137 if {$after ne {}} {
138 uplevel #0 $after $ok
139 } else {
140 done $this $ok
142 return
144 fconfigure $fd -blocking 0
147 method chain {cmdlist {ok 1}} {
148 if {$ok} {
149 if {[llength $cmdlist] == 0} {
150 done $this $ok
151 return
154 set cmd [lindex $cmdlist 0]
155 set cmdlist [lrange $cmdlist 1 end]
157 if {[lindex $cmd 0] eq {exec}} {
158 exec $this \
159 [lrange $cmd 1 end] \
160 [cb chain $cmdlist]
161 } else {
162 uplevel #0 $cmd [cb chain $cmdlist]
164 } else {
165 done $this $ok
169 method insert {txt} {
170 if {![winfo exists $w.m.t]} {_init $this}
171 $w.m.t conf -state normal
172 $w.m.t insert end "$txt\n"
173 set console_cr [$w.m.t index {end -1c}]
174 $w.m.t conf -state disabled
177 method done {ok} {
178 if {$ok} {
179 if {[winfo exists $w.m.s]} {
180 $w.m.s conf -background green -text {Success}
181 if {$is_toplevel} {
182 $w.ok conf -state normal
183 focus $w.ok
186 } else {
187 if {![winfo exists $w.m.s]} {
188 _init $this
190 $w.m.s conf -background red -text {Error: Command Failed}
191 if {$is_toplevel} {
192 $w.ok conf -state normal
193 focus $w.ok
196 delete_this