Skip to content

Instantly share code, notes, and snippets.

@orellabac
Last active April 18, 2024 00:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orellabac/300046c20e2dc14cc1345b43e54940eb to your computer and use it in GitHub Desktop.
Save orellabac/300046c20e2dc14cc1345b43e54940eb to your computer and use it in GitHub Desktop.
// Very simple terminal created using xterm.js. You can just paste this code on jsbin
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/xterm/2.6.0/xterm.css" />
<script src="https://cdn.jsdelivr.net/xterm/2.6.0/xterm.js"></script>
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
</head>
<body>
<div id="terminal"></div>
<script>
var endsWith = function(subjectString,searchString, position) {
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.lastIndexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
var term = new Terminal();
term.open(document.getElementById('terminal'));
term.currentPrompt = '\033[1;3;31mterminal\033[0m $ ';
term.write(term.currentPrompt);
window.term = term;
term.lastCommand = '';
term.currentCommand = '';
term.commandCallback = function(response) {
term.write(response);
if (!endsWith(response,'\r\n'))
term.write('\r\n');
term.write(term.currentPrompt);
};
term.processCommand = function(command) {
term.pendingPromise = $.Deferred();
term.pendingPromise.done(
function(resolvedResponse)
{
term.commandCallback(resolvedResponse);
});
term.executeCommand(command,term.pendingPromise);
}
term.executeCommand = function(command, pendingPromise) {
pendingPromise.resolve('execute ' + command + ' dummy result dummy result dummy result dummy result\r\n');
};
document.getElementById('terminal').onkeyup = function (e) {
switch(e.which) {
case 9:
//Ignore tab
return;
break;
case 13:
term.lastCommand = term.currentCommand;
term.currentCommand = '';
term.write('\r\n');
term.processCommand(term.lastCommand);
break;
case 8:
term.write('\b \b');
term.currentCommand = term.currentCommand.substring(0, term.currentCommand.length-1);
break;
default:
var char = e.key;
term.write(char);
term.currentCommand = term.currentCommand + char;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment