Skip to content

Commit f97e19f

Browse files
committed
more opt-live sharpening
1 parent e07d857 commit f97e19f

5 files changed

Lines changed: 119 additions & 33 deletions

File tree

‎v3/css/opt-live.css‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@
66
background-image: url("lightgreenarrow.png");
77
}
88

9+
.curPrevOverlapLineStepGutter {
10+
background-image: url("redgreenarrow.png");
11+
}
12+
13+
.errorLine {
14+
position: absolute;
15+
z-index: 20;
16+
border-top: 1px solid #e93f34;
17+
border-bottom: 1px solid #e93f34;
18+
/*background: rgba(200, 0, 0, 0.15);*/
19+
}
20+
21+
#sliderStuff {
22+
padding-top: 3px;
23+
}
24+
25+
#curInstr {
26+
padding: 6px;
27+
margin: 6px auto;
28+
text-align: center;
29+
}
30+
931
/* translucent dimmed div: http://stackoverflow.com/a/21457838 */
1032
.dimmed {
1133
position: relative;
@@ -20,7 +42,7 @@
2042
top: 0;
2143
left: 0;
2244
right: 0;
23-
background: rgba(0, 0, 0, 0.25);
45+
background: rgba(200, 0, 0, 0.15);
2446
}
2547

2648
/* augment */

‎v3/css/redgreenarrow.png‎

6.86 KB
Loading

‎v3/js/opt-live.js‎

Lines changed: 85 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,29 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3333

3434
/* TODOs:
3535
36-
- detect exact position of syntax error and put a squiggly line below it
37-
with something like:
36+
- [later] detect exact position of syntax error and put a squiggly line below
37+
it with something like:
3838
3939
File "<string>", line 1
4040
x~=1
4141
^
42+
- make Ace editor resizable width-wise using jQuery resizable
43+
(stackoverflow has some tips for how to do that)
4244
43-
- [later] add a codeopticon-style history slider of the user's past
44-
edits
45+
- add toggles for language choices
46+
47+
- support pasting in code via URL, which will be important for
48+
transporting the user from regular OPT to live mode
4549
46-
- slightly gray out the visualization when there's a syntax error, to
47-
indicate to user that it's not indicative of the current code
50+
- make sure server logging works properly, esp. session and user IDs,
51+
and slider interactions
52+
- have separate web_exec_live executor scripts for logging live mode
4853
49-
- display run-time exceptions properly -- right now display-mode code is
50-
hidden, so no exceptions displayed either
54+
- if these Ace enhancements look good, then I can also use them for
55+
Codeopticon as well!
56+
57+
- [later] add a codeopticon-style history slider of the user's past
58+
edits (but that might be confusing)
5159
5260
*/
5361

@@ -60,28 +68,42 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6068

6169
var originFrontendJsFile = 'opt-live.js';
6270

71+
var prevVisualizer = null; // the visualizer object from the previous execution
72+
73+
var aceEditorWidth = '600px';
74+
75+
var allMarkerIds = [];
76+
6377
function removeAllGutterDecorations() {
6478
var s = pyInputAceEditor.getSession();
6579
var d = s.getDocument();
6680

6781
for (var i = 0; i < d.getLength(); i++) {
6882
s.removeGutterDecoration(i, 'curLineStepGutter');
6983
s.removeGutterDecoration(i, 'prevLineStepGutter');
84+
s.removeGutterDecoration(i, 'curPrevOverlapLineStepGutter');
7085
}
7186
}
7287

