git-gui: Don't display CR within console windows
[git/git.git] / lib / console.tcl
CommitLineData
f522c9b5
SP
1# git-gui console support
2# Copyright (C) 2006, 2007 Shawn Pearce
3
c74b6c66
SP
4class console {
5
6field t_short
7field t_long
8field w
9field console_cr
ba1964be 10field is_toplevel 1; # are we our own window?
c74b6c66
SP
11
12constructor new {short_title long_title} {
13 set t_short $short_title
14 set t_long $long_title
15 _init $this
16 return $this
f522c9b5
SP
17}
18
ba1964be
SP
19constructor 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
26}
27
c74b6c66 28method _init {} {
a35d65d9 29 global M1B
ba1964be
SP
30
31 if {$is_toplevel} {
32 make_toplevel top w -autodelete 0
33 wm title $top "[appname] ([reponame]): $t_short"
34 } else {
35 frame $w
36 }
37
c74b6c66 38 set console_cr 1.0
f522c9b5 39
f522c9b5 40 frame $w.m
c74b6c66
SP
41 label $w.m.l1 \
42 -textvariable @t_long \
f522c9b5
SP
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 \
e87fb0f1 50 -wrap none \
f522c9b5
SP
51 -font font_diff \
52 -state disabled \
e87fb0f1 53 -xscrollcommand [list $w.m.sbx set] \
f522c9b5
SP
54 -yscrollcommand [list $w.m.sby set]
55 label $w.m.s -text {Working... please wait...} \
56 -anchor w \
57 -justify left \
58 -font font_uibold
e87fb0f1 59 scrollbar $w.m.sbx -command [list $w.m.t xview] -orient h
f522c9b5
SP
60 scrollbar $w.m.sby -command [list $w.m.t yview]
61 pack $w.m.l1 -side top -fill x
62 pack $w.m.s -side bottom -fill x
e87fb0f1 63 pack $w.m.sbx -side bottom -fill x
f522c9b5
SP
64 pack $w.m.sby -side right -fill y
65 pack $w.m.t -side left -fill both -expand 1
66 pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
67
68 menu $w.ctxm -tearoff 0
69 $w.ctxm add command -label "Copy" \
70 -command "tk_textCopy $w.m.t"
71 $w.ctxm add command -label "Select All" \
72 -command "focus $w.m.t;$w.m.t tag add sel 0.0 end"
73 $w.ctxm add command -label "Copy All" \
74 -command "
75 $w.m.t tag add sel 0.0 end
76 tk_textCopy $w.m.t
77 $w.m.t tag remove sel 0.0 end
78 "
79
ba1964be
SP
80 if {$is_toplevel} {
81 button $w.ok -text {Close} \
82 -state disabled \
83 -command [list destroy $w]
84 pack $w.ok -side bottom -anchor e -pady 10 -padx 10
85 bind $w <Visibility> [list focus $w]
86 }
f522c9b5
SP
87
88 bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
89 bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
90 bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
f522c9b5
SP
91}
92
c74b6c66 93method exec {cmd {after {}}} {
74c4763c
SP
94 if {[lindex $cmd 0] eq {git}} {
95 set fd_f [eval git_read --stderr [lrange $cmd 1 end]]
96 } else {
97 lappend cmd 2>@1
98 set fd_f [_open_stdout_stderr $cmd]
f522c9b5 99 }
f522c9b5 100 fconfigure $fd_f -blocking 0 -translation binary
c74b6c66 101 fileevent $fd_f readable [cb _read $fd_f $after]
f522c9b5
SP
102}
103
c74b6c66 104method _read {fd after} {
f522c9b5
SP
105 set buf [read $fd]
106 if {$buf ne {}} {
c74b6c66 107 if {![winfo exists $w.m.t]} {_init $this}
f522c9b5
SP
108 $w.m.t conf -state normal
109 set c 0
110 set n [string length $buf]
111 while {$c < $n} {
112 set cr [string first "\r" $buf $c]
113 set lf [string first "\n" $buf $c]
114 if {$cr < 0} {set cr [expr {$n + 1}]}
115 if {$lf < 0} {set lf [expr {$n + 1}]}
116
117 if {$lf < $cr} {
118 $w.m.t insert end [string range $buf $c $lf]
c74b6c66 119 set console_cr [$w.m.t index {end -1c}]
f522c9b5
SP
120 set c $lf
121 incr c
122 } else {
c74b6c66 123 $w.m.t delete $console_cr end
f522c9b5 124 $w.m.t insert end "\n"
bbbadf6e 125 $w.m.t insert end [string range $buf $c [expr {$cr - 1}]]
f522c9b5
SP
126 set c $cr
127 incr c
128 }
129 }
130 $w.m.t conf -state disabled
131 $w.m.t see end
132 }
133
134 fconfigure $fd -blocking 1
135 if {[eof $fd]} {
136 if {[catch {close $fd}]} {
137 set ok 0
138 } else {
139 set ok 1
140 }
a35d65d9 141 if {$after ne {}} {
c74b6c66 142 uplevel #0 $after $ok
a35d65d9 143 } else {
c74b6c66 144 done $this $ok
a35d65d9 145 }
f522c9b5
SP
146 return
147 }
148 fconfigure $fd -blocking 0
149}
150
c74b6c66 151method chain {cmdlist {ok 1}} {
f522c9b5
SP
152 if {$ok} {
153 if {[llength $cmdlist] == 0} {
c74b6c66 154 done $this $ok
f522c9b5
SP
155 return
156 }
157
158 set cmd [lindex $cmdlist 0]
159 set cmdlist [lrange $cmdlist 1 end]
160
a35d65d9 161 if {[lindex $cmd 0] eq {exec}} {
c74b6c66
SP
162 exec $this \
163 [lrange $cmd 1 end] \
164 [cb chain $cmdlist]
f522c9b5 165 } else {
c74b6c66 166 uplevel #0 $cmd [cb chain $cmdlist]
f522c9b5
SP
167 }
168 } else {
c74b6c66 169 done $this $ok
f522c9b5
SP
170 }
171}
172
d41b43eb
SP
173method insert {txt} {
174 if {![winfo exists $w.m.t]} {_init $this}
175 $w.m.t conf -state normal
176 $w.m.t insert end "$txt\n"
177 set console_cr [$w.m.t index {end -1c}]
178 $w.m.t conf -state disabled
179}
180
c74b6c66 181method done {ok} {
f522c9b5 182 if {$ok} {
c74b6c66 183 if {[winfo exists $w.m.s]} {
f522c9b5 184 $w.m.s conf -background green -text {Success}
ba1964be
SP
185 if {$is_toplevel} {
186 $w.ok conf -state normal
187 focus $w.ok
188 }
f522c9b5
SP
189 }
190 } else {
c74b6c66
SP
191 if {![winfo exists $w.m.s]} {
192 _init $this
f522c9b5
SP
193 }
194 $w.m.s conf -background red -text {Error: Command Failed}
ba1964be
SP
195 if {$is_toplevel} {
196 $w.ok conf -state normal
197 focus $w.ok
198 }
f522c9b5 199 }
c74b6c66 200 delete_this
f522c9b5 201}
a35d65d9
SP
202
203}