-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhopwatch_javascript.go
281 lines (276 loc) · 8.43 KB
/
hopwatch_javascript.go
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
// Copyright 2012+ ernestmicklei.com. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package hopwatch
import (
"io"
"net/http"
)
func js(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/javascript")
io.WriteString(w, `
var wsUri = "ws://" + window.location.hostname + ":" + window.location.port + "/hopwatch";
var output;
var websocket = null; // create at init
var connected = false;
var suspended = false;
var golineSize = 18;
function init() {
output = document.getElementById("output");
websocket = new WebSocket(wsUri);
setupWebSocket();
}
function setupWebSocket() {
websocket.addEventListener('open', onOpen);
websocket.addEventListener('close', onClose);
websocket.addEventListener('message', onMessage);
websocket.addEventListener('error', onError);
}
function onOpen(evt) {
connected = true;
document.getElementById("disconnect").className = "buttonEnabled";
writeToScreen("<-> connected","info mono");
sendConnected();
}
function onClose(evt) {
console.log("onClose:",evt);
handleDisconnected();
}
function onMessage(evt) {
try {
var cmd = JSON.parse(evt.data);
} catch (e) {
console.log('[hopwatch] failed to read valid JSON: ', message.data);
return;
}
// console.log("[hopwatch] received: " + evt.data);
if (cmd.Action == "display") {
var logdiv = document.createElement("div");
logdiv.className = "logline"
addTime(logdiv);
addGoline(logdiv,cmd);
addMessage(logdiv,watchParametersToHtml(cmd.Parameters),"watch mono");
output.appendChild(logdiv);
logdiv.scrollIntoView();
sendResume();
return;
}
if (cmd.Action == "print") {
var logdiv = document.createElement("div");
logdiv.className = "logline"
addTime(logdiv);
addGoline(logdiv,cmd);
addMessage(logdiv,cmd.Parameters["line"],"watch mono");
output.appendChild(logdiv);
logdiv.scrollIntoView();
sendResume();
return;
}
if (cmd.Action == "break") {
handleSuspended(cmd);
return;
}
}
function onError(evt) {
writeToScreen(evt,"err mono");
}
function handleSuspended(cmd) {
suspended = true;
document.getElementById("resume").className = "buttonEnabled";
var logdiv = document.createElement("div");
logdiv.className = "logline"
addTime(logdiv);
addGoline(logdiv,cmd);
var celldiv = addMessage(logdiv,"--> program suspended", "suspend mono");
addStack(celldiv,cmd);
output.appendChild(logdiv);
logdiv.scrollIntoView();
handleSourceUpdate(cmd);
}
function handleSourceUpdate(cmd) {
loadSource(cmd.Parameters["go.file"], cmd.Parameters["go.line"]);
}
function writeToScreen(text,cls) {
var logdiv = document.createElement("div");
logdiv.className = "logline"
addTime(logdiv);
addEmptiness(logdiv);
addMessage(logdiv,text,cls);
logdiv.scrollIntoView();
output.appendChild(logdiv);
}
function addTime(logdiv) {
var stamp = document.createElement("span");
stamp.innerHTML = timeHHMMSS();
stamp.className = "time mono"
logdiv.appendChild(stamp);
}
function addMessage(logdiv,msg,msgcls) {
var txt = document.createElement("span");
txt.className = msgcls
var escaped = "";
var arr = safe_tags(msg).split('\n');
for (var i = 0; i < arr.length; i++) {
if (i > 0) escaped += "\n "; // TODO remove hack
escaped += arr[i];
}
txt.innerHTML = escaped;
logdiv.appendChild(txt);
return txt;
}
function safe_tags(str) {
return str.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>') ;
}
function addEmptiness(logdiv) {
var empt = document.createElement("span");
empt.className = "goline"
empt.innerHTML = " ";
logdiv.appendChild(empt);
}
function addGoline(logdiv,cmd) {
var where = document.createElement("span");
where.className = "srcline"
var link = document.createElement("a");
link.href = "#";
link.className = "goline mono";
link.onclick = function() {
loadSource(cmd.Parameters["go.file"], cmd.Parameters["go.line"]);
};
link.innerHTML = goline(cmd.Parameters);
where.appendChild(link);
logdiv.appendChild(where);
}
function loadSource(fileName, nr) {
$("#gofile").html(shortenFileName(fileName));
$("#gosource-pane").show();
$.ajax({
url:"/gosource?file="+fileName
}).done(
function(responseText,status,xhr) {
handleSourceLoaded(responseText,nr);
}
);
}
function handleSourceLoaded(responseText,line) {
gosource = $("#gosource");
gosource.empty();
var breakElm
// Insert line numbers
var arr = responseText.split('\n');
for (var i = 0; i < arr.length; i++) {
var nr = i + 1
var buf = space_padded(nr) + arr[i];
var elm = document.createElement("div");
elm.innerHTML = buf;
if (line == nr) {
elm.className = "break";
breakElm = elm
}
gosource.append(elm)
}
breakElm.scrollIntoView();
}
function space_padded(i) {
var buf = "" + i
if (i<1000) { buf += " " }
if (i<100) { buf += " " }
if (i<10) { buf += " " }
return buf
}
function shortenFileName(fileName) {
return fileName.length > 48 ? "..." + fileName.substring(fileName.length - 48) : fileName;
}
function addStack(celldiv,cmd) {
var stack = cmd.Parameters["go.stack"];
if (stack != null && stack.length > 0) {
addNonEmptyStackTo(stack,celldiv);
}
}
function addNonEmptyStackTo(stack,celldiv) {
var toggle = document.createElement("a");
toggle.href = "#";
toggle.className = "toggle";
toggle.onclick = function() { toggleStack(toggle); };
toggle.innerHTML="stack ▶";
celldiv.appendChild(toggle);
var stk = document.createElement("div");
stk.style.display = "none";
var lines = document.createElement("pre");
lines.innerHTML = stack
lines.className = "stack mono"
stk.appendChild(lines)
celldiv.appendChild(stk)
}
function toggleStack(link) {
var stack = link.nextSibling;
if (stack.style.display == "none") {
link.innerHTML = "stack ▼";
stack.style.display = "block"
stack.scrollIntoView();
} else {
link.innerHTML = "stack ▶";
stack.style.display = "none";
}
}
// http://www.quirksmode.org/js/keys.html
function handleKeyDown(event) {
if (event.keyCode == 119) {
actionResume();
}
}
function watchParametersToHtml(parameters) {
var line = "";
var multiline = false;
for (var prop in parameters) {
if (prop.slice(0,3) != "go.") {
if (multiline) { line = line + ", "; }
line = line + prop + "=" + parameters[prop];
multiline = true;
}
}
return line
}
function goline(parameters) {
var f = parameters["go.file"]
f = f.substr(f.lastIndexOf("/")+1)
var padded = f + ":" + parameters["go.line"]
while (padded.length > golineSize) {
golineSize += 4
}
for (i=padded.length;i<golineSize;i++) padded += " "
return padded
}
function actionResume() {
if (!connected) return;
if (!suspended) return;
suspended = false;
document.getElementById("resume").className = "buttonDisabled";
// writeToScreen("<-- resume program","info mono");
sendResume();
}
function actionDisconnect() {
if (!connected) return;
connected = false;
document.getElementById("disconnect").className = "buttonDisabled";
sendQuit();
writeToScreen("<-- disconnect requested","info mono");
websocket.close(); // seems not to trigger close on Go-side ; so handleDisconnected cannot be used here
}
function handleDisconnected() {
connected = false;
document.getElementById("resume").className = "buttonDisabled";
document.getElementById("disconnect").className = "buttonDisabled";
writeToScreen(">-< disconnected","info mono");
}
function timeHHMMSS() { return new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1"); }
function sendConnected() { doSend('{"Action":"connected"}'); }
function sendResume() { doSend('{"Action":"resume"}'); }
function sendQuit() { doSend('{"Action":"quit"}'); }
function doSend(message) {
// console.log("[hopwatch] send: " + message);
websocket.send(message);
}
window.addEventListener("load", init, false);
window.addEventListener("keydown", handleKeyDown, true); `)
return
}