Commit 9a7a2f32 authored by Nicolas Wavrant's avatar Nicolas Wavrant

runner: minishell has an history

parent 4c36ce04
......@@ -3,9 +3,21 @@
/*global path: true */
/* vim: set et sts=4: */
var shellHistory = "";
var currentCommand = 0;
$(document).ready(function () {
"use strict";
var updateHistory = function () {
$.getJSON("/getMiniShellHistory", function (data) {
shellHistory = data;
currentCommand = shellHistory.length;
});
};
updateHistory();
$("#shell").click (function() {
$("#shell-window").slideToggle("fast");
if ( $("#shell-window").is(':visible') ) {
......@@ -14,15 +26,40 @@ $(document).ready(function () {
});
$("#shell-input").keypress(function (event) {
//if Enter is pressed
if(event.which === 13) {
var command = $("#shell-input").val();
event.preventDefault();
var command = $("#shell-input").val();
var data = { command: command };
$.post("/runCommand", data, function (data) {
data = ">>> " + command + "\n\n" + data;
$("#shell-result").val(data);
$("#shell-input").val("");
});
updateHistory();
});
}
});
$("#shell-input").keydown(function (event) {
//if Key Up is pressed
if(event.which == 38) {
event.preventDefault();
currentCommand--;
if (currentCommand <= 0)
currentCommand = 0;
$("#shell-input").val(shellHistory[currentCommand]);
}
//if Key Down is pressed
if(event.which === 40) {
event.preventDefault();
currentCommand++;
if (currentCommand > shellHistory.length) {
currentCommand = shellHistory.length;
$("#shell-input").val("");
} else {
$("#shell-input").val(shellHistory[currentCommand]);
}
}
});
});
......@@ -60,7 +60,7 @@
<div id="shell-window">
<textarea id="shell-result" cols="80" readonly>
</textarea>
<input type="text" name="command" id="shell-input" />
<input type="text" name="command" id="shell-input" autocomplete="off" />
</div>
<div class="clear"></div>
<div class="software_details">
......
......@@ -926,3 +926,17 @@ def setupDefaultSR(config):
configNewSR(config, config['default_sr'])
if config['auto_deploy']:
thread.start_new_thread(buildAndRun, (config,))
def setMiniShellHistory(config, command):
history_max_size = 10
command = command + "\n"
history_file = config['minishell_history_file']
if os.path.exists(history_file):
history = open(history_file, 'r').readlines()
if len(history) >= history_max_size:
del history[0]
else:
history = []
history.append(command)
open(history_file, 'w+').write(''.join(history))
......@@ -28,6 +28,7 @@ from slapos.runner.utils import (checkSoftwareFolder, configNewSR,
removeSoftwareByName, runInstanceWithLock,
runSoftwareWithLock, runSlapgridUntilSuccess,
saveSession, saveBuildAndRunParams,
setMiniShellHistory,
svcStartStopProcess, svcStopAll, tail,
updateInstanceParameter)
......@@ -728,6 +729,7 @@ def runCommand():
if not command:
return "Changed directory, now in : " + cwd
try:
setMiniShellHistory(app.config, command)
return subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True, cwd=cwd)
except subprocess.CalledProcessError as e:
error = "Error : process exited with exit code " + str(e.returncode) + \
......@@ -735,6 +737,16 @@ def runCommand():
return error
def getMiniShellHistory():
history_file = app.config['minishell_history_file']
if not os.path.exists(history_file):
return ""
history = open(history_file, 'r').readlines()
for line, text in enumerate(history):
history[line] = text.strip()
return json.dumps(history)
#Setup List of URLs
app.add_url_rule('/', 'home', home)
app.add_url_rule('/browseWorkspace', 'browseWorkspace', browseWorkspace)
......@@ -819,3 +831,4 @@ app.add_url_rule('/isSRReady', 'isSRReady', isSRReady)
app.add_url_rule('/addUser', 'addUser', addUser, methods=['POST'])
app.add_url_rule('/getSlapgridParameters', 'getSlapgridParameters', getSlapgridParameters, methods=['GET'])
app.add_url_rule('/runCommand', 'runCommand', runCommand, methods=['POST'])
app.add_url_rule("/getMiniShellHistory", 'getMiniShellHistory', getMiniShellHistory, methods=['GET'])
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