Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

191 lines (170 sloc) 7.164 kB
<html>
<head>
<title>Node REPL</title>
<link rel="stylesheet" href="webrepl.css" TYPE="text/css"/>
<script src="jquery-1.5.min.js" type="text/javascript"></script>
<script type="text/javascript">
var blurbHidden = false;
var cmdHistory = [];
var historyIndex = -1;
function historyUp() {
if (historyIndex + 1 < cmdHistory.length) historyIndex++;
if (historyIndex < cmdHistory.length) {
return cmdHistory[historyIndex];
}
}
function historyDown() {
if (historyIndex > -1) historyIndex--;
if (historyIndex == -1) return "";
if (historyIndex < cmdHistory.length) {
return cmdHistory[historyIndex];
}
}
function getCommonPrefix(words) {
if (words.length == 0) return '';
if (words.length == 1) return words[0];
var prefix = words[0];
for (var i = 1; i < words.length; ++i) {
var _p = '';
var min_len = Math.min(prefix.length, words[i].length);
for (var j = 0; j < min_len && prefix[j] == words[i][j]; ++j) {
_p += prefix[j];
}
prefix = _p;
}
return prefix;
}
$(document).ready(function() {
$('#out').ajaxError(function(e, req, opts, err) {
$('#out').prepend($("<div class='replErr'>[ Error communicating with node process ]</div>"));
});
$('#in').keypress(function(event) {
var keyCode = event.keyCode || event.which;
if (keyCode === 13) {
event.preventDefault();
commandEntered();
}
});
$('#in').keydown(function(event) {
var keyCode = event.keyCode || event.which;
if (keyCode === 9) {
event.preventDefault();
complete();
} else if (keyCode === 38) {
event.preventDefault();
$('#in').val(historyUp());
} else if (keyCode === 40) {
event.preventDefault();
$('#in').val(historyDown());
}
});
$('#clear').width('100px');
$('#clear').click(function() {
clear();
});
$.get('info', function(data) {
$('#header').prepend($("<div>[" + data.pid + '] ' + data.name + "</div>"));
}, 'json');
$('#in').focus();
});
function clear() {
$.post('clear', function(data) {
getResponse();
});
$('#out').prepend($("<div class='replErr'>[ Reset ]</div>"));
}
function commandEntered() {
if (! blurbHidden) {
$('#blurb').hide();
blurbHidden = true;
}
var command = $('#in').val();
cmdHistory.unshift(command);
historyIndex = -1;
$('#out').prepend($("<div class='replIn'>" + command + "</div>"));
$.post('repl', command, function(data) {
$('#in').val('');
getResponse();
}, "text");
}
function getResponse() {
$.get('repl', function(resData) {
var type = 'replOut';
if (resData.match(/.*Error:/)) {
type = 'replErr';
}
$('#out').prepend($("<div class='" + type + "'>" + resData + "</div>"));
});
}
function complete() {
var str = $('#in').val();
var i = str.lastIndexOf(' ');
var word = str.substr(i + 1);
$.get('complete/' + word, function(data) {
if (data.completions.length == 1) {
var sWord = data.completions[0];
$('#in').val($('#in').val() + lettersToAppend(word, sWord));
} else {
var possibles = [];
for (j = 0; j < data.completions.length; j++) {
if (data.completions[j] != '') {
possibles.push(data.completions[j]);
}
}
var expansion = getCommonPrefix(possibles);
if (expansion.length > 0) {
$('#in').val($('#in').val() + lettersToAppend(word, expansion));
}
var suggestString = '';
for (j = 0; j < possibles.length; j++) {
suggestString += possibles[j] + ' ';
}
if (! blurbHidden) {
$('#blurb').hide();
blurbHidden = true;
}
$('#out').prepend($("<div class='replCompl'>" + suggestString + "</div>"));
}
}, "json");
}
function lettersToAppend(word1, word2) {
var word1Matched = 0;
for (i = 0; i < word1.length + word2.length; i++) {
if (word1[i] === word2[word1Matched]) {
word1Matched++;
if ((i+1) == word1.length) {
return word2.substring(word1Matched);
}
} else {
word1Matched = 0;
}
}
}
function longestInCommon(candidates, index) {
var i, ch, memo
do {
memo = null
for (i=0; i < candidates.length; i++) {
ch = candidates[i].charAt(index)
if (!ch) break
if (!memo) memo = ch
else if (ch != memo) break
}
} while (i == candidates.length && ++index)
return candidates[0].slice(0, index)
}
</script>
</head>
<body>
<div id="header"></div>
<table id="input-bar">
<tr>
<td style="width:95%"><input id="in" type="text" /></td>
<td style="width:5%"><span id="clear">^C</span></td>
</tr>
</table>
<div id="main-container">
<div id="out"><div id="blurb">web-repl: Type commands in the above box to execute in this javascript process. Use tab for completion.</div></div>
</div>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.