From 3752341cf43043fe89742baff35ad74df9fe0f4d Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Fri, 4 Sep 2015 16:55:36 +0200 Subject: Initial Python intepreter implementation Using the new codeq.console library. --- js/python.js | 66 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 22 deletions(-) (limited to 'js/python.js') diff --git a/js/python.js b/js/python.js index f4703b2..df23773 100644 --- a/js/python.js +++ b/js/python.js @@ -7,17 +7,45 @@ var firstCharacterPos = {'line': 0, 'ch': 0}; var makePythonTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) { - var terminal = jqConsole.terminal(function (command, terminal) { - terminal.echo('Not implemented yet.'); - }, { - 'history': true, - 'prompt': '>>> ', - 'greetings': 'CodeQ python terminal proxy', - 'exit': false, - 'clear': false - }); + var terminal = codeq.makeConsole(jqConsole, { + 'greeting': 'CodeQ Python terminal proxy' + }); + terminal.onInput = function (text) { + if (!text) + 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(); + }; + + 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); - return {}; + return terminal; }; var makeActivityHandler = function (editor) { @@ -282,8 +310,7 @@ }); $('#btn_code_hint').on('click', function () { // handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]); - jqConsole.echo('?- hint.'); - jqConsole.pause(); + terminal.append('>>> hint\n'); var doc = editor.getDoc(); codeq.comms.sendHint({ 'language': 'python', @@ -291,21 +318,18 @@ 'problem_id': problem.id }).then( function hintSuccess(data) { - jqConsole.resume(); if (data.code === 0) handler.processServerHints(data.hints); else - jqConsole.error(data.message); + terminal.append('error: ' + data.message); }, function hintFailed (error) { - jqConsole.resume(); - jqConsole.exception(error); + terminal.append('exception: ' + error); } ).done(); }); $('#btn_code_test').on('click', function () { - jqConsole.echo('?- test.'); - jqConsole.pause(); + terminal.append('>>> test\n'); var doc = editor.getDoc(); codeq.comms.sendTest({ 'language': 'python', @@ -313,15 +337,13 @@ 'problem_id': problem.id }).then( function testSuccess(data) { - jqConsole.resume(); if (data.code === 0) handler.processServerHints(data.hints); else - jqConsole.error(data.message); + terminal.append('error: ' + data.message); }, function testFailed (error) { - jqConsole.resume(); - jqConsole.exception(error); + terminal.append('exception: ' + error); } ).done(); }); -- cgit v1.2.1