diff options
Diffstat (limited to 'js/python.js')
-rw-r--r-- | js/python.js | 87 |
1 files changed, 32 insertions, 55 deletions
diff --git a/js/python.js b/js/python.js index d16cc72..7d7458e 100644 --- a/js/python.js +++ b/js/python.js @@ -8,51 +8,40 @@ var makePythonTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) { var terminal = codeq.makeConsole(jqConsole, { - 'greeting': 'CodeQ Python terminal proxy' - }); + 'greeting': 'CodeQ Python terminal proxy' + }), + tcs = function terminalCommandSuccess (data) { + if (data.code !== 0) { + terminal.append(data.message, 'error'); + } + }, + tcf = function terminalCommandFailed (error) { + terminal.append(error + '\n', 'error'); + }; + terminal.onInput = function (text) { - if (!text) - return; - codeq.comms.sendPush({ + return codeq.comms.sendPush({ 'text': text + '\n' - }).then( - function pushSuccess (data) { - if (data.code !== 0) - terminal.append('error: ' + data.message); - }, - function pushFailed (error) { - terminal.append('exception: '+ error); - } - ).done(); + }).then(tcs, tcf); }; - var interval_id = setInterval(function () { - codeq.comms.sendPull({}).then( - function pullSuccess (data) { - if (data.code === 0) { - t = data.terminal; - if (!t.text) - return; - terminal.append(t.text); - } - else { - terminal.append('error: ' + data.message); - } - }, - function pullFailed (error) { - terminal.append('exception: '+ error); - } - ).done(); - }, 200); + codeq.comms.on('terminal_output', function (data) { + var text = data.text; + terminal.append(text, 'output'); + lines = text.split('\n'); + terminal.leftmostCol = lines[lines.length-1].length; + }); + + terminal.leftmostCol = 1; return terminal; }; - var makeActivityHandler = function (editor) { + var makeActivityHandler = function (editor, problem_id) { var lastActivityMillis = Date.now(), deltaActivityMillis = function deltaActivityMillisFunc () { var now = Date.now(), - dt = Math.max(0, Math.min(30000, now - lastActivityMillis)); // 0 sec <= dt <= 30 sec + dt = now - lastActivityMillis; lastActivityMillis = now; return dt; }, @@ -62,7 +51,7 @@ var promise; ts = null; if (queue.length === 0) return Q(true); - promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue()); + promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue(), problem_id); queue.length = 0; return promise; }, @@ -72,17 +61,11 @@ }; return { - "trace": function (trace) { + 'queueTrace': function (trace) { trace['dt'] = deltaActivityMillis(); - return trace; - }, - 'queue': function (trace) { queue.push(trace); if (ts === null) setTimeout(timer, 10000); // flush every 10 seconds }, - 'queueTrace': function (trace) { - this.queue(this.trace(trace)); - }, 'flush': flush, 'addAndPurge': function (trace) { var accumulatedTrace = queue; @@ -111,10 +94,8 @@ jqConsole = $('#console'), jqHints = $('#info'), editor = CodeMirror(jqEditor[0], { cursorHeight: 0.85, lineNumbers: true, matchBrackets: true, mode: 'python' }), - activityHandler = makeActivityHandler(editor), + activityHandler = makeActivityHandler(editor, problem.id), terminal = makePythonTerminalHandler(jqConsole, editor, problem.id, activityHandler), - - /** Object.<string, HintDefinition> */ hintDefs = problem.hint, hintCounter = 0, // for generating unique class-names hintCleaners = [], @@ -248,6 +229,7 @@ var handler = { destroy: function () { + terminal.destroy(); jqDescription.empty(); jqEditor.empty(); // TODO: perhaps you do not want to "free" the editor, just empty it jqConsole.empty(); // TODO: the same with the console @@ -312,17 +294,8 @@ // $(jqButtons.get(1)).on('click', function () { handler.processServerHints([{id:'popup_unknown', start: 20, end: 26}]); }); // $(jqButtons.get(2)).on('click', function () { handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]); }); - $('#btn_code_run').on('click', function () { - var doc = editor.getDoc(); -// codeq.comms.sendActivity({'typ': 'slv', 'dt': dt, 'qry': }); -// handler.processServerHints([{id:'list_empty'}]); - }); - $('#btn_code_break').on('click', function () { -// handler.processServerHints([{id:'popup_unknown', start: 20, end: 26}]); - }); $('#btn_code_hint').on('click', function () { // handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]); - terminal.append('>>> hint\n'); var doc = editor.getDoc(); codeq.comms.sendHint({ 'language': 'python', @@ -341,7 +314,6 @@ ).done(); }); $('#btn_code_test').on('click', function () { - terminal.append('>>> test\n'); var doc = editor.getDoc(); codeq.comms.sendTest({ 'language': 'python', @@ -360,6 +332,11 @@ ).done(); }); + // TODO first line of interpreter output is buffered without this, why? + codeq.comms.sendPush({ + 'text': '' + }); + return handler; }; })(); |