Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 1 | # git-gui console support |
| 2 | # Copyright (C) 2006, 2007 Shawn Pearce |
| 3 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 4 | class console { |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 5 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 6 | field t_short |
| 7 | field t_long |
| 8 | field w |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 9 | field w_t |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 10 | field console_cr |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 11 | field is_toplevel 1; # are we our own window? |
Shawn O. Pearce | a35d65d | 2007-05-01 02:59:53 -0400 | [diff] [blame] | 12 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 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 |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 18 | } |
| 19 | |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 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 | } |
| 28 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 29 | method _init {} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 30 | global M1B use_ttk NS |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 31 | |
| 32 | if {$is_toplevel} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 33 | make_dialog top w -autodelete 0 |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 34 | wm title $top "[appname] ([reponame]): $t_short" |
| 35 | } else { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 36 | ${NS}::frame $w |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 39 | set console_cr 1.0 |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 40 | set w_t $w.m.t |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 41 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 42 | ${NS}::frame $w.m |
| 43 | ${NS}::label $w.m.l1 \ |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 44 | -textvariable @t_long \ |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 45 | -anchor w \ |
| 46 | -justify left \ |
| 47 | -font font_uibold |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 48 | text $w_t \ |
Philipp A. Hartmann | c382fdd | 2008-03-05 17:54:22 +0100 | [diff] [blame] | 49 | -background white \ |
| 50 | -foreground black \ |
| 51 | -borderwidth 1 \ |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 52 | -relief sunken \ |
| 53 | -width 80 -height 10 \ |
Shawn O. Pearce | e87fb0f | 2007-07-09 11:14:00 -0400 | [diff] [blame] | 54 | -wrap none \ |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 55 | -font font_diff \ |
| 56 | -state disabled \ |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 57 | -xscrollcommand [cb _sb_set $w.m.sbx h] \ |
| 58 | -yscrollcommand [cb _sb_set $w.m.sby v] |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 59 | label $w.m.s -text [mc "Working... please wait..."] \ |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 60 | -anchor w \ |
| 61 | -justify left \ |
| 62 | -font font_uibold |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 63 | pack $w.m.l1 -side top -fill x |
| 64 | pack $w.m.s -side bottom -fill x |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 65 | pack $w_t -side left -fill both -expand 1 |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 66 | pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10 |
| 67 | |
| 68 | menu $w.ctxm -tearoff 0 |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 69 | $w.ctxm add command -label [mc "Copy"] \ |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 70 | -command "tk_textCopy $w_t" |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 71 | $w.ctxm add command -label [mc "Select All"] \ |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 72 | -command "focus $w_t;$w_t tag add sel 0.0 end" |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 73 | $w.ctxm add command -label [mc "Copy All"] \ |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 74 | -command " |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 75 | $w_t tag add sel 0.0 end |
| 76 | tk_textCopy $w_t |
| 77 | $w_t tag remove sel 0.0 end |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 78 | " |
| 79 | |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 80 | if {$is_toplevel} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 81 | ${NS}::button $w.ok -text [mc "Close"] \ |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 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 | } |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 87 | |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 88 | bind_button3 $w_t "tk_popup $w.ctxm %X %Y" |
| 89 | bind $w_t <$M1B-Key-a> "$w_t tag add sel 0.0 end;break" |
| 90 | bind $w_t <$M1B-Key-A> "$w_t tag add sel 0.0 end;break" |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 93 | method exec {cmd {after {}}} { |
Shawn O. Pearce | 74c4763 | 2007-07-09 03:07:05 -0400 | [diff] [blame] | 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] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 99 | } |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 100 | fconfigure $fd_f -blocking 0 -translation binary |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 101 | fileevent $fd_f readable [cb _read $fd_f $after] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 104 | method _read {fd after} { |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 105 | set buf [read $fd] |
| 106 | if {$buf ne {}} { |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 107 | if {![winfo exists $w_t]} {_init $this} |
| 108 | $w_t conf -state normal |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 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} { |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 118 | $w_t insert end [string range $buf $c $lf] |
| 119 | set console_cr [$w_t index {end -1c}] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 120 | set c $lf |
| 121 | incr c |
| 122 | } else { |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 123 | $w_t delete $console_cr end |
| 124 | $w_t insert end "\n" |
Shawn O. Pearce | c9dcc7f | 2007-10-20 20:43:36 -0400 | [diff] [blame] | 125 | $w_t insert end [string range $buf $c [expr {$cr - 1}]] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 126 | set c $cr |
| 127 | incr c |
| 128 | } |
| 129 | } |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 130 | $w_t conf -state disabled |
| 131 | $w_t see end |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 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 | } |
Shawn O. Pearce | a35d65d | 2007-05-01 02:59:53 -0400 | [diff] [blame] | 141 | if {$after ne {}} { |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 142 | uplevel #0 $after $ok |
Shawn O. Pearce | a35d65d | 2007-05-01 02:59:53 -0400 | [diff] [blame] | 143 | } else { |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 144 | done $this $ok |
Shawn O. Pearce | a35d65d | 2007-05-01 02:59:53 -0400 | [diff] [blame] | 145 | } |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 146 | return |
| 147 | } |
| 148 | fconfigure $fd -blocking 0 |
| 149 | } |
| 150 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 151 | method chain {cmdlist {ok 1}} { |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 152 | if {$ok} { |
| 153 | if {[llength $cmdlist] == 0} { |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 154 | done $this $ok |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 155 | return |
| 156 | } |
| 157 | |
| 158 | set cmd [lindex $cmdlist 0] |
| 159 | set cmdlist [lrange $cmdlist 1 end] |
| 160 | |
Shawn O. Pearce | a35d65d | 2007-05-01 02:59:53 -0400 | [diff] [blame] | 161 | if {[lindex $cmd 0] eq {exec}} { |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 162 | exec $this \ |
| 163 | [lrange $cmd 1 end] \ |
| 164 | [cb chain $cmdlist] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 165 | } else { |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 166 | uplevel #0 $cmd [cb chain $cmdlist] |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 167 | } |
| 168 | } else { |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 169 | done $this $ok |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 173 | method insert {txt} { |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 174 | if {![winfo exists $w_t]} {_init $this} |
| 175 | $w_t conf -state normal |
| 176 | $w_t insert end "$txt\n" |
| 177 | set console_cr [$w_t index {end -1c}] |
| 178 | $w_t conf -state disabled |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 181 | method done {ok} { |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 182 | if {$ok} { |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 183 | if {[winfo exists $w.m.s]} { |
Shawn O. Pearce | 6f2d73e | 2007-09-26 14:05:54 -0400 | [diff] [blame] | 184 | bind $w.m.s <Destroy> [list delete_this $this] |
Philipp A. Hartmann | c382fdd | 2008-03-05 17:54:22 +0100 | [diff] [blame] | 185 | $w.m.s conf -background green -foreground black \ |
| 186 | -text [mc "Success"] |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 187 | if {$is_toplevel} { |
| 188 | $w.ok conf -state normal |
| 189 | focus $w.ok |
| 190 | } |
Shawn O. Pearce | 6f2d73e | 2007-09-26 14:05:54 -0400 | [diff] [blame] | 191 | } else { |
| 192 | delete_this |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 193 | } |
| 194 | } else { |
Shawn O. Pearce | c74b6c6 | 2007-05-08 20:33:47 -0400 | [diff] [blame] | 195 | if {![winfo exists $w.m.s]} { |
| 196 | _init $this |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 197 | } |
Shawn O. Pearce | 6f2d73e | 2007-09-26 14:05:54 -0400 | [diff] [blame] | 198 | bind $w.m.s <Destroy> [list delete_this $this] |
Philipp A. Hartmann | c382fdd | 2008-03-05 17:54:22 +0100 | [diff] [blame] | 199 | $w.m.s conf -background red -foreground black \ |
| 200 | -text [mc "Error: Command Failed"] |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 201 | if {$is_toplevel} { |
| 202 | $w.ok conf -state normal |
| 203 | focus $w.ok |
| 204 | } |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 205 | } |
Shawn O. Pearce | f522c9b | 2007-05-07 23:35:48 -0400 | [diff] [blame] | 206 | } |
Shawn O. Pearce | a35d65d | 2007-05-01 02:59:53 -0400 | [diff] [blame] | 207 | |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 208 | method _sb_set {sb orient first last} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 209 | global NS |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 210 | if {![winfo exists $sb]} { |
Shawn O. Pearce | 59213f6 | 2007-09-26 14:16:45 -0400 | [diff] [blame] | 211 | if {$first == $last || ($first == 0 && $last == 1)} return |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 212 | if {$orient eq {h}} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 213 | ${NS}::scrollbar $sb -orient h -command [list $w_t xview] |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 214 | pack $sb -fill x -side bottom -before $w_t |
| 215 | } else { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 216 | ${NS}::scrollbar $sb -orient v -command [list $w_t yview] |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 217 | pack $sb -fill y -side right -before $w_t |
| 218 | } |
| 219 | } |
Shawn O. Pearce | 59213f6 | 2007-09-26 14:16:45 -0400 | [diff] [blame] | 220 | $sb set $first $last |
Shawn O. Pearce | fbc8a93 | 2007-09-23 05:25:13 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Shawn O. Pearce | a35d65d | 2007-05-01 02:59:53 -0400 | [diff] [blame] | 223 | } |