shell history and tabfill

This commit is contained in:
Lisa Milne 2023-11-26 19:54:37 +10:00
parent 82e39184f1
commit ccbb4fa408
2 changed files with 97 additions and 25 deletions

View file

@ -1,6 +1,6 @@
var clite = { var clite = {
state:{ state:{
version:'0.2-1', version:'0.2-2',
isinit:false isinit:false
}, },
core:{ core:{
@ -1331,13 +1331,14 @@ clite.term = {
return clite.term.tty.data.data.onkeypress(e); return clite.term.tty.data.data.onkeypress(e);
if (e.key.length > 1 && e.key != 'Spacebar' && clite.term.data.handler != null) { if (e.key.length > 1 && e.key != 'Spacebar' && clite.term.data.handler != null) {
if (e.key == 'Enter') { if (e.key == 'Enter') {
clite.term.data.buff = '';
clite.term.data.handler(e.target.value); clite.term.data.handler(e.target.value);
clite.term.buff = ''; return false;
}else{ }else{
clite.term.data.handler('\1'+e.key); clite.term.data.handler('\1'+e.key);
} }
} }
clite.term.buff = e.target.value; clite.term.data.buff = e.target.value;
return false; return false;
}, },
refocus:function(e) { refocus:function(e) {

View file

@ -80,6 +80,44 @@ clite.commands.load('sh',function(args,env,io) {
} }
}; };
var history = {
data:[],
current:'',
index:0,
add:function(txt) {
if (txt.length < 1)
return;
if (history.data.length > 0 && history.data[history.data.length-1] == txt)
return;
history.data.push(txt);
history.index = history.data.length;
},
up:function() {
if (history.index == 0)
return;
history.index--;
term.ttyctrl('iset',history.getCurrent());
},
down:function() {
if (history.index == history.data.length)
return;
history.index++;
term.ttyctrl('iset',history.getCurrent());
},
setCurrent:function(txt) {
history.current = txt;
},
getCurrent:function() {
if (history.index == history.data.length)
return history.current;
return history.data[history.index];
},
resetCurrent:function() {
history.current = '';
history.index = history.data.length;
}
}
function active_exit(pid,v) { function active_exit(pid,v) {
if (pid != active_pid) if (pid != active_pid)
return; return;
@ -157,6 +195,45 @@ clite.commands.load('sh',function(args,env,io) {
return pptxt; return pptxt;
} }
function tabfill() {
var c = term.ttyctrl('iget');
var parts = stdlib.strToArgs(c);
var ai = parts.length-1;
if (ai < 0)
return;
var a = parts[ai];
var add = '';
if (ai == 0) {
// test for shell macros/builtins
Object.keys(macro).forEach(function(key) {
if (add == '' && key.substring(0,a.length) == a) {
add = key.substring(a.length);
}
});
if (add == '') {
// test for commands
var pparts = env.PATH.split(':');
pparts.forEach(function(pp) {
if (add != '')
return;
var fd = stdio.open('/bin',false);
var f;
while (add == '' && (f = stdio.read(fd)) != null) {
if (f.substring(0,a.length) == a) {
add = f.substring(a.length)+' ';
}
}
stdio.close(fd);
});
}
}else{
// TODO: path fill
}
if (add == '')
return;
term.ttyctrl('iset',c+add);
}
function parseScript(fd) { function parseScript(fd) {
var l; var l;
while ((l = stdio.readLine(fd)) != null) { while ((l = stdio.readLine(fd)) != null) {
@ -174,8 +251,8 @@ clite.commands.load('sh',function(args,env,io) {
} }
return; return;
} }
//clite.shell.history.add(txt); history.add(txt);
//clite.shell.history.resetCurrent(); history.resetCurrent();
if (interactive) if (interactive)
stdio.write(io.stdout,prompt+txt); stdio.write(io.stdout,prompt+txt);
// clear the tty prompt // clear the tty prompt
@ -236,26 +313,20 @@ clite.commands.load('sh',function(args,env,io) {
} }
function inputControl(key) { function inputControl(key) {
//try { switch(key) {
//if (e.key == 'Down' || e.key == 'ArrowDown') { case 'Tab':
//clite.shell.history.down(); tabfill();
//return false; break;
//} case 'Down':
//if (e.key == 'Up' || e.key == 'ArrowUp') { case 'ArrowDown':
//clite.shell.history.up(); history.down();
//return false; break;
//} case 'Up':
//if (e.key == 'Tab') { case 'ArrowUp':
//return false; history.up();
//} break;
//var t = clite.term.data.field.value; default:;
//if (e.key != 'Enter') { }
//clite.shell.history.setCurrent(t);
//return true;
//}
//clite.shell.run(t);
//return false;
//} catch(err) {return false;}
} }
function input(str) { function input(str) {
if (str[0] == '\1') { if (str[0] == '\1') {