homepage.js/react/resume/src/emulator/commands/cat.js

29 lines
877 B
JavaScript
Raw Normal View History

2023-03-15 07:13:14 +01:00
import { Terminal } from 'xterm';
const fs = require('../fs');
2023-03-15 07:13:14 +01:00
/**
*
* @param { string[] } argv
* @param { Terminal } terminal
*/
module.exports = (argv, terminal) => {
2023-03-15 07:13:14 +01:00
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++;
})
})
}