From 3c02044761366fe474c7bf8cb5ba7307960c788c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= Date: Tue, 15 Sep 2015 20:06:01 +0200 Subject: Prolog handler now uses CodeQ terminal. --- css/codeq/console.css | 7 +++ js/prolog.js | 140 ++++++++++++++++++++++++++++++++++++++++++++++++-- js/python.js | 9 ++-- 3 files changed, 147 insertions(+), 9 deletions(-) diff --git a/css/codeq/console.css b/css/codeq/console.css index 4290eea..7d1f6ae 100644 --- a/css/codeq/console.css +++ b/css/codeq/console.css @@ -15,3 +15,10 @@ color: greenyellow; background: black; } + +.cq-con-text.input { + color: #a6e1ec; +} +.cq-con-text.error { + color: #d9534f; +} \ No newline at end of file diff --git a/js/prolog.js b/js/prolog.js index 7ea8ce3..cc46736 100644 --- a/js/prolog.js +++ b/js/prolog.js @@ -6,7 +6,7 @@ // a constant var firstCharacterPos = {'line': 0, 'ch': 0}; - var makePrologTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) { +/* var makePrologTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) { var promptMode = true, // default: query composition; alternative: query result browsing tcs = function terminalCommandSuccess (data) { var t, lines, i; @@ -82,6 +82,85 @@ }); return {}; + };*/ + + var makePrologTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) { + var promptMode = true, // default: query composition; alternative: query result browsing + terminal = codeq.makeConsole(jqConsole, { + 'greeting': 'CodeQ Prolog terminal proxy' + }), + tcs = function terminalCommandSuccess (data) { + var t, lines, i; + if (data.code === 0) { + t = data.terminal; + terminal.append(t.messages.join('\n') + '\n', 'output'); + promptMode = !t.have_more; + } + else { + terminal.error(data.message + '\n', 'error'); + promptMode = true; + } + if (promptMode) { + terminal.setLineBuffered(); + terminal.append('?- ', 'output'); + } + }, + tcf = function terminalCommandFailed (error) { + promptMode = true; + terminal.setLineBuffered(); + terminal.append(error + '\n', 'error'); + terminal.append('?- ', 'output'); + }; + + terminal.onKeypress = function (c) { + if (promptMode) return c; // query composition: return the character unchanged + switch (c) { + case ' ': + case ';': + case '\n': + return ';'; // show next answer on space, semicolon or enter + default: + return '.'; // everything else: stop searching for answers + } + }; + + terminal.onInput = function (command) { + if (promptMode) { + promptMode = false; + terminal.setNotBuffered(); + return codeq.comms.sendQuery({ + 'problem_id': problem_id, + 'step': 'run', + 'program': editor.getDoc().getValue(), + 'query': command, + 'trace': activityHandler.addAndPurge({'typ': 'slv', 'qry': command}) + }, problem_id).then(tcs, tcf); + } + else { + if (command == ';') { + // show next answer + return codeq.comms.sendQuery({ + 'problem_id': problem_id, + 'step': 'next', + 'trace': activityHandler.addAndPurge({'typ': 'nxt'}) + }, problem_id).then(tcs, tcf); + } + else { + // stop searching for answers + return codeq.comms.sendQuery({ + 'problem_id': problem_id, + 'step': 'end', + 'trace': activityHandler.addAndPurge({'typ': 'stp'}) + }, problem_id).then(tcs, tcf); + } + + } + }; + + terminal.leftmostCol = 3; + terminal.append('?- ', 'output'); + + return terminal; }; var makeActivityHandler = function (editor, problem_id) { @@ -296,6 +375,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 @@ -370,7 +450,7 @@ }); $('#btn_code_hint').on('click', function () { // handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]); - jqConsole.echo('?- hint.'); +/* jqConsole.echo('?- hint.'); jqConsole.pause(); var doc = editor.getDoc(); codeq.comms.sendHint({ @@ -389,10 +469,35 @@ jqConsole.resume(); jqConsole.exception(error); } - ).done(); + ).done();*/ + + terminal.append('hint.\n', 'input'); + terminal.inputDisable(); + var doc = editor.getDoc(); + codeq.comms.sendHint({ + 'language': 'prolog', + 'program': editor.getDoc().getValue(), + 'problem_id': problem.id + }) + .then( + function hintSuccess(data) { + if (data.code === 0) + handler.processServerHints(data.hints); + else + terminal.append(data.message + '\n', 'error'); + }, + function hintFailed (error) { + terminal.append(error + '\n', 'error'); + } + ) + .fin(function () { + terminal.inputEnable(); + terminal.append('?- ', 'output'); + }) + .done(); }); $('#btn_code_test').on('click', function () { - jqConsole.echo('?- test.'); +/* jqConsole.echo('?- test.'); jqConsole.pause(); var doc = editor.getDoc(); codeq.comms.sendTest({ @@ -411,7 +516,32 @@ jqConsole.resume(); jqConsole.exception(error); } - ).done(); + ).done();*/ + + terminal.append('test.\n', 'input'); + terminal.inputDisable(); + var doc = editor.getDoc(); + codeq.comms.sendTest({ + 'language': 'prolog', + 'program': editor.getDoc().getValue(), + 'problem_id': problem.id + }) + .then( + function testSuccess(data) { + if (data.code === 0) + handler.processServerHints(data.hints); + else + terminal.append(data.message + '\n', 'error'); + }, + function testFailed (error) { + terminal.append(error + '\n', 'error'); + } + ) + .fin(function () { + terminal.inputEnable(); + terminal.append('?- ', 'output'); + }) + .done(); }); return handler; diff --git a/js/python.js b/js/python.js index d16cc72..82423c6 100644 --- a/js/python.js +++ b/js/python.js @@ -48,11 +48,11 @@ 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 +62,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; }, @@ -111,7 +111,7 @@ 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. */ @@ -248,6 +248,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 -- cgit v1.2.1