ls and skills commands

This commit is contained in:
b1ek 2023-03-16 00:38:46 +10:00
parent a598fa9364
commit d76b8db1a0
Signed by: blek
GPG Key ID: 14546221E3595D0C
3 changed files with 77 additions and 9 deletions

View File

@ -9,9 +9,9 @@ const fs = require('../fs');
module.exports = (argv, terminal) => {
if (argv.indexOf('--help') != -1) {
terminal.writeln(`Usage: ${argv[0]} [files] [-n]`);
terminal.writeln(' -n --number: show lines numbers');
terminal.writeln(' --help: show this help');
terminal.writeln('Reads file into stdout');
terminal.writeln(' -n --number show lines numbers');
terminal.writeln(' --help show this help');
terminal.writeln('Read files into stdout');
return;
}
const numbers = (argv.indexOf('-n') != -1) || (argv.indexOf('--number') != -1);
@ -20,11 +20,16 @@ module.exports = (argv, terminal) => {
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++;
})
if (numbers) {
lines.forEach((line, i) => {
terminal.write('\033[35m' + i + ' |\033[0m ');
})
} else {
terminal.write(file);
/// print % if no newline at eof
if (lines[lines.length - 1] != '')
terminal.write('\033[30;47m%\033[0m\n');
}
})
}

View File

@ -0,0 +1,60 @@
const { Terminal } = require('xterm');
const fs = require('../fs');
/**
*
* @param {string[]} argv
* @param {Terminal} terminal
*/
module.exports = (argv, terminal) => {
if (argv.indexOf('--help') != -1) {
terminal.writeln(`Usage: ${argv[0]} [dirs] [-a|--all] [-l]`);
terminal.writeln('Lists files in directories');
terminal.writeln(' -a --all List all files (including those who start with .)');
terminal.writeln(' -l Use long listing format');
return;
}
const has_arg = (arg) => {return argv.indexOf(arg) != -1};
let directories = [...argv];
const all = (has_arg('-a') || has_arg('--all'));
const long_format = has_arg('-l');
directories.shift();
// remove .* files if -a not specified
if (!all)
directories = directories.filter(x => x.startsWith('.'));
// remove arguments
directories = directories.filter(x => !x.startsWith('-'));
if (directories.length == 0) directories = ['.'];
// remove dublicates
directories = [...new Set(directories)];
directories.forEach((dir, i) => {
if (directories.length != 1) {
terminal.writeln(dir + ':');
terminal.writeln('');
}
let files = fs.readdirSync(dir);
files.forEach((file, i) => {
if (!fs.accessSync(file, fs.constants.X_OK))
terminal.write('\033[1;32m');
if (fs.accessSync(file, fs.constants.R_OK))
terminal.write('\033[35m');
terminal.write(file + '\033[0m ');
if ((i+1) % 5 == 0)
terminal.writeln('');
});
terminal.writeln('');
})
}

View File

@ -0,0 +1,3 @@
module.exports = (argv, terminal) => {
terminal.write('uwu\nowo\n');
}