websocketCLI
version 0.1
by Alexander Shpak
HTML
<div id="cli">
<ul id="output">
</ul>
<p id = "greeter"></p><input id = "input" type="text" />
</div>
<div style="clear: left"></div>
CSS
* {
margin: 0px;
font: 12pt monospace;
background: #111;
color: #fff;
}
.userpos {
float: left;
padding: 0 10px 0 0;
}
ul {
padding: 0;
list-style: none;
}
#input {
width: 100%;
border: 0;
}
input {
float: left;
width: 400px;
border: 0;
}
input:focus {
outline:none;
}
.message {
color: #fff;
}
.success {
color: #5cb85c;
}
.info {
color: #5bc0de;
}
.warning {
color: #f0ad4e;
}
.danger {
color: #d9534f;
}
JavaScript
function CliApp(name, usage, argIsString) {
"use strict";
this.name = name;
this.usage = usage;
this.args = [];
this.argIsString = argIsString ? argIsString : false;
this.action = function() {}
}
var CliEngine = function() {
"use strict";
this.version = "0.0.2";
this.apps = [];
this.history = [];
this.historyCurrent = 0;
this.inputPushed = false;
};
CliEngine.prototype = {
"addApp": function(app) {
this.apps.push(app);
return this;
},
"getApp": function(app) {
for (var i = 0, len = this.apps.length; i < len; i++) {
if (this.apps[i].name === app) {
return this.apps[i];
break;
}
}
},
"onEnter": function(str) {
var appExist = false,
appName = str.split(" ")[0].toLowerCase();
if (this.inputPushed)
this.history[this.history.length-1] = str;
else
this.history.push(str);
this.inputPushed = false;
for (var i = 0, len = this.apps.length; i < len; i++) {
if (this.apps[i].name === appName) {
var app = this.apps[i];
appExist = true;
if (str.split(" ").length > 1) {
app.args = app.argIsString ? str.slice(appName.length + 1) : str.slice(appName.length + 1).split(" ");
}
app.action();
};
}
return appExist ? "" : "WARNING: App: " + appName + " not exist";
},
"_pushInput": function(str) {
this.history.push(str);
this.inputPushed = true;
},
"historyPrev": function(str) {
if (!this.inputPushed) {
this._pushInput(str);
this.historyCurrent = this.history.length === 1 ? 0 : this.history.length - 2
} else {
this.historyCurrent = this.historyCurrent === 0 ? 0 : --this.historyCurrent;
}
return this.history[this.historyCurrent];
},
...