add the start of shell history saving

This commit is contained in:
Lisa Milne 2023-12-17 14:57:15 +10:00
parent 98bedb8ca0
commit 7ceb503fee

View file

@ -89,11 +89,10 @@ clite.commands.load('sh',function(args,env,io) {
return 1;
},
exit:function(args,io) {
has_exited = true;
var ev = 0;
if (args.length > 1)
ev = parseInt(args[1]);
io.exit(ev);
doShellExit(ev);
return 0;
}
};
@ -102,6 +101,27 @@ clite.commands.load('sh',function(args,env,io) {
data:[],
current:'',
index:0,
load:function() {
let path = clite.resolvePath('~/.shhistory');
let fd = stdio.open(path,stdio.flags.O_RDONLY|stdio.flags.O_SYNC);
if (!fd)
return;
var e;
while ((e = stdio.readLine(fd)) != null) {
history.add(e);
}
stdio.close(fd);
},
save:function() {
let path = clite.resolvePath('~/.shhistory');
let fd = stdio.open(path,stdio.flags.O_WRONLY|stdio.flags.O_TRUNC|stdio.flags.O_CREAT|stdio.flags.O_SYNC);
if (!fd)
return;
history.data.forEach(function(l) {
stdio.write(fd,l+'\n');
});
stdio.close(fd);
},
add:function(txt) {
if (txt.length < 1)
return;
@ -816,6 +836,13 @@ Options:
`);
}
function doShellExit(v) {
history.save();
stdio.write(io.stdout,'\n');
has_exited = true;
io.exit(v);
}
function resolvePATH(txt) {
// special case of test command
if (txt == '[')
@ -953,6 +980,10 @@ Options:
function inputProcess(str) {
switch (str) {
case String.fromCharCode(4):
case 4:
doShellExit(0);
break;
case '\t': // tab
case 9:
var ct = tabfill(c);
@ -1075,6 +1106,7 @@ Options:
parser.queue(env.HOME+'/.shrc');
parser.run(function() {
history.clear();
history.load();
inputRead();
});
}