diff --git a/react/resume/src/emulator/zsh.js b/react/resume/src/emulator/zsh.js index c3fe7ba..8bb4dc2 100644 --- a/react/resume/src/emulator/zsh.js +++ b/react/resume/src/emulator/zsh.js @@ -8,21 +8,44 @@ let terminal; const prompt = '\033[1;32muser@blek.codes \033[36m~ $ \033[0m'; 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) { cmd += char; terminal.write(char); + // console.log(char.charCodeAt(0)); + if (history_pos != 0) history_pos = 0; } function exec_cmd() { let c = cmd; reset_cmd(); + history.push(c); terminal.writeln('zsh: command not found: ' + c); print_prompt(); } function print_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() { @@ -36,7 +59,7 @@ function control_char(char) { // backspace case 127: - if (terminal.buffer.active.cursorX == prompt.length) break; + if (terminal.buffer.active.cursorX <= text_prompt().length) break; terminal.write('\b \b'); cmd = cmd.substring(0, cmd.length - 1); break; @@ -53,6 +76,11 @@ function control_char(char) { print_prompt(); break; + // history up + case 27: + history_up(); + break; + default: console.log('Unknown special char: ' + id); break;