- initial changes to data shapes
- ls command needs fixing
This commit is contained in:
+1
-6
@@ -3,12 +3,7 @@ import type { Result } from "./result";
|
||||
import type { ShellState } from "./shell_state";
|
||||
import type { ShellError } from "./util/errors";
|
||||
|
||||
export type CommandFn = (
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
) => Promise<CommandOutput<ShellError>>;
|
||||
export type CommandFn = (io: CommandIO, state: ShellState, args: string[]) => Promise<CommandOutput<ShellError>>;
|
||||
|
||||
export interface CommandDescriptor {
|
||||
fn: CommandFn;
|
||||
|
||||
@@ -7,12 +7,7 @@ import type { ShellError } from "@core/util/errors";
|
||||
export const COMMAND_NAME = "";
|
||||
|
||||
//Need to implement escape sequences
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||
|
||||
return {
|
||||
|
||||
@@ -10,12 +10,7 @@ import { resolvePath } from "@core/util/path_resolver";
|
||||
export const COMMAND_NAME = "cat";
|
||||
|
||||
//Need to implement escape sequences
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||
for (const arg of args) {
|
||||
const inode = resolvePath(state, arg);
|
||||
if (inode === -1) {
|
||||
|
||||
@@ -13,12 +13,7 @@ import { resolvePath } from "@core/util/path_resolver";
|
||||
|
||||
export const COMMAND_NAME = "cd";
|
||||
|
||||
async function main(
|
||||
__io: CommandIO,
|
||||
state: ShellState,
|
||||
__flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
async function main(__io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||
if (args.length > 1) {
|
||||
return {
|
||||
state,
|
||||
|
||||
@@ -7,12 +7,7 @@ import type { ShellError } from "@core/util/errors";
|
||||
export const COMMAND_NAME = "echo";
|
||||
|
||||
//Need to implement escape sequences
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
__flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||
const joined = args.join(" ");
|
||||
const encoded = new TextEncoder().encode(joined + "\n");
|
||||
|
||||
|
||||
+16
-6
@@ -11,14 +11,22 @@ import { convertToUnit } from "@termos/util/format_unit";
|
||||
|
||||
export const COMMAND_NAME = "ls";
|
||||
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||
const entries: { inode: number; path: string }[] = [];
|
||||
const e = new TextEncoder();
|
||||
|
||||
const flags: Set<string> = new Set();
|
||||
|
||||
for (const arg of args) {
|
||||
if (arg.startsWith("--")) {
|
||||
arg.slice(2);
|
||||
flags.add(arg);
|
||||
} else if (arg.startsWith("-")) {
|
||||
arg.slice(1);
|
||||
flags.add(arg);
|
||||
}
|
||||
}
|
||||
|
||||
const vars = flags.filter(
|
||||
// Match at least 1 character between each side of a singular = character
|
||||
f =>
|
||||
@@ -238,6 +246,8 @@ function longMode(io: CommandIO, state: ShellState, nodes: TreeNode[], flags: Se
|
||||
}
|
||||
}
|
||||
|
||||
function parseFlags() {}
|
||||
|
||||
function parseMode(mode: number) {
|
||||
const owner = (mode >> 6) & 7;
|
||||
const group = (mode >> 3) & 7;
|
||||
|
||||
@@ -8,12 +8,7 @@ import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
|
||||
const COMMAND_NAME = "async";
|
||||
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
__flags: string[],
|
||||
__args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
async function main(io: CommandIO, state: ShellState, __args: string[]): Promise<CommandOutput<ShellError>> {
|
||||
while (true) {
|
||||
const out = await io.getSignal();
|
||||
|
||||
|
||||
@@ -10,12 +10,7 @@ import { resolvePath } from "@core/util/path_resolver";
|
||||
export const COMMAND_NAME = "touch";
|
||||
|
||||
//Need to implement escape sequences
|
||||
async function main(
|
||||
io: CommandIO,
|
||||
state: ShellState,
|
||||
flags: string[],
|
||||
args: string[],
|
||||
): Promise<CommandOutput<ShellError>> {
|
||||
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||
for (const arg of args) {
|
||||
const inode = resolvePath(state, arg);
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
+1
-52
@@ -6,13 +6,7 @@ interface UnclosedQuoteError {
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
interface ParsedCommand {
|
||||
command: string;
|
||||
flags: string[];
|
||||
args: string[];
|
||||
}
|
||||
|
||||
function tokenize(input: string): string[] {
|
||||
export function tokenize(input: string): string[] {
|
||||
enum State {
|
||||
Normal,
|
||||
SingleQuote,
|
||||
@@ -98,48 +92,3 @@ function tokenize(input: string): string[] {
|
||||
flushBuffer();
|
||||
return tokens;
|
||||
}
|
||||
|
||||
export function parseCommand(input: string): ParsedCommand {
|
||||
const tokens = tokenize(input);
|
||||
const command = tokens[0] ?? "";
|
||||
const flags: string[] = [];
|
||||
const args: string[] = [];
|
||||
|
||||
let pastEndOfOptions = false;
|
||||
|
||||
for (let i = 1; i < tokens.length; i++) {
|
||||
const token = tokens[i]!;
|
||||
|
||||
if (pastEndOfOptions) {
|
||||
args.push(token);
|
||||
continue;
|
||||
}
|
||||
if (token === "--") {
|
||||
pastEndOfOptions = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (token.startsWith("--")) {
|
||||
flags.push(token.slice(2));
|
||||
continue;
|
||||
} else if (token.startsWith("-")) {
|
||||
if (token.includes("=")) {
|
||||
flags.push(token.slice(1));
|
||||
continue;
|
||||
}
|
||||
for (const char of token.slice(1)) {
|
||||
flags.push(char);
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
args.push(token);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
command,
|
||||
flags,
|
||||
args,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user