Skip to content

Commit be842c7

Browse files
author
Philip Guo
committed
more user-friendly "step i of N" printouts thanks
to Peter Wentworth
1 parent 4a338fb commit be842c7

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

‎TODO‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,6 @@ Peter
212212
z = None
213213

214214

215-
- Also, in Peter Wentworth's Python 3 implementation, when you've hit
216-
the FINAL step, he displays that the program has terminated rather
217-
than "Step N of N", which is a more user-friendly touch! Instead, he
218-
displays the penultimate step as "Step (N-1) of (N-1)"
219-
220-
221215
- Integrate Peter's Python 3 fork into one unified application, and
222216
perhaps deploy online on WebFaction, since they offer both Python 2.X
223217
and Python 3.X on their servers.

‎edu-python.js‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function processTrace(traceData) {
104104
updateOutput();
105105
}
106106

107-
function highlightCodeLine(curLine, visitedLinesSet, hasError) {
107+
function highlightCodeLine(curLine, visitedLinesSet, hasError, isTerminated) {
108108
var tbl = $("table#pyCodeOutput");
109109

110110
// reset then set:
@@ -121,14 +121,17 @@ function highlightCodeLine(curLine, visitedLinesSet, hasError) {
121121
lineBgCol = errorColor;
122122
}
123123

124+
// put a default white top border to keep space usage consistent
124125
tbl.find('td.cod').css('border-top', '1px solid #ffffff');
125126

126-
if (!hasError) {
127-
tbl.find('td.cod:eq(' + (curLine - 1) + ')').css('border-top', '1px solid #F87D76')
127+
if (!hasError && !isTerminated) {
128+
tbl.find('td.cod:eq(' + (curLine - 1) + ')').css('border-top', '1px solid #F87D76');
128129
}
129130

130131
tbl.find('td.cod').css('background-color', '');
131-
tbl.find('td.cod:eq(' + (curLine - 1) + ')').css('background-color', lineBgCol);
132+
if (!isTerminated || hasError) {
133+
tbl.find('td.cod:eq(' + (curLine - 1) + ')').css('background-color', lineBgCol);
134+
}
132135
}
133136

134137
// relies on curTrace and curInstr globals
@@ -138,8 +141,15 @@ function updateOutput() {
138141

139142
// render VCR controls:
140143
var totalInstrs = curTrace.length;
141-
$("#vcrControls #curInstr").html(curInstr + 1);
142-
$("#vcrControls #totalInstrs").html(totalInstrs);
144+
145+
// to be user-friendly, if we're on the LAST instruction, print "Program has terminated"
146+
// and DON'T highlight any lines of code in the code display
147+
if (curInstr == (totalInstrs-1)) {
148+
$("#vcrControls #curInstr").html("Program has terminated");
149+
}
150+
else {
151+
$("#vcrControls #curInstr").html("About to do step " + (curInstr + 1) + " of " + (totalInstrs-1));
152+
}
143153

144154
$("#vcrControls #jmpFirstInstr").attr("disabled", false);
145155
$("#vcrControls #jmpStepBack").attr("disabled", false);
@@ -150,7 +160,7 @@ function updateOutput() {
150160
$("#vcrControls #jmpFirstInstr").attr("disabled", true);
151161
$("#vcrControls #jmpStepBack").attr("disabled", true);
152162
}
153-
if (curInstr == (totalInstrs - 1)) {
163+
if (curInstr == (totalInstrs-1)) {
154164
$("#vcrControls #jmpLastInstr").attr("disabled", true);
155165
$("#vcrControls #jmpStepFwd").attr("disabled", true);
156166
}
@@ -193,7 +203,7 @@ function updateOutput() {
193203
visitedLinesSet[curTrace[i].line] = true;
194204
}
195205
}
196-
highlightCodeLine(curEntry.line, visitedLinesSet, hasError);
206+
highlightCodeLine(curEntry.line, visitedLinesSet, hasError, (curInstr == (totalInstrs-1)));
197207
}
198208

199209

‎index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
<div id="vcrControls">
109109
<button id="jmpFirstInstr", type="button">&lt;&lt; First</button>
110110
<button id="jmpStepBack", type="button">&lt; Back</button>
111-
Step <span id="curInstr">?</span> of <span id="totalInstrs">?</span>
111+
<span id="curInstr">Step ? of ?</span>
112112
<button id="jmpStepFwd", type="button">Forward &gt;</button>
113113
<button id="jmpLastInstr", type="button">Last &gt;&gt;</button>
114114

0 commit comments

Comments
 (0)