From 9a3731cf1803596ffcd97f0fed00eeff2202acd6 Mon Sep 17 00:00:00 2001 From: phyzical Date: Sat, 5 Feb 2022 23:27:08 +0800 Subject: [PATCH 1/4] updated grep for ls * implemented flags logic for grep * added --grep and -g * updated docs --- doc/source/basicgameplay/terminal.rst | 12 +++---- src/Terminal/HelpText.ts | 12 +++---- src/Terminal/commands/ls.tsx | 50 +++++++++++++++------------ 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/doc/source/basicgameplay/terminal.rst b/doc/source/basicgameplay/terminal.rst index 89f6abc70..2c5a7596b 100644 --- a/doc/source/basicgameplay/terminal.rst +++ b/doc/source/basicgameplay/terminal.rst @@ -335,7 +335,7 @@ Then to kill this script the same arguments would have to be used:: $ kill foo.script 50e3 sigma-cosmetics -If you are killing the script using its PID, then the PID argument must be numeric. +If you are killing the script using its PID, then the PID argument must be numeric. killall ^^^^^^^ @@ -347,7 +347,7 @@ Kills all scripts on the current server. ls ^^ - $ ls [dir] [| grep pattern] + $ ls [dir] [--grep pattern] 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 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. Examples:: // 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 - $ ls / | grep .js + $ ls / --grep .js // List files/directories with the word 'purchase' in the name, in the :code:`scripts` directory - $ ls scripts | grep purchase + $ ls scripts --grep purchase lscpu diff --git a/src/Terminal/HelpText.ts b/src/Terminal/HelpText.ts index ba19f49c8..d5346fdaa 100644 --- a/src/Terminal/HelpText.ts +++ b/src/Terminal/HelpText.ts @@ -26,7 +26,7 @@ export const TerminalHelpText: string[] = [ " hostname Displays the hostname of the machine", " kill [script/pid] [args...] Stops the specified script on the current server ", " 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", " mem [script] [-t n] Displays the amount of RAM required to run the script", " mv [src] [dest] Move/rename a text or script file", @@ -295,28 +295,28 @@ export const HelpTexts: IMap = { " ", ], 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 files will be displayed in alphabetical order. ", " ", "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:", " ", "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:", " ", - " ls / | grep .js", + " ls / --grep .js", " ", "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", " "], diff --git a/src/Terminal/commands/ls.tsx b/src/Terminal/commands/ls.tsx index a7ed3027d..5e218b2ad 100644 --- a/src/Terminal/commands/ls.tsx +++ b/src/Terminal/commands/ls.tsx @@ -8,6 +8,7 @@ import { BaseServer } from "../../Server/BaseServer"; import { evaluateDirectoryPath, getFirstParentDirectory, isValidDirectoryPath } from "../../Terminal/DirectoryHelpers"; import { IRouter } from "../../ui/Router"; import { ITerminal } from "../ITerminal"; +import * as libarg from "arg" export function ls( terminal: ITerminal, @@ -16,43 +17,46 @@ export function ls( server: BaseServer, args: (string | number | boolean)[], ): 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; 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(); } - // Grep - let filter = ""; // Grep - // Directory path let prefix = terminal.cwd(); if (!prefix.endsWith("/")) { prefix += "/"; } - // If there are 3+ arguments, then the last 3 must be for grep - if (numArgs >= 3) { - if (args[numArgs - 2] !== "grep" || args[numArgs - 3] !== "|") { - return incorrectUsage(); + // If no filter then it must be for listing a directory + if (filter === undefined) { + const dir = args[0] || "" + const newPath = evaluateDirectoryPath(dir + "", terminal.cwd()); + prefix = newPath || ""; + if (!prefix.endsWith("/")) { + prefix += "/"; } - 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("/")) { - prefix += "/"; - } - if (!isValidDirectoryPath(prefix)) { - return incorrectUsage(); - } + if (!isValidDirectoryPath(prefix)) { + return incorrectUsage(); } } From d41b19bced92db328ee9bfcad9a0db301f337d75 Mon Sep 17 00:00:00 2001 From: phyzical Date: Sun, 6 Feb 2022 00:05:48 +0800 Subject: [PATCH 2/4] added support for ls -l --- src/Terminal/commands/ls.tsx | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Terminal/commands/ls.tsx b/src/Terminal/commands/ls.tsx index 5e218b2ad..84750354e 100644 --- a/src/Terminal/commands/ls.tsx +++ b/src/Terminal/commands/ls.tsx @@ -21,7 +21,8 @@ export function ls( try { flags = libarg({ '--grep': String, - '-g': '--grep' + '-g': '--grep', + '-l': Boolean, }, { argv: args } ) @@ -37,7 +38,7 @@ export function ls( terminal.error("Incorrect usage of ls command. Usage: ls [dir] [-g, --grep pattern]"); } - if (numArgs > 3) { + if (numArgs > 4) { return incorrectUsage(); } @@ -47,17 +48,15 @@ export function ls( prefix += "/"; } - // If no filter then it must be for listing a directory - if (filter === undefined) { - const dir = args[0] || "" - const newPath = evaluateDirectoryPath(dir + "", terminal.cwd()); - prefix = newPath || ""; - if (!prefix.endsWith("/")) { - prefix += "/"; - } - if (!isValidDirectoryPath(prefix)) { - return incorrectUsage(); - } + // If first arg doesn't contain a - it must be the file/folder + const dir = (args[0] && typeof args[0] == "string" && !args[0].startsWith("-")) ? args[0] : "" + const newPath = evaluateDirectoryPath(dir + "", terminal.cwd()); + prefix = newPath || ""; + if (!prefix.endsWith("/")) { + prefix += "/"; + } + if (!isValidDirectoryPath(prefix)) { + return incorrectUsage(); } // Root directory, which is the same as no 'prefix' at all @@ -169,9 +168,9 @@ export function ls( ); } - function postSegments(segments: string[], style?: any, linked?: boolean): void { + function postSegments(segments: string[], flags: any, style?: any, linked?: boolean): void { const maxLength = Math.max(...segments.map((s) => s.length)) + 1; - const filesPerRow = Math.floor(80 / maxLength); + const filesPerRow = flags["-l"] === true ? 1 : Math.floor(80 / maxLength); for (let i = 0; i < segments.length; i++) { let row = ""; for (let col = 0; col < filesPerRow; col++) { @@ -202,6 +201,6 @@ export function ls( { segments: allScripts, style: { color: "yellow", fontStyle: "bold" }, linked: true }, ].filter((g) => g.segments.length > 0); for (let i = 0; i < groups.length; i++) { - postSegments(groups[i].segments, groups[i].style, groups[i].linked); + postSegments(groups[i].segments, flags, groups[i].style, groups[i].linked); } } From 26df6fd39c455e41a2c8c1f07bfffd5852cf3aec Mon Sep 17 00:00:00 2001 From: phyzical Date: Sun, 6 Feb 2022 00:12:28 +0800 Subject: [PATCH 3/4] doc updates --- doc/source/basicgameplay/terminal.rst | 8 +++++--- src/Terminal/HelpText.ts | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/doc/source/basicgameplay/terminal.rst b/doc/source/basicgameplay/terminal.rst index 2c5a7596b..b1e2c6c57 100644 --- a/doc/source/basicgameplay/terminal.rst +++ b/doc/source/basicgameplay/terminal.rst @@ -361,16 +361,18 @@ files. The :code:`--grep pattern` optional parameter allows you to only display files and directories with a certain pattern in their names. +The :code:`-l` optional parameter allows you to force each item onto a single line. + Examples:: // List files/directories with the '.script' extension in the current directory - $ ls --grep .script + $ ls -l --grep .script // List files/directories with the '.js' extension in the root directory - $ ls / --grep .js + $ ls / -l --grep .js // List files/directories with the word 'purchase' in the name, in the :code:`scripts` directory - $ ls scripts --grep purchase + $ ls scripts -l --grep purchase lscpu diff --git a/src/Terminal/HelpText.ts b/src/Terminal/HelpText.ts index d5346fdaa..68a2cb90d 100644 --- a/src/Terminal/HelpText.ts +++ b/src/Terminal/HelpText.ts @@ -295,7 +295,7 @@ export const HelpTexts: IMap = { " ", ], ls: [ - "Usage: ls [dir] [--grep pattern]", + "Usage: ls [dir] [--grep pattern] | [-l]", " ", "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. ", @@ -304,19 +304,21 @@ export const HelpTexts: IMap = { " ", "The '--grep pattern' optional parameter can be used to only display files whose filenames match the specified pattern.", " ", + "The '-l' optional parameter allows you to force each item onto a single line.", + " ", "Examples:", " ", "List all files with the '.script' extension in the current directory:", " ", - " ls --grep .script", + " ls -l --grep .script", " ", "List all files with the '.js' extension in the root directory:", " ", - " ls / --grep .js", + " ls / -l --grep .js", " ", "List all files with the word 'purchase' in the filename, in the 'scripts' directory:", " ", - " ls scripts --grep purchase", + " ls scripts -l --grep purchase", " ", ], lscpu: ["Usage: lscpu", " ", "Prints the number of CPU Cores the current server has", " "], From 59ec6213b92c5abbf750b715700b77e38f1d425a Mon Sep 17 00:00:00 2001 From: Master-Guy <566429+Master-Guy@users.noreply.github.com> Date: Thu, 17 Mar 2022 13:17:43 +0100 Subject: [PATCH 4/4] Processed review comments --- src/Terminal/HelpText.ts | 6 +++--- src/Terminal/commands/ls.tsx | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Terminal/HelpText.ts b/src/Terminal/HelpText.ts index 68a2cb90d..2e3bcb728 100644 --- a/src/Terminal/HelpText.ts +++ b/src/Terminal/HelpText.ts @@ -295,17 +295,17 @@ export const HelpTexts: IMap = { " ", ], ls: [ - "Usage: ls [dir] [--grep pattern] | [-l]", + "Usage: ls [dir] [-l] [--grep pattern]", " ", "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 '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 '-l' optional parameter allows you to force each item onto a single line.", " ", + "The '--grep pattern' optional parameter can be used to only display files whose filenames match the specified pattern.", + " ", "Examples:", " ", "List all files with the '.script' extension in the current directory:", diff --git a/src/Terminal/commands/ls.tsx b/src/Terminal/commands/ls.tsx index 84750354e..b47737d12 100644 --- a/src/Terminal/commands/ls.tsx +++ b/src/Terminal/commands/ls.tsx @@ -20,9 +20,9 @@ export function ls( let flags; try { flags = libarg({ + '-l': Boolean, '--grep': String, '-g': '--grep', - '-l': Boolean, }, { argv: args } ) @@ -35,7 +35,7 @@ export function ls( const numArgs = args.length; function incorrectUsage(): void { - terminal.error("Incorrect usage of ls command. Usage: ls [dir] [-g, --grep pattern]"); + terminal.error("Incorrect usage of ls command. Usage: ls [dir] [-l] [-g, --grep pattern]"); } if (numArgs > 4) {