Skip to content

Commit

Permalink
Drop stdout/stderr tabs and have single output tab
Browse files Browse the repository at this point in the history
  • Loading branch information
JonJagger committed Jul 29, 2020
1 parent 893321f commit 00635ee
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 41 deletions.
32 changes: 32 additions & 0 deletions app/assets/javascripts/codemirror/mode/output/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE

(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";

CodeMirror.defineMode("output", function(config) {

return {
token: function(stream, state) {
if (stream.sol()) {
if (stream.match(':stdout:')) { return 'variable'; }
if (stream.match(':stderr:')) { return 'number'; }
if (stream.match(':status:')) { return 'comment'; }
}
stream.skipToEnd();
return 'null';
}
};

});

CodeMirror.defineMIME("text/x-output", "output");

});
5 changes: 4 additions & 1 deletion app/assets/javascripts/cyber-dojo_codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ var cyberDojo = ((cd, $) => {
};

const codeMirrorLineWrapping = (filename) => {
return ['readme.txt','stdout', 'stderr'].includes(filename);
return ['readme.txt', 'output'].includes(filename);
};

const codeMirrorIndentWithTabs = (filename) => {
Expand All @@ -185,6 +185,9 @@ var cyberDojo = ((cd, $) => {
if (filename === 'makefile') {
return 'text/x-makefile';
}
if (filename === 'output') {
return 'text/x-output';
}
switch (fileExtension(filename)) {
// C/C++ have split source
case '.c' : return 'text/x-csrc';
Expand Down
8 changes: 8 additions & 0 deletions app/assets/javascripts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ var cyberDojo = ((cd, $) => {
this.filenames().forEach(filename => this.deleteFile(filename));
};

Editor.prototype.output = function(stdout, stderr, status) {
this.changeFile('output', { content:
":stdout:\n" + stdout + "\n" +
":stderr:\n" + stderr + "\n" +
":status:\n" + status + "\n"
});
};

Editor.prototype.createFile = function(filename, file) {
const $newFile = $makeNewFile(filename, file);
$('#visible-files-box').append($newFile);
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/kata.scss
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
border-bottom: none;
}
&.filename { margin-left:0px; }
&.stdout, &.stderr, &.REPL { float:right; }
&.output, &.stdout, &.stderr, &.REPL { float:right; }
&.REPL { display:none; }
}
.tab.filename.selected { cursor:context-menu; }
Expand Down
17 changes: 12 additions & 5 deletions app/views/kata/_editor.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
$(() => {

cd.loadInitialFiles = () => {
var stdout = '';
var stderr = '';
var status = '';
const files = [];
<% @files.each do |filename,file| %>
var file = {
name:"<%= j raw(filename) %>",
content:"<%= j raw(file['content']) %>"
};
files.push(file);
var filename = "<%= j raw(filename) %>";
var content = "<%= j raw(file['content']) %>";
switch (filename) {
case 'stdout': stdout = content; break;
case 'stderr': stderr = content; break;
case 'status': status = content; break;
default: files.push({ name:filename, content:content }); break;
}
<% end %>
cd.kata.editor.createFiles(files);
cd.kata.editor.output(stdout, stderr, status);
cd.kata.filenames.refresh();
};

Expand Down
5 changes: 5 additions & 0 deletions app/views/kata/_file_new_rename_delete.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ $(() => {
const isValidFilename = filename => {
const contains = illegal => filename.indexOf(illegal) !== -1;
if (alreadyExists(filename)) { return false; }
if (sss(filename)) { return false; }
if (contains('..')) { return false; }
if (contains('\\')) { return false; }
if (contains(' ')) { return false; }
Expand All @@ -139,5 +140,9 @@ $(() => {

const alreadyExists = (filename) => cd.kata.editor.filenames().includes(filename);

const sss = (filename) => {
return filename === 'stdout' || filename === 'stderr' || filename === 'status';
};

});
</script>
17 changes: 6 additions & 11 deletions app/views/kata/_tabs.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
<div class="tab filename">cyber-dojo.sh</div>
<div class="tab REPL">+REPL</div>
<%# These three float:right so are in reverse order of appearance %>
<div class="tab stderr">stderr</div>
<div class="tab stdout">stdout</div>
<div class="tab output">output</div>
</div>

<script type="text/javascript">
Expand All @@ -30,16 +29,14 @@ $(() => {
};

tabs.filename = () => $('.tab.filename', $tabs);
tabs.stdout = () => $('.tab.stdout' , $tabs);
tabs.stderr = () => $('.tab.stderr' , $tabs);
tabs.output = () => $('.tab.output' , $tabs);
tabs.repl = () => $('.tab.REPL' , $tabs);

tabs.selected = () => $('.tab.selected', $tabs);

tabs.reserves = filename => {
const tabNames = [];
tabNames.push(tabs.stdout().text());
tabNames.push(tabs.stderr().text());
tabNames.push(tabs.output().text());
tabNames.push('status');
tabNames.push(tabs.repl().text());
return tabNames.includes(filename);
Expand All @@ -62,9 +59,8 @@ $(() => {
// See app/assets/javascripts/cyber-dojo_codemirror.js
// See app/views/shared/_hotkeys.html.erb
switch (tabs.selected().text()) {
default : tabs.stdout().click(); break;
case tabs.stdout().text(): tabs.stderr().click(); break;
case tabs.stderr().text(): tabs.filename().click(); break;
default : tabs.output().click(); break;
case tabs.output().text(): tabs.filename().click(); break;
}
};

Expand All @@ -91,8 +87,7 @@ $(() => {
};

setupFileTabClickHandler(tabs.filename());
setupFileTabClickHandler(tabs.stdout());
setupFileTabClickHandler(tabs.stderr());
setupFileTabClickHandler(tabs.output());
setupReplTabClickHandler(tabs.repl());

});
Expand Down
25 changes: 2 additions & 23 deletions app/views/kata/run_tests.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,9 @@
};

const updateOutput = () => {
editor.changeFile('stdout', { content: stdout });
editor.changeFile('stderr', { content: stderr });
editor.changeFile('status', { content: status });
const tabs = cd.kata.tabs;
const empty = (s) => s.length === 0;
switch (outcome) {
case 'red':
case 'green':
if (!empty(stdout) || empty(stderr)) {
tabs.stdout().click();
} else {
tabs.stderr().click();
}
break;
case 'amber':
case 'timed_out':
case 'faulty':
if (stdout.length > stderr.length) {
tabs.stdout().click();
} else {
tabs.stderr().click();
}
break;
}
editor.output(stdout, stderr, status);
tabs.output().click();
};

//- - - - - - - - - - - - - - - - - - - - - -
Expand Down

0 comments on commit 00635ee

Please sign in to comment.