added ability to download a file using cat

This commit is contained in:
Lisa Milne 2023-11-07 18:48:52 +10:00
parent c38dd412e7
commit ded2aa9739

View file

@ -77,11 +77,34 @@ Options:
clite.commands.load('cat',function(args,env,io) {
var files = [];
var download = false;
var pending = 0;
function help() {
io.write(`
Usage: cat [OPTION] <FILE> [FILE]
Options:
-? Print this help information
-d Download the file data, instead of printing it
`);
}
for (var i=1; i<args.length; i++) {
if (args[i][0] == '-') {
io.error('unknown argument: '+args[i]);
for (var j=1; j<args[i].length; j++) {
switch (args[i][j]) {
case '?':
help();
return 0;
break;
case 'd':
download = true;
break;
default:
io.error('unknown argument: '+args[i]);
}
}
}else if (args[i][0] == '/') {
files.push(args[i]);
}else{
@ -99,7 +122,18 @@ clite.commands.load('cat',function(args,env,io) {
}else{
var d = clite.io.readAll(fd);
if (d != null) {
io.write(d);
if (download) {
var link = document.createElement("a");
link.target = '_blank';
link.download = fd.node.name;
var blob = new Blob([d], {type: "text/plain"});
link.href = URL.createObjectURL(blob);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}else{
io.write(d);
}
}
}
pending--;
@ -128,9 +162,5 @@ clite.commands.load('cat',function(args,env,io) {
return null;
});
clite.commands.load('download',function(args,env,io) {
return 0;
});
// insert commands above this line
}