Compare commits

..

2 Commits

Author SHA1 Message Date
b1ek a326c8f6b8
add simple history 2023-03-15 16:13:25 +10:00
b1ek 57b5fa7e8d
add cat 2023-03-15 16:13:14 +10:00
2 changed files with 43 additions and 9 deletions

View File

@ -1,5 +1,29 @@
const fs = require('fs'); import { Terminal } from 'xterm';
const fs = require('../fs');
/**
*
* @param { string[] } argv
* @param { Terminal } terminal
*/
module.exports = (argv, terminal) => { module.exports = (argv, terminal) => {
terminal.writeln('hi') if (argv.indexOf('--help') != -1) {
terminal.writeln('Usage: cat [file] [-n]');
terminal.writeln(' -n --number: show lines numbers');
terminal.writeln(' --help: show this help');
return;
}
const numbers = (argv.indexOf('-n') != -1) || (argv.indexOf('--number') != -1);
let files = argv.filter(x => { return !x.startsWith('-') });
files.shift();
files.forEach(file => {
const lines = fs.readFileSync(file).toString().split('\n');
let i = 1;
lines.forEach(line => {
if (numbers) terminal.write('\033[35m' + i + ' |\033[0m ');
terminal.writeln(line);
i++;
})
})
} }

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)