Commit c9320547 authored by ThorbenQuast's avatar ThorbenQuast
Browse files

editor: feature #1967 - first version of command caching

parent 4aafee79
......@@ -21,6 +21,7 @@ class CodeEditorExtension(AbstractExtension):
self.add_js("js/output.js")
self.add_js("js/preview.js")
self.add_js("js/prefs.js")
self.add_js("js/commandline.js")
self.add_css("css/styles.css")
......
......@@ -17,6 +17,12 @@
}
.codeeditor #command-line {
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
left: 78px;
}
.codeeditor #cmd_hist {
left: 100px;
position: absolute;
width: 80px;
}
.codeeditor-left {
position: absolute;
......
......@@ -19,8 +19,16 @@
Abort
</button>
</span>
<input type="text" class="form-control" id="command-line" placeholder="Commandline" value="" data-toggle="tooltip" data-placement="bottom" title="Execution command relative to the filepath. %file is a variable holding the filename. (e.g. 'python %file')">
</div>
<input type="text" class="form-control" id="command-line" placeholder="Commandline" value="" data-toggle="tooltip" data-placement="bottom" title="Execution command relative to the filepath. %file is a variable holding the filename.">
<div class="btn-group" id="cmd_hist">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
History
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
</ul>
</div>
</div>
<pre class="codeeditor-output">Execution Output</pre>
<div class="codeeditor-output-buttons">
<div class="btn-group">
......
var CommandLine = Emitter.extend({
init: function(view) {
this.view = view;
// commandline template -> list of extensions
this.commandlineTemplates = {
"python %file": ["py"],
"sh %file": ["sh"],
"php %file": ["php"],
"pdflatex %file": ["tex"],
"gcc %file -o %file.out && ./%file.out": ["c"],
"g++ %file -o %file.out && ./%file.out": ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]
};
this.commandline = 0;
this.histNode = 0;
this.commands = [];
this.defaultCommand = null;
},
setup: function(node) {
// command line setup
var rightTopNode = node.find(".codeeditor-right-top");
var self = this;
this.commandline = rightTopNode.find("#command-line").on("keypress", function(event) {
if (event.which == 13) { // return key pressed
self.view.output.execute();
event.preventDefault();
}
}).tooltip({
trigger: "focus"
});
this.histNode = rightTopNode.find(".dropdown-menu");
this.setCommandLine();
this.pushDefault(this.getVal());
},
setCommandLine: function() {
if (this.view.editor && this.view.editor.path) {
var fileExtension = this.view.editor.path.match(/\.(.+?)$/);
if (fileExtension) {
fileExtension = fileExtension[1];
for (var i in this.commandlineTemplates) {
if (~this.commandlineTemplates[i].indexOf(fileExtension)) {
this.commandline.val(i);
return true;
}
}
}
}
},
pushDefault: function(cmd) {
if (!cmd)
return;
this.defaultCommand = {
cmd: cmd
};
this.renderHistory();
},
pushEntry: function(cmd) {
if (this.checkExistence(cmd)) {
return;
}
if (this.commands.length == this.view.getPreference("commandHistory")) {
this.commands.pop();
} //do not exceed maximum length
this.commands.unshift({
name: cmd,
cmd: cmd
});
this.renderHistory();
},
checkExistence: function(cmd) {
for (var i = 0; i < this.commands.length; i++) {
if (cmd == this.commands[i].cmd) {
return true;
}
} //no double saving!!
return false;
},
renderHistory: function() {
var self = this;
this.histNode.html("");
$.each(this.commands, function(i, entry) {
self.histNode.append(self.createNode(entry.name, entry.cmd));
});
this.histNode.append($("<li/>").addClass("divider"));
this.histNode.append(self.createNode("default", this.defaultCommand.cmd));
},
createNode: function(name, cmd) {
var self = this;
var node = $("<a/>");
node.html(name);
node.on({
mouseover: function() {
$("body").css("cursor", "hand");
},
mouseleave: function() {
$("body").css("cursor", "default");
},
click: function() {
self.commandline.val(cmd);
}
});
return $("<li/>").append(node);
},
getVal: function() {
return this.commandline.val();
}
});
\ No newline at end of file
......@@ -293,7 +293,7 @@ var CodeEditor = Emitter.extend({
}
});
} else {
self.view.output.setCommandLine();
self.view.commandLine.setCommandLine();
self.save(callback, "new", path);
}
},
......
......@@ -81,6 +81,7 @@ var CodeEditorView = vispa.ExtensionView.Center.extend({
this.output = new CodeEditorOutput(this);
this.preview = new CodeEditorPreview(this);
this.ui = new CodeEditorUI(this);
this.commandLine = new CommandLine(this);
this.forceClose = false;
},
......@@ -110,6 +111,9 @@ var CodeEditorView = vispa.ExtensionView.Center.extend({
//setup the UI and the auto resizes
self.ui.setup();
//setup the commandline
self.commandLine.setup(node);
self.applyPreferences();
self._applyShortcuts();
self.setLoading(false);
......
......@@ -10,18 +10,6 @@ var CodeEditorOutput = Emitter.extend({
this.preNode = null;
this.running = false;
// commandline template -> list of extensions
this.commandlineTemplates = {
"python %file": ["py"],
"sh %file": ["sh"],
"php %file": ["php"],
"pdflatex %file": ["tex"],
"gcc %file -o %file.out && ./%file.out": ["c"],
"g++ %file -o %file.out && ./%file.out":
["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]
};
},
setup: function(node) {
......@@ -58,24 +46,12 @@ var CodeEditorOutput = Emitter.extend({
event.preventDefault();
});
// command line setup
this.commandline = rightTopNode.find("#command-line").on("keypress", function(event) {
if (event.which == 13) { // return key pressed
self.execute();
event.preventDefault();
}
}).tooltip({
trigger: "focus"
});
self.setCommandLine();
// check if there is a running job
this.view.POST("runningjob").done(function(res) {
self.running = !!res.runningjob;
self.updateExeButtons();
});
// register events
this.view.onSocket("start", function(data) {
self.append(
......@@ -97,24 +73,6 @@ var CodeEditorOutput = Emitter.extend({
return this;
},
// command line setup
setCommandLine: function() { //returns true, if extension can; false, respectively
if (this.view.editor && this.view.editor.path) {
var fileExtension = this.view.editor.path.match(/\.(.+?)$/);
if (fileExtension) {
fileExtension = fileExtension[1];
for (var i in this.commandlineTemplates) {
if (~this.commandlineTemplates[i].indexOf(fileExtension)) {
this.commandline.val(i);
return true;
}
}
}
}
return false;
},
formatRuntime: function(t) {
t = parseFloat(t);
var days = Math.floor(t / 86400);
......@@ -148,7 +106,10 @@ var CodeEditorOutput = Emitter.extend({
this.view.editor.save(function() {
// create the command
var cmd = self.commandline.val();
var cmd = self.view.commandLine.getVal();
//store the command
self.view.commandLine.pushEntry(cmd);
if (!cmd) {
self.view.alert("Please indicate a valid command for script execution!");
return this;
......
......@@ -74,7 +74,7 @@ var getDefaultCodeEditorPreferences = function() {
description: "Set the maximum number of lines of the output",
type: "integer",
value: 2000,
range: [100,5000,100]
range: [100, 5000, 100]
},
// preview
......@@ -83,6 +83,14 @@ var getDefaultCodeEditorPreferences = function() {
type: "integer",
value: 150,
range: [50, 500, 10]
},
//history commands
commandHistory: {
description: "The total number of stored commands.",
type: "integer",
value: 3,
range: [1, 10, 1]
}
};
};
......@@ -114,4 +122,4 @@ var getDefaultCodeEditorShortcuts = function() {
}
}
};
};
};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment