From 0eec2bd8f8fd8669682cf618bf22983a36f828a9 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Wed, 16 Sep 2015 16:17:52 +0200 Subject: Implement async. comm. with Python interpreter --- js/python.js | 60 ++++++++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) (limited to 'js/python.js') diff --git a/js/python.js b/js/python.js index 11e8a2b..7d7458e 100644 --- a/js/python.js +++ b/js/python.js @@ -8,42 +8,31 @@ 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; }; @@ -307,7 +296,6 @@ $('#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', @@ -326,7 +314,6 @@ ).done(); }); $('#btn_code_test').on('click', function () { - terminal.append('>>> test\n'); var doc = editor.getDoc(); codeq.comms.sendTest({ 'language': 'python', @@ -345,6 +332,11 @@ ).done(); }); + // TODO first line of interpreter output is buffered without this, why? + codeq.comms.sendPush({ + 'text': '' + }); + return handler; }; })(); -- cgit v1.2.1