Skip to content

Commit eb307fb

Browse files
author
Philip Guo
committed
prettified pyCrazyMode display to show both current and previous exprs
1 parent eb526a6 commit eb307fb

2 files changed

Lines changed: 96 additions & 25 deletions

File tree

‎v3/css/pytutor.css‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,14 @@ div.ExecutionVisualizer .highlight-legend {
720720
border-color: #cccccc;
721721
}
722722

723+
723724
/* for pyCrazyMode */
724-
span.crazyHighlightedChar {
725+
726+
/* prev then curr, so curr gets precedence when both apply */
727+
div.ExecutionVisualizer .pycrazy-highlight-prev {
728+
background-color: #F0F0EA;
729+
}
730+
731+
div.ExecutionVisualizer .pycrazy-highlight-curr {
725732
background-color: #FFFF66;
726733
}

‎v3/js/pytutor.js‎

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ var curVisualizerID = 1; // global to uniquely identify each ExecutionVisualizer
102102
// with some new user input (somewhat hacky!)
103103
// highlightLines - highlight current and previously executed lines (default: false)
104104
// arrowLines - draw arrows pointing to current and previously executed lines (default: true)
105+
// pyCrazyMode - run with Py2crazy, which provides expression-level
106+
// granularity instead of line-level granularity (HIGHLY EXPERIMENTAL!)
105107
function ExecutionVisualizer(domRootID, dat, params) {
106108
this.curInputCode = dat.code.rtrim(); // kill trailing spaces
107109
this.curTrace = dat.trace;
@@ -158,6 +160,11 @@ function ExecutionVisualizer(domRootID, dat, params) {
158160
this.params.arrowLines = !(this.params.highlightLines);
159161
}
160162

163+
// audible!
164+
if (this.params.pyCrazyMode) {
165+
this.params.arrowLines = this.params.highlightLines = false;
166+
}
167+
161168
// David's original logic ...
162169
/*
163170
if (!this.params.arrowLines && !this.params.highlightLines) {
@@ -367,6 +374,13 @@ ExecutionVisualizer.prototype.render = function() {
367374
.append('<span class="highlight-legend highlight-prev">line that has just executed</span> ')
368375
.append('<span class="highlight-legend highlight-curr">next line to execute</span>')
369376
}
377+
else if (this.params.pyCrazyMode) {
378+
myViz.domRoot.find('#legendDiv')
379+
.append('<a href="https://github.com/pgbovine/Py2crazy">Py2crazy</a> mode!')
380+
.append(' Stepping through (roughly) each executed expression. Color codes:<p/>')
381+
.append('<span class="pycrazy-highlight-prev">expression that just executed</span><br/>')
382+
.append('<span class="pycrazy-highlight-curr">next expression to execute</span>');
383+
}
370384

371385

372386
if (this.params.editCodeBaseURL) {
@@ -1157,6 +1171,32 @@ ExecutionVisualizer.prototype.renderPyCodeOutput = function() {
11571171
}
11581172

11591173

1174+
// takes a string inputStr and returns an HTML version with
1175+
// the single character at highlightIndex highlighted with
1176+
// a span of class highlightCssClass
1177+
function htmlWithHighlight(inputStr, highlightInd, highlightCssClass) {
1178+
var prefix = '';
1179+
if (highlightInd > 0) {
1180+
prefix = inputStr.slice(0, highlightInd);
1181+
}
1182+
1183+
var highlightedChar = inputStr.slice(highlightInd, highlightInd + 1);
1184+
1185+
var suffix = '';
1186+
if (highlightInd < inputStr.length - 1) {
1187+
suffix = inputStr.slice(highlightInd + 1, inputStr.length);
1188+
}
1189+
1190+
// ... then set the current line to lineHTML
1191+
var lineHTML = htmlspecialchars(prefix) +
1192+
'<span class="' + highlightCssClass + '">' +
1193+
htmlspecialchars(highlightedChar) +
1194+
'</span>' +
1195+
htmlspecialchars(suffix);
1196+
return lineHTML;
1197+
}
1198+
1199+
11601200
// This function is called every time the display needs to be updated
11611201
// smoothTransition is OPTIONAL!
11621202
ExecutionVisualizer.prototype.updateOutput = function(smoothTransition) {
@@ -1348,37 +1388,61 @@ ExecutionVisualizer.prototype.updateOutput = function(smoothTransition) {
13481388
}
13491389

13501390
if (myViz.pyCrazyMode) {
1351-
var lineInfo = myViz.codeOutputLines[curLineNumber - 1];
1352-
assert(lineInfo.lineNumber == curLineNumber);
1353-
var codeAtLine = lineInfo.text;
1354-
//console.log("Line=", curLineNumber, ", Col=", curColumnNumber, codeAtLine);
1355-
1356-
// super hack to create three different substrings from codeAtLine
1357-
// and then encode each one separately as HTML
1358-
var prefix = '';
1359-
if (curColumnNumber > 0) {
1360-
prefix = codeAtLine.slice(0, curColumnNumber);
1361-
}
1362-
1363-
var highlightedChar = codeAtLine.slice(curColumnNumber, curColumnNumber + 1);
1364-
1365-
var suffix = '';
1366-
if (curColumnNumber < codeAtLine.length - 1) {
1367-
suffix = codeAtLine.slice(curColumnNumber + 1, codeAtLine.length);
1368-
}
1391+
var curLineInfo = myViz.codeOutputLines[curLineNumber - 1];
1392+
assert(curLineInfo.lineNumber == curLineNumber);
1393+
var codeAtLine = curLineInfo.text;
13691394

13701395
// shotgun approach: reset ALL lines to their natural (unbolded) state
13711396
$.each(myViz.codeOutputLines, function(i, e) {
13721397
var d = myViz.generateID('cod' + e.lineNumber);
13731398
myViz.domRoot.find('#' + d).html(htmlspecialchars(e.text));
13741399
});
13751400

1376-
// ... then set the current line to lineHTML
1377-
var lineHTML = htmlspecialchars(prefix) +
1378-
'<span class="crazyHighlightedChar">' + htmlspecialchars(highlightedChar) + '</span>' +
1379-
htmlspecialchars(suffix);
1380-
var curCodeLineDiv = myViz.generateID('cod' + curLineNumber);
1381-
myViz.domRoot.find('#' + curCodeLineDiv).html(lineHTML);
1401+
1402+
// Three possible cases:
1403+
// 1. previous and current trace entries are on the SAME LINE
1404+
// 2. previous and current trace entries are on different lines
1405+
// 3. there is no previous trace entry
1406+
1407+
if (prevLineNumber == curLineNumber) {
1408+
var curLineHTML = null;
1409+
1410+
// generate a combined line with both previous and current
1411+
// columns highlighted
1412+
1413+
if (prevColumnNumber < curColumnNumber) {
1414+
var firstPortion = codeAtLine.slice(0, prevColumnNumber+1);
1415+
var secondPortion = codeAtLine.slice(prevColumnNumber+1, codeAtLine.length);
1416+
curLineHTML =
1417+
htmlWithHighlight(firstPortion, prevColumnNumber, 'pycrazy-highlight-prev') +
1418+
htmlWithHighlight(secondPortion, curColumnNumber - prevColumnNumber - 1, 'pycrazy-highlight-curr');
1419+
}
1420+
else if (prevColumnNumber == curColumnNumber) {
1421+
// just display current
1422+
curLineHTML = htmlWithHighlight(codeAtLine, curColumnNumber, 'pycrazy-highlight-curr');
1423+
}
1424+
else {
1425+
assert(curColumnNumber < prevColumnNumber);
1426+
1427+
var firstPortion = codeAtLine.slice(0, curColumnNumber+1);
1428+
var secondPortion = codeAtLine.slice(curColumnNumber+1, codeAtLine.length);
1429+
curLineHTML =
1430+
htmlWithHighlight(firstPortion, curColumnNumber, 'pycrazy-highlight-curr') +
1431+
htmlWithHighlight(secondPortion, prevColumnNumber - curColumnNumber - 1, 'pycrazy-highlight-prev');
1432+
}
1433+
1434+
assert(curLineHTML);
1435+
myViz.domRoot.find('#' + myViz.generateID('cod' + curLineNumber)).html(curLineHTML);
1436+
}
1437+
else {
1438+
if (prevLineNumber) {
1439+
var prevLineInfo = myViz.codeOutputLines[prevLineNumber - 1];
1440+
var prevLineHTML = htmlWithHighlight(prevLineInfo.text, prevColumnNumber, 'pycrazy-highlight-prev');
1441+
myViz.domRoot.find('#' + myViz.generateID('cod' + prevLineNumber)).html(prevLineHTML);
1442+
}
1443+
var curLineHTML = htmlWithHighlight(codeAtLine, curColumnNumber, 'pycrazy-highlight-curr');
1444+
myViz.domRoot.find('#' + myViz.generateID('cod' + curLineNumber)).html(curLineHTML);
1445+
}
13821446
}
13831447

13841448
// on 'return' events, give a bit more of a vertical nudge to show that

0 commit comments

Comments
 (0)