Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/shell redirection #23

Merged
merged 18 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/Terminal/EditableLine.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function getCharPosition (offset: number): { top: number, left: number } {
let characterCount = 0;
const walker = document.createTreeWalker(inputBox.value, NodeFilter.SHOW_TEXT, null, false);
let node: Node | null = null;
// eslint-disable-next-line
while (node = walker.nextNode()) {
const textLength = node.textContent!.length;
if (characterCount + textLength > offset) {
Expand Down Expand Up @@ -106,7 +107,7 @@ async function onKeydown (e: KeyboardEvent) {
}
}

async function handleControlKey(key: string) {
async function handleControlKey (key: string) {
const { content } = props;
switch (key) {
case 'v':
Expand Down
2 changes: 1 addition & 1 deletion composables/cwd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { VirtualPath } from '~/lib/path';
export const useCwdStore = createGlobalState(() => {
const homeDir = VirtualPath.homeDir(window?.localStorage.getItem('username') || 'guest');
const cwd = ref(homeDir);
function switchCwd(newDir: string) {
function switchCwd (newDir: string) {
cwd.value = cwd.value.resolve(newDir);
}
return {
Expand Down
4 changes: 2 additions & 2 deletions composables/umask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { createGlobalState } from '@vueuse/core';

export const useUmaskStore = createGlobalState(() => {
const umask = ref(window?.localStorage?.getItem('umask') || '000111111010');
function changeUmask(newUmask: string & { length: 12 } & { [index: number]: '0' | '1' }) {
function changeUmask (newUmask: string & { length: 12 } & { [index: number]: '0' | '1' }) {
umask.value = newUmask;
localStorage.setItem('umask', newUmask);
}
function clearUmask() {
function clearUmask () {
umask.value = '000111111010';
localStorage.setItem('umask', umask.value);
}
Expand Down
2 changes: 1 addition & 1 deletion composables/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useUserStore = createGlobalState(() => {
const userId = ref(null);
const groupId = ref(null);
const createdAt = ref(null);
function switchUser(name: string) {
function switchUser (name: string) {
username.value = name;
localStorage.setItem('username', name);
clearUmask();
Expand Down
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ export default withNuxt({
semi: ['error', 'always'],
indent: ['error', 2],
quotes: ['error', 'single'],
'@typescript-eslint/no-explicit-any': 'warn',
'space-before-function-paren': ['error', 'always'],
},
});
6 changes: 2 additions & 4 deletions lib/command/impls/cd.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { fileService } from '~/services';
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';

export const cd: CommandFunc = async function(...args) {
export const cd: AsyncCommandFunc = async function (...args) {
// discard `cd`
args.shift();
// discard first space
args.shift();

if (args.length !== 1 || !args[0].trim()) {
return [
Expand Down
8 changes: 2 additions & 6 deletions lib/command/impls/cp.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { fileService } from '~/services';
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';

export const cp: CommandFunc = async function(...args) {
export const cp: AsyncCommandFunc = async function (...args) {
// discard `cp`
args.shift();
// discard first space
args.shift();

const src = formatArg(args.shift());
args.shift();
const dest = formatArg(args.shift());
args.shift();
if (args.length > 0 || !src || !dest) {
return [
'Invalid use of cp. Run \'help cp\'',
Expand Down
4 changes: 1 addition & 3 deletions lib/command/impls/echo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { formatArg } from '../utils';
import type { CommandFunc } from './types';

export const echo: CommandFunc = function(...args) {
export const echo: CommandFunc = function (...args) {
// discard `echo`
args.shift();
// discard first space
args.shift();

return [
args.map((arg) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/command/impls/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const commandDescriptions: Record<Command, CommandDescription> = {
},
};

function getDescription(commandName: string): string[] {
function getDescription (commandName: string): string[] {
const commandDescription = commandDescriptions[commandName as Command];
if (commandDescription !== undefined) {
return [
Expand All @@ -96,7 +96,7 @@ function getDescription(commandName: string): string[] {
}
}

export const help: CommandFunc = function(...args) {
export const help: CommandFunc = function (...args) {
args.shift();
args.shift();

Expand Down
10 changes: 4 additions & 6 deletions lib/command/impls/ls.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { userService } from '~/services/users';
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';
import { fileService } from '~/services/files';
import { groupService } from '~/services/groups';

export const ls: CommandFunc = async function(...args) {
export const ls: AsyncCommandFunc = async function (...args) {
// discard `ls`
args.shift();
// discard first space
args.shift();

if (args.length > 1) {
return ['Expect an optional dirname as argument.'];
Expand Down Expand Up @@ -38,7 +36,7 @@ export const ls: CommandFunc = async function(...args) {
];
};

function formatFileType(fileType: string): string {
function formatFileType (fileType: string): string {
switch (fileType) {
case 'file': return '-';
case 'directory': return 'd';
Expand All @@ -47,7 +45,7 @@ function formatFileType(fileType: string): string {
}
}

function formatPermissionBits(permissionBits: string): string {
function formatPermissionBits (permissionBits: string): string {
const ownerRead = Number.parseInt(permissionBits[3]);
const ownerWrite = Number.parseInt(permissionBits[4]);
const ownerExecute = Number.parseInt(permissionBits[5]);
Expand Down
8 changes: 3 additions & 5 deletions lib/command/impls/mkdir.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { fileService } from '~/services';
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';

export const mkdir: CommandFunc = async function(...args) {
export const mkdir: AsyncCommandFunc = async function (...args) {
// discard `mkdir`
args.shift();
// discard first space
args.shift();

if (args.length > 1 || args.length === 0) {
if (args.length !== 0) {
return ['Invalid use of mkdir. Run \'help mkdir\''];
}

Expand Down
8 changes: 2 additions & 6 deletions lib/command/impls/mv.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { fileService } from '~/services';
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';

export const mv: CommandFunc = async function(...args) {
export const mv: AsyncCommandFunc = async function (...args) {
// discard `mv`
args.shift();
// discard first space
args.shift();

const src = formatArg(args.shift());
args.shift();
const dest = formatArg(args.shift());
args.shift();
if (args.length > 0 || !src || !dest) {
return [
'Invalid use of mv. Run \'help mv\'',
Expand Down
6 changes: 2 additions & 4 deletions lib/command/impls/rm.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { uniq } from 'lodash-es';
import { fileService } from '~/services';
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';

export const rm: CommandFunc = async function(...args) {
export const rm: AsyncCommandFunc = async function (...args) {
// discard `rm`
args.shift();
// discard first space
args.shift();

const { cwd } = useCwdStore();
const filenames = uniq(args.filter((arg) => arg.trim()).map((arg) => cwd.value.resolve(formatArg(arg)!).toString()));
Expand Down
8 changes: 2 additions & 6 deletions lib/command/impls/su.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';
import { userService } from '~/services/users';

export const su: CommandFunc = async function(...args) {
export const su: AsyncCommandFunc = async function (...args) {
// discard `su`
args.shift();
// discard first space
args.shift();

let username;
let password;

while (args.length) {
const opt = args.shift();
args.shift();
if (args.length === 0) return ['Invalid use of su. Run \'help su\''];
switch (opt) {
case '-u':
Expand All @@ -25,7 +22,6 @@ export const su: CommandFunc = async function(...args) {
default:
return ['Invalid use of su. Run \'help su\''];
}
args.shift();
}

if (username === undefined) {
Expand Down
8 changes: 3 additions & 5 deletions lib/command/impls/touch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { fileService } from '~/services';
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';

export const touch: CommandFunc = async function(...args) {
export const touch: AsyncCommandFunc = async function (...args) {
// discard `touch`
args.shift();
// discard first space
args.shift();

if (args.length > 1 || args.length === 0) {
if (args.length !== 1) {
return ['Invalid use of touch. Run \'help touch\''];
}

Expand Down
3 changes: 2 additions & 1 deletion lib/command/impls/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type CommandFunc = (...args: string[]) => Promise<string[]> | string[];
export type CommandFunc = (...args: string[]) => string[];
export type AsyncCommandFunc = (...args: string[]) => Promise<string[]>;

export enum Command {
ECHO = 'echo',
Expand Down
14 changes: 6 additions & 8 deletions lib/command/impls/umask.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';

export const umask: CommandFunc = async function(...args) {
export const umask: AsyncCommandFunc = async function (...args) {
// discard `umask`
args.shift();
// discard first space
args.shift();

if (args.length === 0) {
const { umask } = useUmaskStore();
Expand All @@ -23,18 +21,18 @@ export const umask: CommandFunc = async function(...args) {
return ['Invalid umask'];
}
const { changeUmask } = useUmaskStore();
changeUmask(umaskFromOct(umask as any));
changeUmask(umaskFromOct(umask as any) as any);
return [
'Change umask successfully',
];
};

function isOctDigit(c: string): boolean {
function isOctDigit (c: string): boolean {
const n = Number.parseInt(c);
return c.length === 1 && 0 <= n && n <= 7;
}

function umaskToOct(umask: string): string {
function umaskToOct (umask: string): string {
const ownerRead = Number.parseInt(umask[3]);
const ownerWrite = Number.parseInt(umask[4]);
const ownerExecute = Number.parseInt(umask[5]);
Expand All @@ -50,7 +48,7 @@ function umaskToOct(umask: string): string {
return `${ownerOct}${groupOct}${otherOct}`;
}

function umaskFromOct(octs: string): string {
function umaskFromOct (octs: string): string {
const ownerOct = Number.parseInt(octs[0]);
const groupOct = Number.parseInt(octs[1]);
const otherOct = Number.parseInt(octs[2]);
Expand Down
8 changes: 2 additions & 6 deletions lib/command/impls/useradd.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { formatArg } from '../utils';
import type { CommandFunc } from './types';
import type { AsyncCommandFunc } from './types';
import { userService } from '~/services/users';

export const useradd: CommandFunc = async function(...args) {
export const useradd: AsyncCommandFunc = async function (...args) {
// discard `useradd`
args.shift();
// discard first space
args.shift();

let username;
let password;

while (args.length) {
const opt = args.shift();
args.shift();
if (args.length === 0) return ['Invalid use of useradd. Run \'help useradd\''];
switch (opt) {
case '-u':
Expand All @@ -25,7 +22,6 @@ export const useradd: CommandFunc = async function(...args) {
default:
return ['Invalid use of useradd. Run \'help useradd\''];
}
args.shift();
}

if (username === undefined) {
Expand Down
Loading
Loading