Browse Source

Add terminal support on compilation error and stderr

Marcelo Fornet 5 years ago
parent
commit
722b00b3b8
3 changed files with 61 additions and 22 deletions
  1. 26 1
      src/core.ts
  2. 3 21
      src/extension.ts
  3. 32 0
      src/terminal.ts

+ 26 - 1
src/core.ts

@@ -5,6 +5,7 @@ import { dirname, join, extname, basename } from "path";
 import * as child_process from 'child_process';
 import * as gwen from './gwen';
 import { TestcaseResult, Veredict, SolutionResult, Problem, Contest, SiteDescription } from "./types";
+import { ceTerminal, stderrTerminal } from './terminal';
 const md5File = require('md5-file');
 
 export const TESTCASES = 'testcases';
@@ -359,6 +360,13 @@ export function timedRun(path: string, tcName: string, timeout: number){
 
     let spanTime = new Date().getTime() - startTime;
 
+    if (xresult.stderr.length > 0) {
+        let stderrTer = stderrTerminal();
+        let escaped_output = escape_double_ticks(xresult.stderr.toString());
+        stderrTer.sendText(`echo "${escaped_output}"`);
+        stderrTer.show();
+    }
+
     // Check if an error happened
     if (xresult.status !== 0){
         if (spanTime < timeout){
@@ -385,6 +393,13 @@ export function timedRun(path: string, tcName: string, timeout: number){
     }
 }
 
+function escape_double_ticks(text: string) {
+    text = text.toString();
+    // text = text.replace('"', '\"');
+    console.log(text);
+    return text;
+}
+
 export function compileCode(pathCode: string, pathOutput: string){
     let pathCodeMD5 = pathCode + '.md5';
     let md5data = "";
@@ -419,7 +434,17 @@ export function compileCode(pathCode: string, pathOutput: string){
     let program = splitedInstruction[0];
     let args = splitedInstruction.slice(1);
 
-    return child_process.spawnSync(program, args);
+    let result =  child_process.spawnSync(program, args);
+
+    if (result.status !== 0) {
+        // Write to the compile error terminal
+        let ter = ceTerminal();
+        let escaped_output = escape_double_ticks(result.stderr);
+        ter.sendText(`echo "${escaped_output}"`);
+        ter.show();
+    }
+
+    return result;
 }
 
 export function testSolution(path: string){

+ 3 - 21
src/extension.ts

@@ -6,6 +6,7 @@ import { SITES, getSite } from './conn';
 import { newContestFromId, testSolution, veredictName, stressSolution, upgradeArena, newProblemFromId, removeExtension, solFile, initAcmX, currentProblem, compileCode, ATTIC, SRC } from './core';
 import { Veredict, SiteDescription } from './types';
 import { startCompetitiveCompanionService } from './companion';
+import { hideTerminals } from './terminal';
 
 const TESTCASES = 'testcases';
 
@@ -219,28 +220,8 @@ async function addTestcase() {
     await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(out), vscode.ViewColumn.Two);
 }
 
-function getTerminal() {
-    let target = undefined;
-
-    vscode.window.terminals.forEach(value => {
-        if (value.name === 'acmx-output') {
-            target = value;
-        }
-    });
-
-    if (target === undefined) {
-        target = vscode.window.createTerminal("acmx-output");
-    }
-
-    return target;
-}
-
 async function coding() {
-    let terminal = getTerminal();
-
-    let result = terminal.sendText('echo "Hello"');
-    terminal.show();
-    console.log(result);
+    hideTerminals();
 
     let path = currentProblem();
 
@@ -337,6 +318,7 @@ async function debugTest(){
     console.log("no bugs :O");
 }
 
+// TODO: Make all the code async.
 // this method is called when your extension is activated
 // your extension is activated the very first time the command is executed
 export function activate(context: vscode.ExtensionContext) {

+ 32 - 0
src/terminal.ts

@@ -0,0 +1,32 @@
+'use strict';
+import * as vscode from 'vscode';
+
+function getTerminal(name: string) {
+    let target = undefined;
+
+    vscode.window.terminals.forEach(value => {
+        if (value.name === name) {
+            target = value;
+        }
+    });
+
+    if (target === undefined) {
+        target = vscode.window.createTerminal(name);
+    }
+
+    return target;
+}
+
+export function ceTerminal() {
+    return getTerminal('acmx-compile-error');
+}
+
+export function stderrTerminal() {
+    return getTerminal('acmx-stderr');
+}
+
+export function hideTerminals() {
+    vscode.window.terminals.forEach(ter => {
+        ter.hide();
+    });
+}