implement zsh (partially)

This commit is contained in:
b1ek 2023-03-13 01:20:34 +10:00
parent c73c3c5284
commit d8b2c061df
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 29 additions and 1 deletions

View File

@ -8,21 +8,44 @@ let terminal;
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 history = [];
let history_pos = 0;
function text_prompt() {
return prompt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
}
function pr_char(char) { function pr_char(char) {
cmd += char; cmd += char;
terminal.write(char); terminal.write(char);
// console.log(char.charCodeAt(0));
if (history_pos != 0) history_pos = 0;
} }
function exec_cmd() { function exec_cmd() {
let c = cmd; let c = cmd;
reset_cmd(); reset_cmd();
history.push(c);
terminal.writeln('zsh: command not found: ' + c); terminal.writeln('zsh: command not found: ' + c);
print_prompt(); print_prompt();
} }
function print_prompt() { function print_prompt() {
terminal.write(prompt); terminal.write(prompt);
history_pos = 0;
}
function reprint_prompt() {
terminal.write('\033[2K\r');
print_prompt();
}
function history_up() {
if (history_pos != history.length) {
reprint_prompt();
terminal.write(history[history_pos]);
history_pos++;
}
} }
function reset_cmd() { function reset_cmd() {
@ -36,7 +59,7 @@ function control_char(char) {
// backspace // backspace
case 127: case 127:
if (terminal.buffer.active.cursorX == prompt.length) break; if (terminal.buffer.active.cursorX <= text_prompt().length) break;
terminal.write('\b \b'); terminal.write('\b \b');
cmd = cmd.substring(0, cmd.length - 1); cmd = cmd.substring(0, cmd.length - 1);
break; break;
@ -53,6 +76,11 @@ function control_char(char) {
print_prompt(); print_prompt();
break; break;
// history up
case 27:
history_up();
break;
default: default:
console.log('Unknown special char: ' + id); console.log('Unknown special char: ' + id);
break; break;