73-
7488
function updateStepLabels() {
7589
assert(myVisualizer);
90+
myVisualizer.updateCurPrevLines(); // do this first to update the right fields
91+
92+
var s = pyInputAceEditor.getSession();
93+
allMarkerIds.forEach(function(e) {
94+
s.removeMarker(e);
95+
});
96+
allMarkerIds = [];
97+
7698
var totalInstrs = myVisualizer.curTrace.length;
77-
var isLastInstr = myVisualizer.curInstr == (totalInstrs-1);
99+
var isLastInstr = myVisualizer.curInstr === (totalInstrs-1);
78100
if (isLastInstr) {
79101
if (myVisualizer.promptForUserInput || myVisualizer.promptForMouseInput) {
80102
$("#curInstr").html('<b><font color="' + brightRed + '">Enter user input:</font></b>');
81103
} else if (myVisualizer.instrLimitReached) {
82-
$("#curInstr").html("Instruction limit reached");
104+
$("#curInstr").html("Instruction limit reached (" + String(totalInstrs-1) + " steps)");
83105
} else {
84-
$("#curInstr").html("Program terminated");
106+
$("#curInstr").html("Program terminated (" + String(totalInstrs-1) + " steps)");
85107
}
86108
} else {
87109
$("#curInstr").html("Step " + String(myVisualizer.curInstr + 1) + " of " + String(totalInstrs-1));
@@ -98,20 +120,32 @@ function updateStepLabels() {
98120
$("#frontendErrorOutput").html(htmlspecialchars(curEntry.exception_msg));
99121
}
100122
$("#frontendErrorOutput").show();
123+
124+
if (myVisualizer.curLineNumber) {
125+
var Range = ace.require('ace/range').Range;
126+
var markerId = s.addMarker(new Range(myVisualizer.curLineNumber - 1, 0,
127+
myVisualizer.curLineNumber - 1, 1), "errorLine", "fullLine");
128+
allMarkerIds.push(markerId);
129+
}
130+
} else {
131+
$("#frontendErrorOutput").html(''); // clear it
101132
}
102133

103134
removeAllGutterDecorations();
104135

105-
// update gutter arrows
106-
myVisualizer.updateCurPrevLines();
107-
108-
var s = pyInputAceEditor.getSession();
109-
if (myVisualizer.curLineNumber) {
110-
s.addGutterDecoration(myVisualizer.curLineNumber-1, 'curLineStepGutter');
111-
}
112-
113-
if (myVisualizer.prevLineNumber) {
114-
s.addGutterDecoration(myVisualizer.prevLineNumber-1, 'prevLineStepGutter');
136+
// special case if both arrows overlap
137+
if ( myVisualizer.curLineNumber &&
138+
(myVisualizer.curLineNumber === myVisualizer.prevLineNumber)) {
139+
s.addGutterDecoration(myVisualizer.curLineNumber-1,
140+
'curPrevOverlapLineStepGutter');
141+
} else {
142+
// render separately
143+
if (myVisualizer.curLineNumber) {
144+
s.addGutterDecoration(myVisualizer.curLineNumber-1, 'curLineStepGutter');
145+
}
146+
if (myVisualizer.prevLineNumber) {
147+
s.addGutterDecoration(myVisualizer.prevLineNumber-1, 'prevLineStepGutter');
148+
}
115149
}
116150
}
117151

@@ -120,7 +154,7 @@ function optliveFinishSuccessfulExecution() {
120154
$("#pyOutputPane").show();
121155
doneExecutingCode();
122156

123-
$("#dataViz").removeClass('dimmed'); // un-dim the visualization
157+
$("#dataViz,#curInstr").removeClass('dimmed'); // un-dim the visualization
124158

125159
// set up execution slider, code inspired by pytutor.js:
126160
var sliderDiv = $('#executionSlider');
@@ -154,7 +188,25 @@ function optliveFinishSuccessfulExecution() {
154188

155189
// do this AFTER making #pyOutputPane visible, or else
156190
// jsPlumb connectors won't render properly
157-
myVisualizer.updateOutput();
191+
192+
// try to "match" the same position as the previous visualizer so that
193+
// the display isn't jerky
194+
if (prevVisualizer) {
195+
var prevVizInstr = prevVisualizer.curInstr;
196+
var prevVizIsFinalInstr = (prevVisualizer.curInstr === (prevVisualizer.curTrace.length-1));
197+
198+
// match the previous step if it we weren't on the last one, and the new
199+
// trace is at least as long
200+
if (!prevVizIsFinalInstr &&
201+
(myVisualizer.curTrace.length >= prevVisualizer.curTrace.length)) {
202+
myVisualizer.renderStep(prevVizInstr);
203+
} else {
204+
myVisualizer.updateOutput();
205+
}
206+
} else {
207+
myVisualizer.updateOutput();
208+
}
209+
158210
updateStepLabels(); // do it once
159211
}
160212

@@ -163,7 +215,12 @@ function optliveHandleUncaughtExceptionFunc(trace) {
163215
var errorLineNo = trace[0].line - 1; /* CodeMirror lines are zero-indexed */
164216
if (errorLineNo !== undefined && errorLineNo != NaN) {
165217
removeAllGutterDecorations();
166-
$("#dataViz").addClass('dimmed'); // dim the visualization until we fix the error
218+
219+
if (myVisualizer) {
220+
$("#dataViz,#curInstr").addClass('dimmed'); // dim the visualization until we fix the error
221+
myVisualizer.updateOutput(); // to update arrows
222+
}
223+
167224
var s = pyInputAceEditor.getSession();
168225
s.setAnnotations([{row: errorLineNo,
169226
type: 'error',
@@ -195,9 +252,9 @@ function initAceEditor(height) {
195252
pyInputAceEditor.$blockScrolling = Infinity; // kludgy to shut up weird warnings
196253

197254
// auto-grow height as fit
198-
pyInputAceEditor.setOptions({minLines: 18, maxLines: 1000});
255+
pyInputAceEditor.setOptions({minLines: 20, maxLines: 20});
199256

200-
$('#codeInputPane').css('width', '450px');
257+
$("#pyInputPane,#codeInputPane").css('width', aceEditorWidth);
201258
$('#codeInputPane').css('height', height + 'px'); // VERY IMPORTANT so that it works on I.E., ugh!
202259

203260
pyInputAceEditor.on('change', function(e) {
@@ -259,6 +316,7 @@ function optliveExecuteCodeAndCreateViz(codeToExec,
259316
}
260317
}
261318
else {
319+
prevVisualizer = myVisualizer;
262320
myVisualizer = new ExecutionVisualizer(outputDiv, dataFromBackend, frontendOptionsObj);
263321
handleSuccessFunc();
264322
}
@@ -303,7 +361,6 @@ function executeCode(forceStartingInstr, forceRawInputLst) {
303361
var startingInstruction = forceStartingInstr ? forceStartingInstr : 0;
304362
var frontendOptionsObj = {startingInstruction: startingInstruction,
305363
executeCodeWithRawInputFunc: executeCodeWithRawInput,
306-
updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');},
307364
hideCode: true,
308365
jumpToEnd: true,
309366
}

‎v3/js/pytutor.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,13 @@ ExecutionVisualizer.prototype.updateCurPrevLines = function() {
14861486
}
14871487
}
14881488

1489+
if (curEntry.event == 'exception' ||
1490+
curEntry.event == 'uncaught_exception') {
1491+
assert(curEntry.exception_msg);
1492+
hasError = true;
1493+
myViz.curLineExceptionMsg = curEntry.exception_msg;
1494+
}
1495+
14891496
curLineNumber = curEntry.line;
14901497

14911498
// edge case for the final instruction :0
@@ -1831,8 +1838,8 @@ ExecutionVisualizer.prototype.updateOutputFull = function(smoothTransition) {
18311838

18321839
// on 'return' events, give a bit more of a vertical nudge to show that
18331840
// the arrow is aligned with the 'bottom' of the line ...
1834-
var prevVerticalNudge = prevIsReturn ? Math.floor(myViz.codeRowHeight / 2) : 0;
1835-
var curVerticalNudge = curIsReturn ? Math.floor(myViz.codeRowHeight / 2) : 0;
1841+
var prevVerticalNudge = prevIsReturn ? Math.floor(myViz.codeRowHeight / 3) : 0;
1842+
var curVerticalNudge = curIsReturn ? Math.floor(myViz.codeRowHeight / 3) : 0;
18361843

18371844

18381845
// edge case for the final instruction :0

‎v3/opt-live.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565
<div id="pyInputPane">
6666
<div id="codeInputWarnings">Write Python code here:</div>
6767
<div id="codeInputPane"></div>
68-
<div id="frontendErrorOutput"></div>
6968
<div id="executionSlider" style="margin-top: 10px;"></div>
70-
<div id="curInstr" style="margin: 6px auto; text-align: center;">Step ? of ?</div>
69+
<div id="curInstr"></div>
70+
<div id="frontendErrorOutput"></div>
7171
</div>
7272
</td>
7373
<td valign="top">

0 commit comments

Comments
 (0)