add simple history

This commit is contained in:
b1ek 2023-03-15 16:13:25 +10:00
parent 57b5fa7e8d
commit a326c8f6b8
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 17 additions and 7 deletions

View File

@ -18,6 +18,7 @@ let dom;
const prompt = '\033[1;32muser@blek.codes \033[36m~ $ \033[0m'; const prompt = '\033[1;32muser@blek.codes \033[36m~ $ \033[0m';
let cmd = ''; let cmd = '';
let lastcmd = '';
function text_prompt() { function text_prompt() {
return prompt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); return prompt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
@ -48,28 +49,30 @@ function exec_file(f) {
function exec_cmd() { function exec_cmd() {
let c = cmd; let c = cmd;
const command = c.split(' ')[0];
reset_cmd(c); reset_cmd(c);
lastcmd = c;
if (c == '') { if (command == '') {
print_prompt(); print_prompt();
return; return;
} }
// if path // if path
if (c.match(/^((\.|\.\.)\/|\/).+$/gm)) { if (command.match(/^((\.|\.\.)\/|\/).+$/gm)) {
exec_file(c); exec_file(command);
terminal.writeln(''); terminal.writeln('');
print_prompt(); print_prompt();
return; return;
} }
if (cmds[c] != undefined) { if (cmds[command] != undefined) {
cmds[c](c.split(' '), terminal); cmds[command](c.split(' '), terminal);
print_prompt(); print_prompt();
return; return;
} }
terminal.writeln('zsh: command not found: ' + c); terminal.writeln('zsh: command not found: ' + command);
print_prompt(); print_prompt();
} }
@ -108,6 +111,13 @@ function control_char(id, dom) {
exec_cmd(); exec_cmd();
break; break;
case 38:
if (lastcmd == '') break;
cmd = lastcmd;
reprint_prompt();
terminal.write(lastcmd);
break;
// Ctrl+c // Ctrl+c
case 67: case 67:
if (dom.ctrlKey) { if (dom.ctrlKey) {
@ -135,7 +145,7 @@ function control_char(id, dom) {
function key(e) { function key(e) {
/** @type {KeyboardEvent} */ /** @type {KeyboardEvent} */
const dom = e.domEvent; const dom = e.domEvent;
if (dom.key.length == 1 && !(dom.ctrlKey || dom.altKey || dom.shiftKey)) { if (dom.key.length == 1 && !(dom.ctrlKey || dom.altKey)) {
pr_char(e.domEvent.key); pr_char(e.domEvent.key);
} else { } else {
control_char(e.domEvent.keyCode, dom) control_char(e.domEvent.keyCode, dom)