diff --git a/src/emulator/commands/index.js b/src/emulator/commands/index.js index ff0c3e8..d96a03d 100644 --- a/src/emulator/commands/index.js +++ b/src/emulator/commands/index.js @@ -15,6 +15,7 @@ let cmds = { 'echo': require('./echo'), 'exit': require('./exit'), 'systemctl': require('./systemctl'), + 'set-hostname': require('./set-hostname.js'), 'guide': require('./guide'), diff --git a/src/emulator/commands/set-hostname.js b/src/emulator/commands/set-hostname.js new file mode 100644 index 0000000..9c53630 --- /dev/null +++ b/src/emulator/commands/set-hostname.js @@ -0,0 +1,30 @@ +const { Terminal } = require("xterm"); + +const original_data = Object.freeze(data); +global.orig = original_data; + +/** + * + * @param {string[]} argv + * @param {Terminal} terminal + */ +module.exports = (argv, terminal, zsh) => { + if (argv.indexOf('--help') !== -1) { + terminal.writeln(` +Usage: ${argv[0]} [hostname] [-R] [--help] + -R Reset it to original IP (${original_data.ip}) + --help Show this page` + ); + return 0; + } + + if (argv.indexOf('-R') !== -1) { + data.ip = original_data.ip; + zsh.update_prompt(); + return 0; + } + + data.ip = argv[1] || 'resume.js'; + zsh.update_prompt(); + return 0; +} \ No newline at end of file diff --git a/src/emulator/zsh.js b/src/emulator/zsh.js index 8d19e1b..ec52e87 100644 --- a/src/emulator/zsh.js +++ b/src/emulator/zsh.js @@ -2,9 +2,18 @@ import { Terminal } from 'xterm'; import { XTerm } from 'xterm-for-react'; +const memfs = require('memfs'); const fs = require('./fs'); global.fs = fs; const cmds = require('./commands'); +const sleep = require('../lib/sleep'); + +// ZSH api to be used in commands (see references) +const zshapi = { + update_prompt, + text_prompt, + pr_char +}; /** * @type { Terminal } @@ -16,10 +25,14 @@ let terminal; */ let dom; -const prompt = `\x1b[1;32muser@${data.ip} \x1b[36m~ $ \x1b[0m`; +let prompt = `\x1b[1;32muser@${data.ip} \x1b[36m~ $ \x1b[0m`; let cmd = ''; let lastcmd = window.sessionStorage.getItem('last_cmd') || ''; +function update_prompt() { + prompt = `\x1b[1;32muser@${data.ip} \x1b[36m~ $ \x1b[0m`; +} + function text_prompt() { return prompt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); } @@ -44,13 +57,15 @@ function exec_file(f) { return; } - const executable = fs.accessSync(f, fs.constants.X_OK); + // This fails for some reason (vvvv) + const executable = fs.accessSync(f, memfs.constants.X_OK); + // const executable = true; if (!executable) { - terminal.writeln('zsh: permission denied: ' + f); + terminal.write('zsh: permission denied: ' + f); return; } - terminal.writeln('This is an online resume. It is not big enough to have a script runtime.\n'); + terminal.writeln('This is just a simple online resume. It is not big enough to have a script runtime for god\'s sake.'); return; } @@ -77,9 +92,7 @@ async function exec_cmd() { if (cmds[command] != undefined) { const startY = terminal.buffer.normal.cursorY - await cmds[command](c.split(' '), terminal); - - await (new Promise(resolve => setTimeout(resolve, 10))); + await cmds[command](c.split(' '), terminal, zshapi); if (terminal.buffer.active.cursorX != 0 && startY != terminal.buffer.active.cursorY) { terminal.write('\033[30;47m%\033[0m\n');