-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrunner
executable file
·204 lines (174 loc) · 5.33 KB
/
tkrunner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#! /usr/bin/env wish
#
# Copyright (c) 2020 Peter Piwowarski <peterjpiwowarski@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# {*} and ttk
package require Tcl 8.5
package require Tk 8.5
set cmd {}
set history {}
# Relative to the end; an hpointer of 1 corresponds to a list index of end-1.
# Note that [lindex /foo/ end--1] is the empty string.
set hpointer -1
interp create runner
interp eval runner {
package require trun
namespace path [concat [namespace path] ::tcl::mathop ::tcl::mathfunc ::trun]
namespace unknown ::trun::unknown
namespace eval Tkrunner {
variable hide_handle {}
}
trace add execution trun::unknown leave {apply { {cmd code result op} {
if {!$code} {
set Tkrunner::hide_handle [after 2000 hide]
}
}}}
}
interp alias runner hide {} hide
# apply padding to a list of grid-ed widgets
proc pad_grid_widgets {widgets {amt 1}} {
set amt [expr {ceil($amt * [tk scaling])}]
foreach widget $widgets {
grid configure $widget -padx $amt -pady $amt
}
}
# Run a command, properly adding it to the history.
proc run {cmd} {
if {$::history ne ""} {
.tkrunner.f.msg ins end \n
}
lappend ::history $cmd
set n [llength $::history]
.tkrunner.f.hc configure -text [expr {$n + 1}]
.tkrunner.f.msg ins end "$n: [try {
interp eval runner $cmd
} on error {err opts} {
expr {"error: $err"}
}]" history_line
.tkrunner.f.msg see end
set ::hpointer -1
}
# move a window under the mouse
proc snap2mouse {w} {
set xpos [expr {[winfo pointerx $w] - ([winfo width $w] / 2)}]
set ypos [expr {[winfo pointery $w] - ([winfo height $w] / 2)}]
wm geometry $w +$xpos+$ypos
}
# define (if necessary) and show the "Run Command" toplevel
proc show {} {
if {[winfo exists .tkrunner]} {
wm withdraw .tkrunner
wm deiconify .tkrunner
# Cancel any pending hide action.
interp eval runner {
after cancel $Tkrunner::hide_handle
}
} else {
set ::cmd {}
# Most themes have buttons that are just too large.
ttk::style configure Run.TKRunner.TButton -padding 1
toplevel .tkrunner
wm title .tkrunner "Run Command"
wm attributes .tkrunner -topmost yes
ttk::frame .tkrunner.f
ttk::label .tkrunner.f.hc -text 1 -width 2
ttk::entry .tkrunner.f.cmdline -textvariable ::cmd
ttk::button .tkrunner.f.run -text Run -width 0 -command {
run $::cmd
set ::cmd {}
} -style Run.TKRunner.TButton
text .tkrunner.f.msg -width 80 -height 4 -wrap char \
-yscrollcommand { .tkrunner.f.scr set }
.tkrunner.f.msg tag configure history_line \
-lmargin2 [expr {[tk scaling] * 8}]
# raw read-only wrapper inspired by the example at
# https://wiki.tcl-lang.org/page/Read%2Donly+text+widget
rename .tkrunner.f.msg @internal.tkrunner.f.msg
proc .tkrunner.f.msg {args} {
switch -- [lindex $args 0] {
insert {}
delete {}
replace {}
ins {
return [@internal.tkrunner.f.msg insert {*}[lrange $args 1 end]]
}
default {
return [@internal.tkrunner.f.msg {*}$args]
}
}
}
ttk::scrollbar .tkrunner.f.scr -orient vertical -command {
.tkrunner.f.msg yview
}
grid .tkrunner.f.hc .tkrunner.f.cmdline .tkrunner.f.run - -sticky ew
grid .tkrunner.f.msg - - .tkrunner.f.scr -sticky nsew
grid columnconfigure .tkrunner.f 1 -weight 1
grid rowconfigure .tkrunner.f 1 -weight 1
pad_grid_widgets [winfo children .tkrunner.f]
pack .tkrunner.f -expand y -fill both
wm attributes .tkrunner -topmost yes
focus .tkrunner.f.cmdline
bind .tkrunner.f.cmdline <KeyPress-Up> {
set n [llength $::history]
if {$::hpointer < ($n - 1)} {
incr ::hpointer
.tkrunner.f.hc configure -text [expr {$n - $::hpointer}]
}
set ::cmd [lindex $::history end-$::hpointer]
%W icursor end
}
bind .tkrunner.f.cmdline <KeyPress-Down> {
if {$::hpointer >= 0} {
set ::cmd [lindex $::history end-[incr ::hpointer -1]]
.tkrunner.f.hc configure -text [expr {$n - $::hpointer}]
} else {
set ::cmd {}
.tkrunner.f.hc configure -text [expr {$n + 1}]
}
%W icursor end
}
foreach key {Return KP_Enter} {
bind .tkrunner <KeyPress-$key> { .tkrunner.f.run invoke }
}
bind .tkrunner <KeyPress-Escape> { hide }
bind .tkrunner <Map> {
snap2mouse .tkrunner
focus -force .tkrunner.f.cmdline
}
wm protocol .tkrunner WM_DELETE_WINDOW { hide }
}
}
# hide the "Run Command" toplevel
proc hide {} { wm withdraw .tkrunner }
if {[tk appname tkrunner] ne "tkrunner"} {
# Tell the already-running tkrunner instance to show its "Run Command"
# toplevel.
send tkrunner show
exit
} else {
wm withdraw .
set quiet no
foreach arg $argv {
switch -- $arg {
-quiet { set quiet yes }
default {
puts "error: unknown option $arg"
exit
}
}
}
if {!$quiet} {
show
}
}