mirror of
https://codeberg.org/TicklishHoneyBee/CLIte.git
synced 2026-03-11 09:04:37 +00:00
166 lines
3.8 KiB
JavaScript
166 lines
3.8 KiB
JavaScript
var clite = {
|
|
state:{
|
|
isinit:false,
|
|
terminal:null,
|
|
input:{
|
|
ispass:false,
|
|
form:null,
|
|
field:null
|
|
}
|
|
},
|
|
events:{
|
|
keyup:function(e) {
|
|
if (e.key == 'Down' || e.key == 'ArrowDown') {
|
|
clite.shell.history.down();
|
|
return false;
|
|
}
|
|
if (e.key == 'Up' || e.key == 'ArrowUp') {
|
|
clite.shell.history.up();
|
|
return false;
|
|
}
|
|
var t = clite.state.input.field.value;
|
|
if (e.key != 'Enter') {
|
|
clite.shell.history.setCurrent(t);
|
|
return true;
|
|
}
|
|
clite.shell.exec(t);
|
|
return false;
|
|
},
|
|
refocus:function(e) {
|
|
clite.core.genForm();
|
|
clite.state.input.field.focus();
|
|
}
|
|
},
|
|
core:{
|
|
genForm:function() {
|
|
var f = document.getElementById('form');
|
|
if (!f)
|
|
f = document.createElement('form');
|
|
f.id = 'form';
|
|
f.innerHTML = '';
|
|
var l = document.createElement('label');
|
|
l.innerHTML = clite.shell.prompt.getHTML();
|
|
f.appendChild(l);
|
|
var i = document.createElement('input');
|
|
i.type = clite.state.input.ispass ? 'password' : 'text';
|
|
i.value = clite.shell.history.getCurrent();
|
|
f.appendChild(i);
|
|
clite.state.input.form = f;
|
|
clite.state.input.field = i;
|
|
var t = document.getElementById('terminal');
|
|
if (!t)
|
|
return;
|
|
t.appendChild(f);
|
|
i.onkeyup = clite.events.keyup;
|
|
i.onfocusout = clite.events.refocus;
|
|
i.onblur = clite.events.refocus;
|
|
f.onsubmit = function() {return false;};
|
|
},
|
|
setPass:function(is) {
|
|
clite.state.input.ispass = !!is;
|
|
if (typeof clite.state.input.ispass != 'Boolean')
|
|
clite.state.input.ispass = false;
|
|
}
|
|
},
|
|
init:function() {
|
|
if (this.state.isinit)
|
|
return;
|
|
this.state.isinit = true;
|
|
|
|
document.getElementById('terminal').innerHTML = '';
|
|
// TODO: read in /usr/share/introduction
|
|
this.events.refocus();
|
|
}
|
|
};
|
|
|
|
clite.shell = {
|
|
readLine:function(cb) {
|
|
},
|
|
writeLine:function(txt) {
|
|
var a = document.createElement('article');
|
|
a.innerHTML = txt;
|
|
var t = document.getElementById('terminal');
|
|
if (!t)
|
|
return;
|
|
t.appendChild(a);
|
|
clite.events.refocus();
|
|
},
|
|
exec:function(txt) {
|
|
if (txt.length < 1) {
|
|
clite.shell.writeLine(clite.shell.prompt.getHTML());
|
|
return;
|
|
}
|
|
clite.shell.history.add(txt);
|
|
clite.shell.history.resetCurrent();
|
|
clite.shell.writeLine(clite.shell.prompt.getHTML()+txt);
|
|
// TODO: actually execute the command
|
|
},
|
|
prompt:{
|
|
list:[],
|
|
data:'$ ',
|
|
set:function(txt) {
|
|
clite.shell.prompt.list = [];
|
|
clite.shell.prompt.data = txt;
|
|
},
|
|
push:function(txt) {
|
|
clite.shell.prompt.list.push(clite.shell.prompt.data);
|
|
clite.shell.prompt.data = txt;
|
|
},
|
|
pop:function() {
|
|
clite.shell.prompt.data = clite.shell.prompt.list.pop();
|
|
},
|
|
get:function() {
|
|
return clite.shell.prompt.data;
|
|
},
|
|
getHTML:function() {
|
|
return clite.lib.htmlEncode(clite.shell.prompt.data);
|
|
}
|
|
},
|
|
history:{
|
|
data:[],
|
|
current:'',
|
|
index:0,
|
|
add:function(txt) {
|
|
if (txt.length < 1)
|
|
return;
|
|
clite.shell.history.data.push(txt);
|
|
clite.shell.history.index = clite.shell.history.data.length;
|
|
},
|
|
up:function() {
|
|
if (clite.shell.history.index == 0)
|
|
return;
|
|
clite.shell.history.index--;
|
|
clite.events.refocus();
|
|
},
|
|
down:function() {
|
|
if (clite.shell.history.index == clite.shell.history.data.length)
|
|
return;
|
|
clite.shell.history.index++;
|
|
clite.events.refocus();
|
|
},
|
|
setCurrent:function(txt) {
|
|
clite.shell.history.current = txt;
|
|
},
|
|
getCurrent:function() {
|
|
if (clite.shell.history.index == clite.shell.history.data.length)
|
|
return clite.shell.history.current;
|
|
return clite.shell.history.data[clite.shell.history.index];
|
|
},
|
|
resetCurrent:function() {
|
|
clite.shell.history.current = '';
|
|
clite.shell.history.index = clite.shell.history.data.length;
|
|
}
|
|
}
|
|
};
|
|
|
|
clite.lib = {
|
|
htmlEncode:function(txt) {
|
|
return txt
|
|
.replace(/&/g, '&')
|
|
.replace(/'/g, '&apos')
|
|
.replace(/"/g, '"')
|
|
.replace(/>/g, '>')
|
|
.replace(/</g, '<')
|
|
.replace(/ /g, ' ');
|
|
}
|
|
}
|