From ccbb4fa408be56e882ce4fc5b38e4af24fae1bb2 Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Sun, 26 Nov 2023 19:54:37 +1000 Subject: [PATCH] shell history and tabfill --- clite/core.js | 7 +-- clite/shell.js | 115 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 97 insertions(+), 25 deletions(-) diff --git a/clite/core.js b/clite/core.js index fb1b075..ac9d957 100644 --- a/clite/core.js +++ b/clite/core.js @@ -1,6 +1,6 @@ var clite = { state:{ - version:'0.2-1', + version:'0.2-2', isinit:false }, core:{ @@ -1331,13 +1331,14 @@ clite.term = { return clite.term.tty.data.data.onkeypress(e); if (e.key.length > 1 && e.key != 'Spacebar' && clite.term.data.handler != null) { if (e.key == 'Enter') { + clite.term.data.buff = ''; clite.term.data.handler(e.target.value); - clite.term.buff = ''; + return false; }else{ clite.term.data.handler('\1'+e.key); } } - clite.term.buff = e.target.value; + clite.term.data.buff = e.target.value; return false; }, refocus:function(e) { diff --git a/clite/shell.js b/clite/shell.js index 6767a3e..820c054 100644 --- a/clite/shell.js +++ b/clite/shell.js @@ -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) { if (pid != active_pid) return; @@ -157,6 +195,45 @@ clite.commands.load('sh',function(args,env,io) { 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) { var l; while ((l = stdio.readLine(fd)) != null) { @@ -174,8 +251,8 @@ clite.commands.load('sh',function(args,env,io) { } return; } - //clite.shell.history.add(txt); - //clite.shell.history.resetCurrent(); + history.add(txt); + history.resetCurrent(); if (interactive) stdio.write(io.stdout,prompt+txt); // clear the tty prompt @@ -236,26 +313,20 @@ clite.commands.load('sh',function(args,env,io) { } function inputControl(key) { - //try { - //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; - //} - //if (e.key == 'Tab') { - //return false; - //} - //var t = clite.term.data.field.value; - //if (e.key != 'Enter') { - //clite.shell.history.setCurrent(t); - //return true; - //} - //clite.shell.run(t); - //return false; - //} catch(err) {return false;} + switch(key) { + case 'Tab': + tabfill(); + break; + case 'Down': + case 'ArrowDown': + history.down(); + break; + case 'Up': + case 'ArrowUp': + history.up(); + break; + default:; + } } function input(str) { if (str[0] == '\1') {