summaryrefslogtreecommitdiff
path: root/Components/DisplayFile
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-11-28 17:31:34 +0100
committerMinteck <contact@minteck.org>2022-11-28 17:31:34 +0100
commit7923aa8942b55884320ef2428417e3ee4b121613 (patch)
tree7993632f2898b1998f25b11ce40a8d2eb3d44730 /Components/DisplayFile
downloadmistyos-og-7923aa8942b55884320ef2428417e3ee4b121613.tar.gz
mistyos-og-7923aa8942b55884320ef2428417e3ee4b121613.tar.bz2
mistyos-og-7923aa8942b55884320ef2428417e3ee4b121613.zip
Initial commitHEADmane
Diffstat (limited to 'Components/DisplayFile')
-rw-r--r--Components/DisplayFile/hex.js44
-rw-r--r--Components/DisplayFile/index.js43
-rw-r--r--Components/DisplayFile/metadata.yml46
3 files changed, 133 insertions, 0 deletions
diff --git a/Components/DisplayFile/hex.js b/Components/DisplayFile/hex.js
new file mode 100644
index 0000000..f062608
--- /dev/null
+++ b/Components/DisplayFile/hex.js
@@ -0,0 +1,44 @@
+const chalk = require(__dirname + '/../../MistyCore/node_modules/chalk');
+
+function hexEight(dec) {
+ let hex = Math.round(dec).toString(16);
+ let zero = "00000000";
+ return zero.substring(0, 8 - hex.length) + hex;
+}
+
+function fixLength(string, length) {
+ let end = " ".repeat(length);
+ if (string.length > length) return string.substring(0, length);
+
+ return string + end.substring(0, length - string.length);
+}
+
+module.exports = (buffer, color) => {
+ let ret = "";
+ let lines = buffer.toString("hex").match(/.{1,32}/g).map(i => i.match(/.{1,2}/g).join(" "));
+
+ let byte = 0;
+ for (let line of lines) {
+ if (color) {
+ ret += chalk.gray(hexEight(byte)) + " " + fixLength(line, 47).split(" ").map((i, _) => {
+ if ((_ + 1) % 2 === 1) {
+ return chalk.red(i);
+ } else {
+ return chalk.magenta(i);
+ }
+ }).join(" ") + " " + chalk.blue(Buffer.from(line.replaceAll(" ", ""), "hex").toString().replace(/[\x00-\x1F]/gm, chalk.gray("."))) + "\n";
+ } else {
+ ret += hexEight(byte) + " " + fixLength(line, 47) + " " + Buffer.from(line.replaceAll(" ", ""), "hex").toString().replace(/[\x00-\x1F]/gm, ".") + "\n";
+ }
+
+ byte += line.replaceAll(" ", "").length / 2;
+ }
+
+ if (color) {
+ ret += chalk.gray(hexEight(byte));
+ } else {
+ ret += hexEight(byte);
+ }
+
+ return ret;
+} \ No newline at end of file
diff --git a/Components/DisplayFile/index.js b/Components/DisplayFile/index.js
new file mode 100644
index 0000000..c6b3f25
--- /dev/null
+++ b/Components/DisplayFile/index.js
@@ -0,0 +1,43 @@
+let arguments = JSON.parse(process.argv[2]);
+const chalk = require(__dirname + '/../../MistyCore/node_modules/chalk');
+const fs = require("fs");
+const child_process = require("child_process");
+
+if (arguments._finals.length > 0) {
+ for (let file of arguments._finals) {
+ if (fs.existsSync(file)) {
+ try {
+ fs.accessSync(file, fs.constants.R_OK);
+
+ if (arguments['hex']) {
+ fs.writeFileSync("/System/Volumes/VM/HexDump", require('./hex')(fs.readFileSync(file), !arguments['pagination']));
+ file = "/System/Volumes/VM/HexDump";
+ }
+
+ if (fs.lstatSync(file).isDirectory()) {
+ console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("Is a directory: ") + file);
+ } else {
+ if (arguments['pagination']) {
+ console.clear();
+ child_process.execFileSync("/System/Binaries/busybox", [ "less", "-~", "-S", "--", file ], { stdio: "inherit" });
+ console.clear();
+ } else {
+ if (arguments['search']) {
+ console.log(chalk.white(fs.readFileSync(file).toString().replace(arguments['search'], chalk.bold.red(arguments['search']))));
+ } else {
+ console.log(chalk.white(fs.readFileSync(file).toString()));
+ }
+ }
+ }
+
+ if (fs.existsSync("/System/Volumes/VM/HexDump")) fs.unlinkSync("/System/Volumes/VM/HexDump");
+ } catch (e) {
+ console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("Permission denied: ") + file);
+ }
+ } else {
+ console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("No such file or directory: ") + file);
+ }
+ }
+} else {
+ console.log(chalk.bgYellow.white("<!>") + " " + chalk.yellow("Missing operand"));
+} \ No newline at end of file
diff --git a/Components/DisplayFile/metadata.yml b/Components/DisplayFile/metadata.yml
new file mode 100644
index 0000000..e05d233
--- /dev/null
+++ b/Components/DisplayFile/metadata.yml
@@ -0,0 +1,46 @@
+description: Displays one or multiple file(s) in the terminal
+internal: false
+
+aliases:
+ - cat
+ - type
+ - more
+ - less
+
+manual:
+ summary: |
+ This command displays one or multiple file(s) in the terminal.
+
+ parameters:
+ - name: Pagination
+ description: Enable pagination (scrolling)
+ required: false
+
+ - name: Hex
+ description: Display an hexadecimal dump of the file instead of the contents of the file itself
+ required: false
+
+ - name: Search
+ value: Query
+ description: Search for text in the file(s); does not work with -Pagination
+ required: false
+
+ final:
+ name: File
+ description: The file(s) to display
+ required: true
+ multiple: true
+ command: false
+ path: true
+ daemon: false
+
+ examples:
+ - command: DisplayFile /User/Document.txt
+ description: Display the "Document.txt" file
+
+ - command: DisplayFile -Pagination /User/Essay.txt
+ description: Display the "Essay.txt" file with pagination
+
+ compatibility:
+ mistyos: '>=1.0.0'
+ kernel: '>=5.10.0' \ No newline at end of file