Skip to content

Commit

Permalink
Merge pull request #97 from Flow-Works/thinliquid/qol
Browse files Browse the repository at this point in the history
🐛 bugfix: fix fs command bugs
  • Loading branch information
ThinLiquid authored Aug 12, 2023
2 parents c9cdbb8 + 51878d5 commit 6eb18d4
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 53 deletions.
Binary file not shown.
18 changes: 10 additions & 8 deletions public/builtin/apps/scripts/terminal/commands/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ export const metadata = {
};

export const exec = (fs, term, usr, dir, args) => {
if (fs.existsSync(args[1])) {
const content = fs.readFileSync(args[1]).split('\n');
return content.toString().split('\n');
} else if (fs.existsSync(dir.path + '/' + args[1])) {
const content = fs.readFileSync(dir.path + '/' + args[1]);
return content.toString().split('\n');
} else {
return `cat: ${args[1] || '/'}: No such file or directory`;
let path;
if (!args[1]) { return ''; };

if (path !== '/' && args[1].startsWith('/')) {
path = args[1];
} else if (path !== '/') {
if (fs.realpathSync(dir.path) == '/') { path = '/' + args[1]; }
else { path = fs.realpathSync(dir.path) + '/' + args[1]; }
};

return fs.readFileSync(path).toString().split('\n');
};
30 changes: 14 additions & 16 deletions public/builtin/apps/scripts/terminal/commands/cd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ export const metadata = {
};

export const exec = (fs, term, usr, dir, args) => {
if (fs.existsSync(args[1] || '/')) {
if (args[1]) {
if (!args[1].startsWith('/')) dir.set('/' + args[1]);
else dir.set(args[1] || '/');
} else {
dir.set(args[1] || '/');
}
} else if (fs.existsSync(dir.path + '/' + (args[1] || '/'))) {
if (args[1]) {
dir.set(dir.path + '/' + (args[1] || '/'));
} else {
dir.set(args[1] || '/');
}
} else {
return `-flush: cd: ${args[1] || '/'}: No such file or directory`;
}
let path;
if (!args[1]) { path = '/'; };

if (path !== '/' && args[1].startsWith('/')) {
path = args[1];
} else if (path !== '/') {
if (fs.realpathSync(dir.path) == '/') { path = '/' + args[1]; }
else { path = fs.realpathSync(dir.path) + '/' + args[1]; }
};

fs.readdirSync(path);
dir.set(path);

return '';
};
29 changes: 16 additions & 13 deletions public/builtin/apps/scripts/terminal/commands/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ export const metadata = {
};

export const exec = async (fs, term, usr, dir, args) => {
if (args[1] == 'clone') {
term.writeln(`Cloning into '${args[2].split(/(\\|\/)/g).pop()}'...`);
await git.clone({
fs,
http,
dir: dir.path + '/' + args[2].split(/(\\|\/)/g).pop(),
corsProxy: 'https://cors.isomorphic-git.org',
url: args[2],
singleBranch: true,
depth: 1
});
return 'Done!';
}
return new Promise(async (resolve) => {
if (args[1] == 'clone') {
term.writeln(`Cloning into '${args[2].split(/(\\|\/)/g).pop()}'...`);
await git.clone({
fs,
http,
dir: dir.path + '/' + args[2].split(/(\\|\/)/g).pop(),
corsProxy: 'https://cors.isomorphic-git.org',
url: args[2],
singleBranch: true,
depth: 1,
onMessage: (e) => term.writeln(e),
});
resolve('');
}
});
};
140 changes: 135 additions & 5 deletions public/builtin/apps/scripts/terminal/commands/ls.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,138 @@
import { table } from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';

const modeToString = (mode) => {
if (mode.mode) mode = mode.mode; // handle fs.stat

let result = new Buffer('drwxrwxrwx', 'ascii');

if (!(mode & 0o40000)) result[0] = 45;

for (let i = 1, bit = 0o400; bit; i++, bit >>= 1) {
if (!(mode & bit)) result[i] = 45; // ASCII for '-'
}

return result.toString();
};

export const metadata = {
cmd: 'ls',
description: 'List information about the FILEs (the current directory by default).'
cmd: 'ls',
description:
'List information about the FILEs (the current directory by default).',
};

export const exec = (fs, term, usr, dir) => {
return fs.readdirSync(dir.path);
};
export const exec = (fs, term, usr, dir, args) => {
let realPath = fs.realpathSync(dir.path);

let path;
if (!args[1]) { path = realPath; };

if (path !== realPath && args[1].startsWith('/')) {
path = args[1];
} else if (path !== realPath) {
if (realPath == '/') { path = '/' + args[1]; }
else { path = realPath + '/' + args[1]; }
};

const config = {
singleLine: true,
columns: [
{ alignment: 'left' },
{ alignment: 'left' },
{ alignment: 'left' },
{ alignment: 'left' },
{ alignment: 'right' },
{ alignment: 'left' },
{ alignment: 'left' },
],
};

const data = [];

if (JSON.stringify(fs.readdirSync(path)) == '[]') return '';

fs.readdirSync(path).forEach((name) => {
const stat = fs.statSync(path + '/' + name);
const date = new Intl.DateTimeFormat('en-US', {
dateStyle: 'medium',
timeStyle: 'short',
hour12: false,
})
.format(stat.mtime)
.replace(/\,/g, '')
.replace(new Date().getFullYear() + ' ', '');

if (stat.isFile()) {
let icon;

switch (name.split('.').pop()) {
default:
icon = '󰈔 ';
break;
case 'key':
icon = '󱆄 ';
break;
case 'crt':
icon = '󱆆 ';
break;
case 'txt':
case 'text':
icon = '󰈙 ';
break;
case 'jpg':
case 'jpeg':
case 'png':
case 'gif':
case 'webp':
icon = '󰈟 ';
break;

case 'cjs':
case 'mjs':
case 'js':
icon = '󰌞 ';
break;
case 'ts':
icon = '󰛦 ';
break;
case 'jsonc':
case 'json':
icon = '󰘦 ';
break;
case 'md':
icon = '󰍔 ';
break;
case 'css':
icon = '󰌜 ';
break;
case 'py':
icon = '󰌠 ';
break;
case 'html':
icon = '󰌝 ';
break;
}

data.push([
modeToString(stat.mode),
stat.nlink,
usr.username,
'user',
stat.size,
date,
icon + name,
]);
} else if (stat.isDirectory()) {
data.push([
modeToString(stat.mode),
stat.nlink,
usr.username,
'user',
stat.size,
date,
' ' + name,
]);
}
});

return table(data, config).split('\n');
};
12 changes: 10 additions & 2 deletions public/builtin/apps/scripts/terminal/commands/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ export const metadata = {
};

export const exec = (fs, term, usr, dir, args) => {
if (!args[1].startsWith('/')) fs.mkdirSync(dir.path + '/' + args[1]);
else fs.mkdirSync(dir.path + args[1]);
let path;

if (path !== '/' && args[1].startsWith('/')) {
path = args[1];
} else if (path !== '/') {
if (fs.realpathSync(dir.path) == '/') { path = '/' + args[1]; }
else { path = fs.realpathSync(dir.path) + '/' + args[1]; }
};

fs.mkdirSync(path);
};
17 changes: 9 additions & 8 deletions public/builtin/apps/scripts/terminal/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import sh from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
export class CommandsAddon {
_disposables = [];

promptMSG = () => `${c.blue('')}${c.bgBlue(` flow@${this.user.username}`)}${c.bgCyan(c.blue('') + ` ${this.dir.path}`)}${c.cyan('')} `;
promptMSG = async () => `${c.blue('')}${c.bgBlue(` flow@${this.user.username}`)}${c.bgCyan(c.blue('') + ` ${this.dir.path}`)}${c.cyan('')} `;

constructor(options, usr, dir) {
this.commandsMap = options.commands;
Expand All @@ -32,14 +32,15 @@ export class CommandsAddon {
if (e) console.error(e);

this.fs = require('fs');
this.promptMSG = async () => `${c.blue('')}${c.bgBlue(` flow@${this.user.username}`)}${c.bgCyan(c.blue('') + ` ${this.fs.realpathSync(this.dir.path)}`)}${c.cyan('')} `;
});
}

activate(term) {
this.terminal = term;

this.terminal.prompt = () => {
this.terminal.write('\r' + this.promptMSG());
this.terminal.prompt = async () => {
this.terminal.write('\r' + await this.promptMSG());
};

this.terminal.writeln('Welcome to FluSH!');
Expand Down Expand Up @@ -72,18 +73,18 @@ export class CommandsAddon {
if (this.current.length == 0) return;
term.writeln('');
const args = sh.parse(this.current);

// eslint-disable-next-line no-unused-vars
import('./commands/' + args[0] + '.js').then((command) => {
const cmd = eval(`command.exec(this.fs, this.terminal, this.user, this.dir, args)`);

import('./commands/' + args[0] + '.js').then(async (command) => {
const cmd = await command.exec(this.fs, this.terminal, this.user, this.dir, args);
if (cmd && !Array.isArray(cmd)) {
term.writeln(cmd);
} else if (Array.isArray(cmd)) {
cmd.forEach(ln => term.writeln(ln));
};
term.prompt();
}).catch((e) => {
term.writeln(c.red(e.name + ': ' + e.message));
console.error(e);
term.writeln(c.red(e.message));
term.prompt();
});

Expand Down
2 changes: 1 addition & 1 deletion public/builtin/apps/terminal.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<style>
@font-face {
font-family: 'JetBrains Mono Nerd Font';
src: url(/assets/fonts/JetBrainsMonoNerdFontMono-Regular.ttf);
src: url(/assets/fonts/JetBrainsMonoNerdFont-Regular.ttf);
}
</style>
</body>
Expand Down

0 comments on commit 6eb18d4

Please sign in to comment.