add set-hostname command

This commit is contained in:
b1ek 2023-05-05 00:56:43 +10:00
parent 3bafeaa768
commit a60f5485fc
Signed by: blek
GPG Key ID: 14546221E3595D0C
3 changed files with 51 additions and 7 deletions

View File

@ -15,6 +15,7 @@ let cmds = {
'echo': require('./echo'),
'exit': require('./exit'),
'systemctl': require('./systemctl'),
'set-hostname': require('./set-hostname.js'),
'guide': require('./guide'),

View File

@ -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;
}

View File

@ -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');