mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-19 04:35:46 +01:00
updated grep for ls
* implemented flags logic for grep * added --grep and -g * updated docs
This commit is contained in:
parent
9ddb1c4379
commit
9a3731cf18
@ -347,7 +347,7 @@ Kills all scripts on the current server.
|
|||||||
ls
|
ls
|
||||||
^^
|
^^
|
||||||
|
|
||||||
$ ls [dir] [| grep pattern]
|
$ ls [dir] [--grep pattern]
|
||||||
|
|
||||||
Prints files and directories on the current server to the Terminal screen.
|
Prints files and directories on the current server to the Terminal screen.
|
||||||
|
|
||||||
@ -358,19 +358,19 @@ followed by the files (also in alphabetical order).
|
|||||||
The :code:`dir` optional parameter allows you to specify the directory for which to display
|
The :code:`dir` optional parameter allows you to specify the directory for which to display
|
||||||
files.
|
files.
|
||||||
|
|
||||||
The :code:`| grep pattern` optional parameter allows you to only display files and directories
|
The :code:`--grep pattern` optional parameter allows you to only display files and directories
|
||||||
with a certain pattern in their names.
|
with a certain pattern in their names.
|
||||||
|
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
// List files/directories with the '.script' extension in the current directory
|
// List files/directories with the '.script' extension in the current directory
|
||||||
$ ls | grep .script
|
$ ls --grep .script
|
||||||
|
|
||||||
// List files/directories with the '.js' extension in the root directory
|
// List files/directories with the '.js' extension in the root directory
|
||||||
$ ls / | grep .js
|
$ ls / --grep .js
|
||||||
|
|
||||||
// List files/directories with the word 'purchase' in the name, in the :code:`scripts` directory
|
// List files/directories with the word 'purchase' in the name, in the :code:`scripts` directory
|
||||||
$ ls scripts | grep purchase
|
$ ls scripts --grep purchase
|
||||||
|
|
||||||
|
|
||||||
lscpu
|
lscpu
|
||||||
|
@ -26,7 +26,7 @@ export const TerminalHelpText: string[] = [
|
|||||||
" hostname Displays the hostname of the machine",
|
" hostname Displays the hostname of the machine",
|
||||||
" kill [script/pid] [args...] Stops the specified script on the current server ",
|
" kill [script/pid] [args...] Stops the specified script on the current server ",
|
||||||
" killall Stops all running scripts on the current machine",
|
" killall Stops all running scripts on the current machine",
|
||||||
" ls [dir] [| grep pattern] Displays all files on the machine",
|
" ls [dir] [--grep pattern] Displays all files on the machine",
|
||||||
" lscpu Displays the number of CPU cores on the machine",
|
" lscpu Displays the number of CPU cores on the machine",
|
||||||
" mem [script] [-t n] Displays the amount of RAM required to run the script",
|
" mem [script] [-t n] Displays the amount of RAM required to run the script",
|
||||||
" mv [src] [dest] Move/rename a text or script file",
|
" mv [src] [dest] Move/rename a text or script file",
|
||||||
@ -295,28 +295,28 @@ export const HelpTexts: IMap<string[]> = {
|
|||||||
" ",
|
" ",
|
||||||
],
|
],
|
||||||
ls: [
|
ls: [
|
||||||
"Usage: ls [dir] [| grep pattern]",
|
"Usage: ls [dir] [--grep pattern]",
|
||||||
" ",
|
" ",
|
||||||
"The ls command, with no arguments, prints all files and directories on the current server's directory to the Terminal screen. ",
|
"The ls command, with no arguments, prints all files and directories on the current server's directory to the Terminal screen. ",
|
||||||
"The files will be displayed in alphabetical order. ",
|
"The files will be displayed in alphabetical order. ",
|
||||||
" ",
|
" ",
|
||||||
"The 'dir' optional parameter can be used to display files/directories in another directory.",
|
"The 'dir' optional parameter can be used to display files/directories in another directory.",
|
||||||
" ",
|
" ",
|
||||||
"The '| grep pattern' optional parameter can be used to only display files whose filenames match the specified pattern.",
|
"The '--grep pattern' optional parameter can be used to only display files whose filenames match the specified pattern.",
|
||||||
" ",
|
" ",
|
||||||
"Examples:",
|
"Examples:",
|
||||||
" ",
|
" ",
|
||||||
"List all files with the '.script' extension in the current directory:",
|
"List all files with the '.script' extension in the current directory:",
|
||||||
" ",
|
" ",
|
||||||
" ls | grep .script",
|
" ls --grep .script",
|
||||||
" ",
|
" ",
|
||||||
"List all files with the '.js' extension in the root directory:",
|
"List all files with the '.js' extension in the root directory:",
|
||||||
" ",
|
" ",
|
||||||
" ls / | grep .js",
|
" ls / --grep .js",
|
||||||
" ",
|
" ",
|
||||||
"List all files with the word 'purchase' in the filename, in the 'scripts' directory:",
|
"List all files with the word 'purchase' in the filename, in the 'scripts' directory:",
|
||||||
" ",
|
" ",
|
||||||
" ls scripts | grep purchase",
|
" ls scripts --grep purchase",
|
||||||
" ",
|
" ",
|
||||||
],
|
],
|
||||||
lscpu: ["Usage: lscpu", " ", "Prints the number of CPU Cores the current server has", " "],
|
lscpu: ["Usage: lscpu", " ", "Prints the number of CPU Cores the current server has", " "],
|
||||||
|
@ -8,6 +8,7 @@ import { BaseServer } from "../../Server/BaseServer";
|
|||||||
import { evaluateDirectoryPath, getFirstParentDirectory, isValidDirectoryPath } from "../../Terminal/DirectoryHelpers";
|
import { evaluateDirectoryPath, getFirstParentDirectory, isValidDirectoryPath } from "../../Terminal/DirectoryHelpers";
|
||||||
import { IRouter } from "../../ui/Router";
|
import { IRouter } from "../../ui/Router";
|
||||||
import { ITerminal } from "../ITerminal";
|
import { ITerminal } from "../ITerminal";
|
||||||
|
import * as libarg from "arg"
|
||||||
|
|
||||||
export function ls(
|
export function ls(
|
||||||
terminal: ITerminal,
|
terminal: ITerminal,
|
||||||
@ -16,37 +17,41 @@ export function ls(
|
|||||||
server: BaseServer,
|
server: BaseServer,
|
||||||
args: (string | number | boolean)[],
|
args: (string | number | boolean)[],
|
||||||
): void {
|
): void {
|
||||||
|
let flags;
|
||||||
|
try {
|
||||||
|
flags = libarg({
|
||||||
|
'--grep': String,
|
||||||
|
'-g': '--grep'
|
||||||
|
},
|
||||||
|
{ argv: args }
|
||||||
|
)
|
||||||
|
} catch (e) {
|
||||||
|
// catch passing only -g / --grep with no string to use as the search
|
||||||
|
incorrectUsage()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const filter = flags['--grep']
|
||||||
|
|
||||||
const numArgs = args.length;
|
const numArgs = args.length;
|
||||||
function incorrectUsage(): void {
|
function incorrectUsage(): void {
|
||||||
terminal.error("Incorrect usage of ls command. Usage: ls [dir] [| grep pattern]");
|
terminal.error("Incorrect usage of ls command. Usage: ls [dir] [-g, --grep pattern]");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numArgs > 4 || numArgs === 2) {
|
if (numArgs > 3) {
|
||||||
return incorrectUsage();
|
return incorrectUsage();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grep
|
|
||||||
let filter = ""; // Grep
|
|
||||||
|
|
||||||
// Directory path
|
// Directory path
|
||||||
let prefix = terminal.cwd();
|
let prefix = terminal.cwd();
|
||||||
if (!prefix.endsWith("/")) {
|
if (!prefix.endsWith("/")) {
|
||||||
prefix += "/";
|
prefix += "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there are 3+ arguments, then the last 3 must be for grep
|
// If no filter then it must be for listing a directory
|
||||||
if (numArgs >= 3) {
|
if (filter === undefined) {
|
||||||
if (args[numArgs - 2] !== "grep" || args[numArgs - 3] !== "|") {
|
const dir = args[0] || ""
|
||||||
return incorrectUsage();
|
const newPath = evaluateDirectoryPath(dir + "", terminal.cwd());
|
||||||
}
|
prefix = newPath || "";
|
||||||
filter = args[numArgs - 1] + "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the second argument is not a pipe, then it must be for listing a directory
|
|
||||||
if (numArgs >= 1 && args[0] !== "|") {
|
|
||||||
const newPath = evaluateDirectoryPath(args[0] + "", terminal.cwd());
|
|
||||||
prefix = newPath ? newPath : "";
|
|
||||||
if (prefix != null) {
|
|
||||||
if (!prefix.endsWith("/")) {
|
if (!prefix.endsWith("/")) {
|
||||||
prefix += "/";
|
prefix += "/";
|
||||||
}
|
}
|
||||||
@ -54,7 +59,6 @@ export function ls(
|
|||||||
return incorrectUsage();
|
return incorrectUsage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Root directory, which is the same as no 'prefix' at all
|
// Root directory, which is the same as no 'prefix' at all
|
||||||
if (prefix === "/") {
|
if (prefix === "/") {
|
||||||
|
Loading…
Reference in New Issue
Block a user