Repository navigation
Expand file tree
/
Copy pathrun.ahk
More file actions
404 lines (309 loc) · 11 KB
/
Copy pathrun.ahk
File metadata and controls
404 lines (309 loc) · 11 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#Requires AutoHotkey v2.0
#SingleInstance Force
#Warn
SetWorkingDir(A_ScriptDir)
AppName := "CodeHS Python AutoTyper"
SettingsPath := A_ScriptDir "\settings.ini"
StopRequested := false
IsTyping := false
TargetWindow := 0
MyGui := Gui(, AppName)
MyGui.MarginX := 14
MyGui.MarginY := 12
MyGui.SetFont("s9", "Segoe UI")
MyGui.Add("Text", "xm w680", "Paste your Python code below. Leading indentation is measured, but is not typed because CodeHS adds it automatically.")
MyGui.Add("Text", "xm", "Start delay (ms, 0-60000):")
StartDelayControl := MyGui.Add("Edit", "vStartDelay Number w160", "2000")
MyGui.Add("Text", "xm", "Typing speed (ms per character, 0-2000):")
TypingSpeedControl := MyGui.Add("Edit", "vTypingSpeed Number w160", "50")
MyGui.Add("Text", "xm", "Random extra delay (ms, 0-2000):")
TypingJitterControl := MyGui.Add("Edit", "vTypingJitter Number w160", "50")
MyGui.Add("Text", "xm", "Spaces per indentation level (1-8):")
IndentSizeControl := MyGui.Add("Edit", "vIndentSize Number w160", "4")
AlwaysOnTopControl := MyGui.Add("CheckBox", "vAlwaysOnTop xm", "Keep this window always on top")
MyGui.Add("Text", "xm", "Code to type:")
MyGui.SetFont("s10", "Consolas")
InputControl := MyGui.Add("Edit", "vInputText xm r18 w680 WantTab -Wrap HScroll")
MyGui.SetFont("s9", "Segoe UI")
StartButton := MyGui.Add("Button", "xm Default w120", "Start Typing")
SaveButton := MyGui.Add("Button", "x+10 w110", "Save Settings")
StatusText := MyGui.Add("Text", "xm w680", "Ready. F1 stops an active typing run.")
StartButton.OnEvent("Click", StartTyping)
SaveButton.OnEvent("Click", SaveSettings)
AlwaysOnTopControl.OnEvent("Click", ApplyAlwaysOnTop)
MyGui.OnEvent("Close", CloseApp)
LoadSettings()
ApplyAlwaysOnTop()
MyGui.Show()
return
#HotIf IsTypingNow()
F1::RequestStop()
#HotIf
StartTyping(*) {
global MyGui, AppName, StartButton, StatusText
global StopRequested, IsTyping, TargetWindow
if IsTyping
return
Settings := GetValidatedSettings()
if !IsObject(Settings)
return
if (Trim(Settings.InputText, " `t`r`n") = "") {
MsgBox("Enter some Python code before starting.", AppName, "Icon!")
return
}
StopRequested := false
IsTyping := true
TargetWindow := 0
StartButton.Enabled := false
StatusText.Text := "Waiting for the CodeHS editor..."
; Hiding the GUI activates the window beneath it. The configured delay gives
; the user time to click the CodeHS editor if a different window is active.
MyGui.Hide()
if !WaitForStartDelay(Settings.StartDelay) {
FinishTyping("stopped")
return
}
try TargetWindow := WinGetID("A")
catch
TargetWindow := 0
if (!TargetWindow || TargetWindow = MyGui.Hwnd) {
FinishTyping("no_target")
return
}
Result := ""
ErrorText := ""
try {
Result := TypeCode(Settings.InputText, Settings)
} catch as Err {
Result := "error"
ErrorText := Err.Message
}
FinishTyping(Result, ErrorText)
}
TypeCode(InputText, Settings) {
NormalizedText := StrReplace(InputText, "`r`n", "`n")
NormalizedText := StrReplace(NormalizedText, "`r", "`n")
Lines := StrSplit(NormalizedText, "`n")
LastIndentLevel := 0
for Index, Line in Lines {
State := CurrentRunState()
if (State != "")
return State
; Keep the indentation for comparison, but type only Content. This avoids
; adding source spaces on top of CodeHS's automatic indentation.
Content := LTrim(Line, " `t")
LeadingWhitespace := SubStr(Line, 1, StrLen(Line) - StrLen(Content))
; A blank line must not reset LastIndentLevel. Resetting it to zero would
; break the indentation of the next non-blank line in the same block.
if (Content = "") {
if (Index < Lines.Length) {
Send("{Enter}")
if !TypingPause(Max(25, Settings.TypingSpeed))
return CurrentRunState()
}
continue
}
IndentWidth := MeasureIndent(LeadingWhitespace, Settings.IndentSize)
CurrentIndentLevel := IndentWidth // Settings.IndentSize
; CodeHS performs indentation increases after Enter. Shift+Tab is sent
; only when the source code moves back one or more indentation levels.
if (CurrentIndentLevel < LastIndentLevel) {
DedentCount := LastIndentLevel - CurrentIndentLevel
Loop DedentCount {
Send("+{Tab}")
if !TypingPause(Max(25, Settings.TypingSpeed))
return CurrentRunState()
}
}
Loop Parse, Content {
State := CurrentRunState()
if (State != "")
return State
SendText(A_LoopField)
Delay := Settings.TypingSpeed
if (Settings.TypingJitter > 0)
Delay += Random(0, Settings.TypingJitter)
if !TypingPause(Delay)
return CurrentRunState()
}
LastIndentLevel := CurrentIndentLevel
; Send Enter only when the input contains another line. This prevents an
; unwanted newline from being added to input which does not end with one.
if (Index < Lines.Length) {
Send("{Enter}")
if !TypingPause(Max(25, Settings.TypingSpeed))
return CurrentRunState()
}
}
return "complete"
}
MeasureIndent(Whitespace, IndentSize) {
Columns := 0
Loop Parse, Whitespace {
if (A_LoopField = "`t")
Columns += IndentSize - Mod(Columns, IndentSize)
else
Columns += 1
}
return Columns
}
WaitForStartDelay(DelayMs) {
global StopRequested
EndTime := A_TickCount + DelayMs
LastSecond := -1
while (A_TickCount < EndTime) {
if StopRequested {
ToolTip()
return false
}
Remaining := EndTime - A_TickCount
Seconds := Ceil(Remaining / 1000)
if (Seconds != LastSecond) {
ToolTip("Focus the CodeHS editor.`nTyping starts in " Seconds " second(s).`nPress F1 to cancel.")
LastSecond := Seconds
}
Sleep(Min(50, Remaining))
}
ToolTip()
return !StopRequested
}
TypingPause(DelayMs) {
State := CurrentRunState()
if (State != "")
return false
if (DelayMs <= 0)
return true
EndTime := A_TickCount + DelayMs
while (A_TickCount < EndTime) {
State := CurrentRunState()
if (State != "")
return false
Sleep(Min(20, EndTime - A_TickCount))
}
return true
}
CurrentRunState() {
global StopRequested, TargetWindow
if StopRequested
return "stopped"
if (!TargetWindow || !WinActive("ahk_id " TargetWindow))
return "focus_lost"
return ""
}
FinishTyping(Result, ErrorText := "") {
global MyGui, StartButton, StatusText
global StopRequested, IsTyping, TargetWindow
ToolTip()
StopRequested := false
IsTyping := false
TargetWindow := 0
StartButton.Enabled := true
MyGui.Show()
ApplyAlwaysOnTop()
switch Result {
case "complete":
StatusText.Text := "Typing complete."
SoundBeep(1000, 120)
case "stopped":
StatusText.Text := "Typing stopped with F1."
case "focus_lost":
StatusText.Text := "Stopped because the target window lost focus."
case "no_target":
StatusText.Text := "No target window was selected. Try again and focus CodeHS during the countdown."
case "error":
StatusText.Text := "Typing stopped because of an error: " ErrorText
default:
StatusText.Text := "Typing stopped."
}
}
RequestStop(*) {
global StopRequested, IsTyping
if IsTyping
StopRequested := true
}
IsTypingNow() {
global IsTyping
return IsTyping
}
SaveSettings(*) {
global SettingsPath, AppName, StatusText
Settings := GetValidatedSettings()
if !IsObject(Settings)
return
IniWrite(Settings.StartDelay, SettingsPath, "Settings", "StartDelay")
IniWrite(Settings.TypingSpeed, SettingsPath, "Settings", "TypingSpeed")
IniWrite(Settings.TypingJitter, SettingsPath, "Settings", "TypingJitter")
IniWrite(Settings.IndentSize, SettingsPath, "Settings", "IndentSize")
IniWrite(Settings.AlwaysOnTop, SettingsPath, "Settings", "AlwaysOnTop")
ApplyAlwaysOnTop()
StatusText.Text := "Settings saved."
MsgBox("Settings saved.", AppName, "Iconi")
}
LoadSettings() {
global SettingsPath
global StartDelayControl, TypingSpeedControl, TypingJitterControl
global IndentSizeControl, AlwaysOnTopControl
StartDelayControl.Value := ReadIntegerSetting("StartDelay", 2000, 0, 60000)
TypingSpeedControl.Value := ReadIntegerSetting("TypingSpeed", 50, 0, 2000)
; TypingOffset keeps compatibility with the original settings.ini.
LegacyJitter := IniRead(SettingsPath, "Settings", "TypingOffset", "")
JitterDefault := RegExMatch(LegacyJitter, "^\d+$") ? LegacyJitter : 50
TypingJitterControl.Value := ReadIntegerSetting("TypingJitter", JitterDefault, 0, 2000)
IndentSizeControl.Value := ReadIntegerSetting("IndentSize", 4, 1, 8)
AlwaysOnTopControl.Value := ReadIntegerSetting("AlwaysOnTop", 1, 0, 1)
}
ReadIntegerSetting(Key, DefaultValue, Minimum, Maximum) {
global SettingsPath
Value := IniRead(SettingsPath, "Settings", Key, DefaultValue)
TextValue := Trim(Value)
if !RegExMatch(TextValue, "^\d+$")
return DefaultValue
NumericValue := TextValue + 0
if (NumericValue < Minimum || NumericValue > Maximum)
return DefaultValue
return NumericValue
}
GetValidatedSettings() {
global MyGui
Values := MyGui.Submit(false)
if !ValidateInteger(Values.StartDelay, "Start delay", 0, 60000, &StartDelay)
return false
if !ValidateInteger(Values.TypingSpeed, "Typing speed", 0, 2000, &TypingSpeed)
return false
if !ValidateInteger(Values.TypingJitter, "Random extra delay", 0, 2000, &TypingJitter)
return false
if !ValidateInteger(Values.IndentSize, "Indent size", 1, 8, &IndentSize)
return false
return {
StartDelay: StartDelay,
TypingSpeed: TypingSpeed,
TypingJitter: TypingJitter,
IndentSize: IndentSize,
AlwaysOnTop: Values.AlwaysOnTop ? 1 : 0,
InputText: Values.InputText
}
}
ValidateInteger(Value, Label, Minimum, Maximum, &Result) {
global AppName
TextValue := Trim(Value)
if !RegExMatch(TextValue, "^\d+$") {
MsgBox(Label " must be a whole number between " Minimum " and " Maximum ".", AppName, "Iconx")
return false
}
Result := TextValue + 0
if (Result < Minimum || Result > Maximum) {
MsgBox(Label " must be between " Minimum " and " Maximum ".", AppName, "Iconx")
return false
}
return true
}
ApplyAlwaysOnTop(*) {
global MyGui, AlwaysOnTopControl
if AlwaysOnTopControl.Value
MyGui.Opt("+AlwaysOnTop")
else
MyGui.Opt("-AlwaysOnTop")
}
CloseApp(*) {
ExitApp()
}