Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/utils/bin/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as bin from './index';
import config from '../../../config.json';
import { list as listHistory } from '../history';

// Help
export const help = async (args: string[]): Promise<string> => {
Expand Down Expand Up @@ -138,6 +139,11 @@ export const sudo = async (args?: string[]): Promise<string> => {
return `Permission denied: with little power comes... no responsibility? `;
};

// history
export const history = async (args: string[]): Promise<string> => {
return listHistory();
};

// Banner
export const banner = (args?: string[]): string => {
return `
Expand Down
69 changes: 69 additions & 0 deletions src/utils/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
type History = Record<number, string>;
const history: History = {};
let historyIndex = 0;

export function list(): string {
const maximumNumberOfDigits = historyIndex.toString().length;

const array: Array<string> = [];
for (const index in history) {
const value = history[index];
const paddedIndex = index.toString().padStart(maximumNumberOfDigits, ' ');

array.push(`${paddedIndex}: ${value}`);
}

return array.join('\n');
}

export function push(command: string): History {
if (getLast() !== command) {
history[++historyIndex] = command;
}

return history;
}

export function getCommandIfHistoryExpansion(input): string {
let command = undefined;
const regExpExecArray = /!(-?)(?:(\d+)|!)/.exec(input);
if (regExpExecArray) {
const isMinus =
Boolean(regExpExecArray[1]) && regExpExecArray[1].length > 0;

/**
* either digits or '!'
*/
const n = regExpExecArray[2];
const index = parseInt(n);
if (Number.isNaN(index)) {
command = getLast();
} else {
/**
* !0, !-0 are invalid
*/
if (index > 0) {
if (isMinus) {
command = getMinus(index);
} else {
command = get(index);
}
}
}
}

return command;
}

function get(index: number): string {
return history[index];
}

function getMinus(index: number): string {
// '!!' is a synonym for '!-1' so + 1
return history[historyIndex - index + 1];
}

function getLast(): string {
return history[historyIndex];
}
14 changes: 13 additions & 1 deletion src/utils/shell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import * as bin from './bin';
import { getCommandIfHistoryExpansion, push as pushHistory } from './history';

export const shell = async (
command: string,
Expand All @@ -8,17 +9,28 @@ export const shell = async (
setCommand: React.Dispatch<React.SetStateAction<string>>,
) => {
const args = command.split(' ');
args[0] = args[0].toLowerCase();
args[0] = args[0].toLowerCase().trim();

let expandedCommand = getCommandIfHistoryExpansion(args[0]);
if (expandedCommand) {
// like bash with histverify on or zsh default
setCommand(expandedCommand);
return;
}

if (args[0] === 'clear') {
pushHistory(args[0]);
clearHistory();
} else if (command === '') {
setHistory('');
} else if (Object.keys(bin).indexOf(args[0]) === -1) {
pushHistory(args[0]);
setHistory(
`shell: command not found: ${args[0]}. Try 'help' to get started.`,
);
} else {
pushHistory(args[0]);

const output = await bin[args[0]](args.slice(1));
setHistory(output);
}
Expand Down