Git fork
at reftables-rust 223 lines 4.8 kB view raw
1# git-gui console support 2# Copyright (C) 2006, 2007 Shawn Pearce 3 4class console { 5 6field t_short 7field t_long 8field w 9field w_t 10field console_cr 11field is_toplevel 1; # are we our own window? 12 13constructor new {short_title long_title} { 14 set t_short $short_title 15 set t_long $long_title 16 _init $this 17 return $this 18} 19 20constructor 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 29method _init {} { 30 global M1B 31 32 if {$is_toplevel} { 33 make_dialog top w -autodelete 0 34 wm title $top "[appname] ([reponame]): $t_short" 35 } else { 36 ttk::frame $w 37 } 38 39 set console_cr 1.0 40 set w_t $w.m.t 41 42 ttk::frame $w.m 43 ttk::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 \ 50 -foreground black \ 51 -borderwidth 1 \ 52 -relief sunken \ 53 -width 80 -height 10 \ 54 -wrap none \ 55 -font font_diff \ 56 -state disabled \ 57 -xscrollcommand [cb _sb_set $w.m.sbx h] \ 58 -yscrollcommand [cb _sb_set $w.m.sby v] 59 label $w.m.s -text [mc "Working... please wait..."] \ 60 -anchor w \ 61 -justify left \ 62 -font font_uibold 63 pack $w.m.l1 -side top -fill x 64 pack $w.m.s -side bottom -fill x 65 pack $w_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 [mc "Copy"] \ 70 -command "tk_textCopy $w_t" 71 $w.ctxm add command -label [mc "Select All"] \ 72 -command "focus $w_t;$w_t tag add sel 0.0 end" 73 $w.ctxm add command -label [mc "Copy All"] \ 74 -command " 75 $w_t tag add sel 0.0 end 76 tk_textCopy $w_t 77 $w_t tag remove sel 0.0 end 78 " 79 80 if {$is_toplevel} { 81 ttk::button $w.ok -text [mc "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 } 87 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" 91} 92 93method exec {cmd {after {}}} { 94 if {[lindex $cmd 0] eq {git}} { 95 set fd_f [git_read [lrange $cmd 1 end] [list 2>@1]] 96 } else { 97 set fd_f [safe_open_command $cmd [list 2>@1]] 98 } 99 fconfigure $fd_f -blocking 0 -translation binary -encoding [encoding system] 100 fileevent $fd_f readable [cb _read $fd_f $after] 101} 102 103method _read {fd after} { 104 set buf [read $fd] 105 if {$buf ne {}} { 106 if {![winfo exists $w_t]} {_init $this} 107 $w_t conf -state normal 108 set c 0 109 set n [string length $buf] 110 while {$c < $n} { 111 set cr [string first "\r" $buf $c] 112 set lf [string first "\n" $buf $c] 113 if {$cr < 0} {set cr [expr {$n + 1}]} 114 if {$lf < 0} {set lf [expr {$n + 1}]} 115 116 if {$lf < $cr} { 117 $w_t insert end [string range $buf $c $lf] 118 set console_cr [$w_t index {end -1c}] 119 set c $lf 120 incr c 121 } else { 122 $w_t delete $console_cr end 123 $w_t insert end "\n" 124 $w_t insert end [string range $buf $c [expr {$cr - 1}]] 125 set c $cr 126 incr c 127 } 128 } 129 $w_t conf -state disabled 130 $w_t see end 131 } 132 133 fconfigure $fd -blocking 1 134 if {[eof $fd]} { 135 if {[catch {close $fd}]} { 136 set ok 0 137 } else { 138 set ok 1 139 } 140 if {$after ne {}} { 141 uplevel #0 $after $ok 142 } else { 143 done $this $ok 144 } 145 return 146 } 147 fconfigure $fd -blocking 0 148} 149 150method chain {cmdlist {ok 1}} { 151 if {$ok} { 152 if {[llength $cmdlist] == 0} { 153 done $this $ok 154 return 155 } 156 157 set cmd [lindex $cmdlist 0] 158 set cmdlist [lrange $cmdlist 1 end] 159 160 if {[lindex $cmd 0] eq {exec}} { 161 exec $this \ 162 [lrange $cmd 1 end] \ 163 [cb chain $cmdlist] 164 } else { 165 uplevel #0 $cmd [cb chain $cmdlist] 166 } 167 } else { 168 done $this $ok 169 } 170} 171 172method insert {txt} { 173 if {![winfo exists $w_t]} {_init $this} 174 $w_t conf -state normal 175 $w_t insert end "$txt\n" 176 set console_cr [$w_t index {end -1c}] 177 $w_t conf -state disabled 178} 179 180method done {ok} { 181 if {$ok} { 182 if {[winfo exists $w.m.s]} { 183 bind $w.m.s <Destroy> [list delete_this $this] 184 $w.m.s conf -background green -foreground black \ 185 -text [mc "Success"] 186 if {$is_toplevel} { 187 $w.ok conf -state normal 188 focus $w.ok 189 } 190 } else { 191 delete_this 192 } 193 } else { 194 if {![winfo exists $w.m.s]} { 195 _init $this 196 } 197 bind $w.m.s <Destroy> [list delete_this $this] 198 $w.m.s conf -background red -foreground black \ 199 -text [mc "Error: Command Failed"] 200 if {$is_toplevel} { 201 $w.ok conf -state normal 202 focus $w.ok 203 } 204 } 205 206 bind $w <Key-Escape> "destroy $w;break" 207} 208 209method _sb_set {sb orient first last} { 210 if {![winfo exists $sb]} { 211 if {$first == $last || ($first == 0 && $last == 1)} return 212 if {$orient eq {h}} { 213 ttk::scrollbar $sb -orient h -command [list $w_t xview] 214 pack $sb -fill x -side bottom -before $w_t 215 } else { 216 ttk::scrollbar $sb -orient v -command [list $w_t yview] 217 pack $sb -fill y -side right -before $w_t 218 } 219 } 220 $sb set $first $last 221} 222 223}